11 #include "rigidbody.h"
15 #include "shaders/standard.h"
20 submodel sm_road
, sm_terrain
;
29 teleport_gate gates
[16];
32 rigidbody temp_rbs
[32];
39 static void render_world( m4x4f projection
, m4x3f camera
)
43 m4x3f identity_matrix
;
44 m4x3_identity( identity_matrix
);
46 render_terrain( projection
, camera
[3] );
47 scene_bind( &world
.geo
);
48 scene_draw( &world
.geo
);
50 glDisable(GL_CULL_FACE
);
51 scene_bind( &world
.foliage
);
52 scene_draw( &world
.foliage
);
53 glEnable(GL_CULL_FACE
);
56 static void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
58 for( int i
=0; i
<3; i
++ )
59 v3_copy( world
.geo
.verts
[ hit
->tri
[i
] ].co
, tri
[i
] );
62 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
64 return scene_raycast( &world
.geo
, pos
, dir
, hit
);
67 static int ray_hit_is_ramp( ray_hit
*hit
)
69 return hit
->tri
[0] < world
.sm_road
.vertex_count
;
72 static bh_system bh_system_rigidbodies
;
73 static void world_load(void)
76 scene_init( &world
.geo
);
77 model
*mworld
= vg_asset_read( "models/mp_dev.mdl" );
79 for( int i
=0; i
<mworld
->layer_count
; i
++ )
81 submodel
*sm
= model_get_submodel( mworld
, i
);
82 if( !strcmp( sm
->material
, "surf" ) )
83 scene_add_model( &world
.geo
, mworld
, sm
, sm
->pivot
, 0.0f
, 1.0f
);
86 scene_copy_slice( &world
.geo
, &world
.sm_road
);
88 for( int i
=0; i
<mworld
->layer_count
; i
++ )
90 submodel
*sm
= model_get_submodel( mworld
, i
);
91 if( !strcmp( sm
->material
, "terrain" ) )
92 scene_add_model( &world
.geo
, mworld
, sm
, sm
->pivot
, 0.0f
, 1.0f
);
95 scene_copy_slice( &world
.geo
, &world
.sm_terrain
);
98 * TODO: Parametric marker import
100 v3_copy( model_marker_get( mworld
, "start" )->co
, world
.tutorial
);
106 world
.gate_count
= 0;
107 for( int i
=0; i
<mworld
->marker_count
; i
++ )
109 model_marker
*ga
= model_get_marker( mworld
, i
);
111 if( ga
->classtype
== k_classtype_gate
)
113 struct classtype_gate
*data
= get_entdata_raw( mworld
, ga
);
117 model_marker
*gb
= model_get_marker( mworld
, data
->target
);
119 teleport_gate
*gate
= &world
.gates
[ world
.gate_count
++ ];
121 v3_copy( ga
->co
, gate
->co
[0] );
122 v3_copy( gb
->co
, gate
->co
[1] );
123 v4_copy( ga
->q
, gate
->q
[0] );
124 v4_copy( gb
->q
, gate
->q
[1] );
125 v2_copy( ga
->s
, gate
->dims
);
127 gate_transform_update( gate
);
133 * Load water mesh (1 per world)
135 for( int i
=0; i
<mworld
->layer_count
; i
++ )
137 submodel
*sm
= model_get_submodel( mworld
, i
);
138 if( !strcmp( sm
->material
, "water" ) )
141 model_unpack_submodel( mworld
, &surf
, sm
);
144 water_set_surface( &surf
, sm
->pivot
[1] );
150 scene_upload( &world
.geo
);
151 scene_bh_create( &world
.geo
);
153 water_compute_depth( world
.geo
.bbx
);
155 scene_init( &world
.foliage
);
156 model
*mfoliage
= vg_asset_read("models/rs_foliage.mdl");
159 * TODO: Load any other meshes into the foliage scene, and create rbs for
165 for( int i
=0; i
<mworld
->layer_count
; i
++ )
167 submodel
*sm
= model_get_submodel( mworld
, i
);
168 if( !strcmp( sm
->material
, "surf" ) ||
169 !strcmp( sm
->material
, "terrain" ) ||
170 !strcmp( sm
->material
, "water" ) )
174 q_m3x3( sm
->q
, transform
);
175 v3_copy( sm
->pivot
, transform
[3] );
176 scene_add_foliage( &world
.foliage
, mworld
, sm
, transform
);
178 rigidbody
*rb
= &world
.temp_rbs
[ world
.rb_count
++ ];
180 box_copy( sm
->bbx
, rb
->bbx
);
181 v3_copy( sm
->pivot
, rb
->co
);
183 v4_copy( sm
->q
, rb
->q
);
184 rb_update_transform( rb
);
190 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
195 submodel
*sm_blob
= submodel_get( mfoliage
, "blob" ),
196 *sm_tree
= submodel_get( mfoliage
, "tree" );
198 for( int i
=0;i
<100000;i
++ )
201 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
202 v3_add( pos
, world
.geo
.bbx
[0], pos
);
207 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
209 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
210 hit
.pos
[1] > wrender
.height
)
212 v4f qsurface
, qrandom
;
215 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
217 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
218 q_axis_angle( qsurface
, axis
, angle
);
219 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
220 q_mul( qsurface
, qrandom
, qsurface
);
221 q_m3x3( qsurface
, transform
);
223 v3_copy( hit
.pos
, transform
[3] );
225 if( vg_randf() < 0.00000006f
)
227 m3x3_identity( transform
);
228 scene_add_foliage( &world
.foliage
, mfoliage
, sm_tree
, transform
);
231 scene_add_foliage( &world
.foliage
, mfoliage
, sm_blob
, transform
);
237 scene_upload( &world
.foliage
);
239 bh_create( &world
.bhcubes
,
240 &bh_system_rigidbodies
, world
.temp_rbs
, world
.rb_count
);