11 #include "rigidbody.h"
14 #include "shaders/standard.h"
19 submodel sm_road
, sm_terrain
;
28 teleport_gate gates
[16];
31 rigidbody temp_rbs
[32];
36 static void render_world( m4x4f projection
, m4x3f camera
)
40 m4x3f identity_matrix
;
41 m4x3_identity( identity_matrix
);
43 render_terrain( projection
, camera
[3] );
44 scene_bind( &world
.geo
);
45 scene_draw( &world
.geo
);
47 glDisable(GL_CULL_FACE
);
48 scene_bind( &world
.foliage
);
49 scene_draw( &world
.foliage
);
50 glEnable(GL_CULL_FACE
);
53 static void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
55 for( int i
=0; i
<3; i
++ )
56 v3_copy( world
.geo
.verts
[ hit
->tri
[i
] ].co
, tri
[i
] );
59 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
61 return bvh_raycast( &world
.geo
, pos
, dir
, hit
);
64 static int ray_hit_is_ramp( ray_hit
*hit
)
66 return hit
->tri
[0] < world
.sm_road
.vertex_count
;
69 static void world_load(void)
72 scene_init( &world
.geo
);
73 model
*mworld
= vg_asset_read( "models/mp_dev.mdl" );
75 for( int i
=0; i
<mworld
->layer_count
; i
++ )
77 submodel
*sm
= model_get_submodel( mworld
, i
);
78 if( !strcmp( sm
->material
, "surf" ) )
79 scene_add_model( &world
.geo
, mworld
, sm
, sm
->pivot
, 0.0f
, 1.0f
);
82 scene_copy_slice( &world
.geo
, &world
.sm_road
);
84 for( int i
=0; i
<mworld
->layer_count
; i
++ )
86 submodel
*sm
= model_get_submodel( mworld
, i
);
87 if( !strcmp( sm
->material
, "terrain" ) )
88 scene_add_model( &world
.geo
, mworld
, sm
, sm
->pivot
, 0.0f
, 1.0f
);
91 scene_copy_slice( &world
.geo
, &world
.sm_terrain
);
94 * TODO: Parametric marker import
96 v3_copy( model_marker_get( mworld
, "start" )->co
, world
.tutorial
);
102 world
.gate_count
= 0;
103 for( int i
=0; i
<mworld
->marker_count
; i
++ )
105 model_marker
*ga
= model_get_marker( mworld
, i
);
107 if( ga
->classtype
== k_classtype_gate
)
109 struct classtype_gate
*data
= get_entdata_raw( mworld
, ga
);
113 model_marker
*gb
= model_get_marker( mworld
, data
->target
);
115 teleport_gate
*gate
= &world
.gates
[ world
.gate_count
++ ];
117 v3_copy( ga
->co
, gate
->co
[0] );
118 v3_copy( gb
->co
, gate
->co
[1] );
119 v4_copy( ga
->q
, gate
->q
[0] );
120 v4_copy( gb
->q
, gate
->q
[1] );
121 v2_copy( ga
->s
, gate
->dims
);
123 gate_transform_update( gate
);
129 * Load water mesh (1 per world)
131 for( int i
=0; i
<mworld
->layer_count
; i
++ )
133 submodel
*sm
= model_get_submodel( mworld
, i
);
134 if( !strcmp( sm
->material
, "water" ) )
137 model_unpack_submodel( mworld
, &surf
, sm
);
140 water_set_surface( &surf
, sm
->pivot
[1] );
146 scene_upload( &world
.geo
);
147 bvh_create( &world
.geo
);
149 water_compute_depth( world
.geo
.bbx
);
151 scene_init( &world
.foliage
);
152 model
*mfoliage
= vg_asset_read("models/rs_foliage.mdl");
155 * TODO: Load any other meshes into the foliage scene, and create rbs for
161 scene_add_foliage( &world
.foliage
, mworld
, boxtest
, world
.box
.to_world
);
166 submodel
*boxtest
= submodel_get( mworld
, "cubey" );
170 for( int i
=0; i
<mworld
->layer_count
; i
++ )
172 submodel
*sm
= model_get_submodel( mworld
, i
);
173 if( !strcmp( sm
->material
, "surf" ) ||
174 !strcmp( sm
->material
, "terrain" ) ||
175 !strcmp( sm
->material
, "water" ) )
179 q_m3x3( sm
->q
, transform
);
180 v3_copy( sm
->pivot
, transform
[3] );
181 scene_add_foliage( &world
.foliage
, mworld
, sm
, transform
);
183 rigidbody
*rb
= &world
.temp_rbs
[ world
.rb_count
++ ];
185 box_copy( sm
->bbx
, rb
->bbx
);
186 v3_copy( sm
->pivot
, rb
->co
);
188 v4_copy( sm
->q
, rb
->q
);
189 rb_update_transform( rb
);
196 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
201 submodel
*sm_blob
= submodel_get( mfoliage
, "blob" ),
202 *sm_tree
= submodel_get( mfoliage
, "tree" );
204 for( int i
=0;i
<100000;i
++ )
207 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
208 v3_add( pos
, world
.geo
.bbx
[0], pos
);
213 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
215 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
216 hit
.pos
[1] > wrender
.height
)
218 v4f qsurface
, qrandom
;
221 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
223 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
224 q_axis_angle( qsurface
, axis
, angle
);
225 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
226 q_mul( qsurface
, qrandom
, qsurface
);
227 q_m3x3( qsurface
, transform
);
229 v3_copy( hit
.pos
, transform
[3] );
231 if( vg_randf() < 0.00000006f
)
233 m3x3_identity( transform
);
234 scene_add_foliage( &world
.foliage
, mfoliage
, sm_tree
, transform
);
237 scene_add_foliage( &world
.foliage
, mfoliage
, sm_blob
, transform
);
243 scene_upload( &world
.foliage
);