3 static int ray_world( v3f pos
, v3f dir
, ray_hit
*hit
);
12 #include "rigidbody.h"
16 #include "shaders/standard.h"
21 submodel sm_road
, sm_terrain
;
26 teleport_gate gates
[16];
29 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 scene_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 scene_bh_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 for( int i
=0; i
<mworld
->layer_count
; i
++ )
163 submodel
*sm
= model_get_submodel( mworld
, i
);
164 if( !strcmp( sm
->material
, "surf" ) ||
165 !strcmp( sm
->material
, "terrain" ) ||
166 !strcmp( sm
->material
, "water" ) )
170 q_m3x3( sm
->q
, transform
);
171 v3_copy( sm
->pivot
, transform
[3] );
172 scene_add_foliage( &world
.foliage
, mworld
, sm
, transform
);
174 rigidbody
*rb
= &world
.temp_rbs
[ world
.rb_count
++ ];
176 box_copy( sm
->bbx
, rb
->bbx
);
177 v3_copy( sm
->pivot
, rb
->co
);
179 v4_copy( sm
->q
, rb
->q
);
180 rb_update_transform( rb
);
186 v3_sub( world
.geo
.bbx
[1], world
.geo
.bbx
[0], volume
);
191 submodel
*sm_blob
= submodel_get( mfoliage
, "blob" ),
192 *sm_tree
= submodel_get( mfoliage
, "tree" );
194 for( int i
=0;i
<100000;i
++ )
197 v3_mul( volume
, (v3f
){ vg_randf(), 1000.0f
, vg_randf() }, pos
);
198 v3_add( pos
, world
.geo
.bbx
[0], pos
);
203 if( ray_world( pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &hit
))
205 if( hit
.normal
[1] > 0.8f
&& !ray_hit_is_ramp(&hit
) &&
206 hit
.pos
[1] > water_height()+10.0f
)
208 v4f qsurface
, qrandom
;
211 v3_cross( (v3f
){0.0f
,1.0f
,0.0f
}, hit
.normal
, axis
);
213 float angle
= v3_dot(hit
.normal
,(v3f
){0.0f
,1.0f
,0.0f
});
214 q_axis_angle( qsurface
, axis
, angle
);
215 q_axis_angle( qrandom
, (v3f
){0.0f
,1.0f
,0.0f
}, vg_randf()*VG_TAUf
);
216 q_mul( qsurface
, qrandom
, qsurface
);
217 q_m3x3( qsurface
, transform
);
219 v3_copy( hit
.pos
, transform
[3] );
221 if( vg_randf() < 0.0006f
)
223 m3x3_identity( transform
);
224 scene_add_foliage( &world
.foliage
, mfoliage
, sm_tree
, transform
);
227 scene_add_foliage( &world
.foliage
, mfoliage
, sm_blob
, transform
);
233 scene_upload( &world
.foliage
);
235 bh_create( &world
.bhcubes
,
236 &bh_system_rigidbodies
, world
.temp_rbs
, world
.rb_count
);