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];
41 static void render_world( m4x4f projection
, m4x3f camera
)
45 m4x3f identity_matrix
;
46 m4x3_identity( identity_matrix
);
48 render_terrain( projection
, camera
[3] );
49 scene_bind( &world
.geo
);
50 scene_draw( &world
.geo
);
52 glDisable(GL_CULL_FACE
);
53 scene_bind( &world
.foliage
);
54 scene_draw( &world
.foliage
);
55 glEnable(GL_CULL_FACE
);
58 static void ray_world_get_tri( ray_hit
*hit
, v3f tri
[3] )
60 for( int i
=0; i
<3; i
++ )
61 v3_copy( world
.geo
.verts
[ hit
->tri
[i
] ].co
, tri
[i
] );
64 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
)
66 return bvh_raycast( &world
.geo
, pos
, dir
, hit
);
69 static int ray_hit_is_ramp( ray_hit
*hit
)
71 return hit
->tri
[0] < world
.sm_road
.vertex_count
;
74 static bh_system bh_system_rigidbodies
;
75 static void world_load(void)
78 scene_init( &world
.geo
);
79 model
*mworld
= vg_asset_read( "models/mp_dev.mdl" );
81 for( int i
=0; i
<mworld
->layer_count
; i
++ )
83 submodel
*sm
= model_get_submodel( mworld
, i
);
84 if( !strcmp( sm
->material
, "surf" ) )
85 scene_add_model( &world
.geo
, mworld
, sm
, sm
->pivot
, 0.0f
, 1.0f
);
88 scene_copy_slice( &world
.geo
, &world
.sm_road
);
90 for( int i
=0; i
<mworld
->layer_count
; i
++ )
92 submodel
*sm
= model_get_submodel( mworld
, i
);
93 if( !strcmp( sm
->material
, "terrain" ) )
94 scene_add_model( &world
.geo
, mworld
, sm
, sm
->pivot
, 0.0f
, 1.0f
);
97 scene_copy_slice( &world
.geo
, &world
.sm_terrain
);
100 * TODO: Parametric marker import
102 v3_copy( model_marker_get( mworld
, "start" )->co
, world
.tutorial
);
108 world
.gate_count
= 0;
109 for( int i
=0; i
<mworld
->marker_count
; i
++ )
111 model_marker
*ga
= model_get_marker( mworld
, i
);
113 if( ga
->classtype
== k_classtype_gate
)
115 struct classtype_gate
*data
= get_entdata_raw( mworld
, ga
);
119 model_marker
*gb
= model_get_marker( mworld
, data
->target
);
121 teleport_gate
*gate
= &world
.gates
[ world
.gate_count
++ ];
123 v3_copy( ga
->co
, gate
->co
[0] );
124 v3_copy( gb
->co
, gate
->co
[1] );
125 v4_copy( ga
->q
, gate
->q
[0] );
126 v4_copy( gb
->q
, gate
->q
[1] );
127 v2_copy( ga
->s
, gate
->dims
);
129 gate_transform_update( gate
);
135 * Load water mesh (1 per world)
137 for( int i
=0; i
<mworld
->layer_count
; i
++ )
139 submodel
*sm
= model_get_submodel( mworld
, i
);
140 if( !strcmp( sm
->material
, "water" ) )
143 model_unpack_submodel( mworld
, &surf
, sm
);
146 water_set_surface( &surf
, sm
->pivot
[1] );
152 scene_upload( &world
.geo
);
153 bvh_create( &world
.geo
);
155 water_compute_depth( world
.geo
.bbx
);
157 scene_init( &world
.foliage
);
158 model
*mfoliage
= vg_asset_read("models/rs_foliage.mdl");
161 * TODO: Load any other meshes into the foliage scene, and create rbs for
167 for( int i
=0; i
<mworld
->layer_count
; i
++ )
169 submodel
*sm
= model_get_submodel( mworld
, i
);
170 if( !strcmp( sm
->material
, "surf" ) ||
171 !strcmp( sm
->material
, "terrain" ) ||
172 !strcmp( sm
->material
, "water" ) )
176 q_m3x3( sm
->q
, transform
);
177 v3_copy( sm
->pivot
, transform
[3] );
178 scene_add_foliage( &world
.foliage
, mworld
, sm
, transform
);
180 rigidbody
*rb
= &world
.temp_rbs
[ world
.rb_count
++ ];
182 box_copy( sm
->bbx
, rb
->bbx
);
183 v3_copy( sm
->pivot
, rb
->co
);
185 v4_copy( sm
->q
, rb
->q
);
186 rb_update_transform( rb
);
192 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
197 submodel
*sm_blob
= submodel_get( mfoliage
, "blob" ),
198 *sm_tree
= submodel_get( mfoliage
, "tree" );
200 for( int i
=0;i
<100000;i
++ )
203 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
204 v3_add( pos
, world
.geo
.bbx
[0], pos
);
209 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
211 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
212 hit
.pos
[1] > wrender
.height
)
214 v4f qsurface
, qrandom
;
217 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
219 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
220 q_axis_angle( qsurface
, axis
, angle
);
221 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
222 q_mul( qsurface
, qrandom
, qsurface
);
223 q_m3x3( qsurface
, transform
);
225 v3_copy( hit
.pos
, transform
[3] );
227 if( vg_randf() < 0.00000006f
)
229 m3x3_identity( transform
);
230 scene_add_foliage( &world
.foliage
, mfoliage
, sm_tree
, transform
);
233 scene_add_foliage( &world
.foliage
, mfoliage
, sm_blob
, transform
);
239 scene_upload( &world
.foliage
);
241 world
.bhcubes
.user
= world
.temp_rbs
;
242 bh_create( &world
.bhcubes
, &bh_system_rigidbodies
, world
.rb_count
);