a lot
[carveJwlIkooP6JGAAIwe30JlM.git] / world.h
1 #include "common.h"
2
3 static int ray_world( v3f pos, v3f dir, ray_hit *hit );
4
5 #ifndef WORLD_H
6 #define WORLD_H
7
8 #include "scene.h"
9 #include "terrain.h"
10 #include "render.h"
11 #include "water.h"
12 #include "rigidbody.h"
13 #include "gate.h"
14 #include "bvh.h"
15 #include "lighting.h"
16 #include "model.h"
17 #include "traffic.h"
18
19 #include "shaders/terrain.h"
20 #include "shaders/sky.h"
21 #include "shaders/planeinf.h"
22 #include "shaders/standard.h"
23 #include "shaders/vblend.h"
24 #include "shaders/gpos.h"
25 #include "shaders/fscolour.h"
26
27 static struct gworld
28 {
29 /* gameplay */
30 struct respawn_point
31 {
32 v3f co;
33 v4f q;
34 char name[32];
35 }
36 spawns[32];
37 u32 spawn_count;
38
39 teleport_gate gates[64];
40 u32 gate_count;
41
42 /* Paths */
43 traffic_node traffic[128];
44 u32 traffic_count;
45
46 traffic_driver van_man[6];
47
48 /* Physics */
49 rigidbody temp_rbs[128];
50 u32 rb_count;
51 bh_tree bhcubes;
52
53 /* Rendering & geometry */
54 scene geo, foliage, props;
55 mdl_submesh sm_surface, sm_other;
56
57 glmesh skybox, skydome;
58 mdl_submesh dome_upper, dome_lower;
59
60 glmesh cars;
61 mdl_submesh car_holden;
62 }
63 world;
64
65 vg_tex2d tex_terrain_colours = { .path = "textures/gradients.qoi",
66 .flags = VG_TEXTURE_CLAMP|VG_TEXTURE_NEAREST };
67
68 vg_tex2d tex_terrain_noise = { .path = "textures/garbage.qoi",
69 .flags = VG_TEXTURE_NEAREST };
70
71 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
72 {
73 for( int i=0; i<3; i++ )
74 v3_copy( world.geo.verts[ hit->tri[i] ].co, tri[i] );
75 }
76
77 static int ray_world( v3f pos, v3f dir, ray_hit *hit )
78 {
79 return scene_raycast( &world.geo, pos, dir, hit );
80 }
81
82 static int ray_hit_is_ramp( ray_hit *hit )
83 {
84 return hit->tri[0] < world.sm_surface.vertex_count;
85 }
86
87 static void world_register(void)
88 {
89 shader_terrain_register();
90 shader_sky_register();
91 shader_planeinf_register();
92 shader_gpos_register();
93 shader_fscolour_register();
94 }
95
96 static void world_free(void)
97 {
98 /* TODO.. */
99 }
100
101 static void render_world_depth( m4x4f projection, m4x3f camera );
102
103 static void add_all_if_material( scene *pscene, mdl_header *mdl, u32 id )
104 {
105 for( int i=0; i<mdl->node_count; i++ )
106 {
107 mdl_node *pnode = mdl_node_from_id( mdl, i );
108
109 for( int j=0; j<pnode->submesh_count; j++ )
110 {
111 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
112
113 if( sm->material_id == id )
114 {
115 m4x3f transform;
116 mdl_node_transform( pnode, transform );
117 scene_add_submesh( pscene, mdl, sm, transform );
118 }
119 }
120 }
121 }
122
123 static void world_apply_foliage(void)
124 {
125 scene_init( &world.foliage );
126 mdl_header *mfoliage = mdl_load("models/rs_foliage.mdl");
127
128 v3f volume;
129 v3_sub( world.geo.bbx[1], world.geo.bbx[0], volume );
130 volume[1] = 1.0f;
131
132 m4x3f transform;
133 mdl_node *mblob = mdl_node_from_name( mfoliage, "blob" );
134 mdl_submesh *sm_blob = mdl_node_submesh( mfoliage, mblob, 0 );
135
136 for( int i=0;i<100000;i++ )
137 {
138 v3f pos;
139 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
140 pos[1] = 1000.0f;
141 v3_add( pos, world.geo.bbx[0], pos );
142
143 ray_hit hit;
144 hit.dist = INFINITY;
145
146 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
147 {
148 if( hit.normal[1] > 0.8f && !ray_hit_is_ramp(&hit) &&
149 hit.pos[1] > water_height()+10.0f )
150 {
151 v4f qsurface, qrandom;
152 v3f axis;
153
154 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
155
156 float angle = v3_dot(hit.normal,(v3f){0.0f,1.0f,0.0f});
157 q_axis_angle( qsurface, axis, angle );
158 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
159 q_mul( qsurface, qrandom, qsurface );
160 q_m3x3( qsurface, transform );
161
162 v3_copy( hit.pos, transform[3] );
163 scene_add_submesh( &world.foliage, mfoliage, sm_blob, transform);
164 }
165 }
166 }
167
168 scene_upload( &world.foliage );
169 free( mfoliage );
170 }
171
172 static void world_load(void)
173 {
174 mdl_header *mworld = mdl_load( "models/mp_dev.mdl" );
175
176 world.spawn_count = 0;
177 world.gate_count = 0;
178 world.rb_count = 0;
179 world.traffic_count = 0;
180
181 scene_init( &world.geo );
182 scene_init( &world.props );
183
184 /*
185 * Compile meshes into the world scenes
186 */
187 u32 mat_surf = 0,
188 mat_surf_oob = 0,
189 mat_vertex_blend = 0;
190
191 for( int i=1; i<mworld->material_count; i++ )
192 {
193 mdl_material *mat = mdl_material_from_id( mworld, i );
194 const char *mat_name = mdl_pstr( mworld, mat->pstr_name );
195
196 vg_info( "%d %s\n", mat->pstr_name, mat_name );
197
198 if( !strcmp( "surf", mat_name ))
199 mat_surf = i;
200 else if( !strcmp( "surf_oob", mat_name ))
201 mat_surf_oob = i;
202 else if( !strcmp( "vertex_blend", mat_name ))
203 mat_vertex_blend = i;
204 }
205
206 if( mat_surf )
207 add_all_if_material( &world.geo, mworld, mat_surf );
208 if( mat_vertex_blend )
209 add_all_if_material( &world.geo, mworld, mat_vertex_blend );
210
211
212 scene_copy_slice( &world.geo, &world.sm_surface );
213
214 if( mat_surf_oob )
215 add_all_if_material( &world.geo, mworld, mat_surf_oob );
216 else
217 vg_warn( "No OOB surface\n" );
218
219 scene_bh_create( &world.geo );
220 scene_upload( &world.geo );
221
222 if( mat_vertex_blend )
223 add_all_if_material( &world.props, mworld, mat_vertex_blend );
224
225 /* TODO bvh? */
226
227 /*
228 * Process entities
229 */
230 for( int i=0; i<mworld->node_count; i++ )
231 {
232 mdl_node *pnode = mdl_node_from_id( mworld, i );
233
234 if( pnode->classtype == k_classtype_none )
235 {}
236 else if( pnode->classtype == k_classtype_gate )
237 {
238 struct classtype_gate *entgate = mdl_get_entdata( mworld, pnode );
239
240 if( entgate->target )
241 {
242 mdl_node *pother = mdl_node_from_id( mworld, entgate->target );
243
244 teleport_gate *gate = &world.gates[ world.gate_count ++ ];
245
246 v3_copy( pnode->co, gate->co[0] );
247 v3_copy( pother->co, gate->co[1] );
248 v4_copy( pnode->q, gate->q[0] );
249 v4_copy( pother->q, gate->q[1] );
250 v2_copy( pnode->s, gate->dims );
251
252 gate_transform_update( gate );
253 }
254 }
255 else if( pnode->classtype == k_classtype_block )
256 {
257 struct classtype_block *block = mdl_get_entdata( mworld, pnode );
258
259 m4x3f transform;
260 mdl_node_transform( pnode, transform );
261
262 rigidbody *rb = &world.temp_rbs[ world.rb_count ++ ];
263
264 box_copy( block->bbx, rb->bbx ); /* TODO: apply scale */
265 v3_copy( pnode->co, rb->co );
266 rb_init( rb );
267 v4_copy( pnode->q, rb->q );
268 rb_update_transform( rb );
269 }
270 else if( pnode->classtype == k_classtype_spawn )
271 {
272 struct respawn_point *rp = &world.spawns[ world.spawn_count ++ ];
273
274 v3_copy( pnode->co, rp->co );
275 v4_copy( pnode->q, rp->q );
276 strcpy( rp->name, mdl_pstr( mworld, pnode->pstr_name ) );
277 }
278 else if( pnode->classtype == k_classtype_water )
279 {
280 if( wrender.enabled )
281 {
282 vg_warn( "Multiple water surfaces in level! ('%s')\n",
283 mdl_pstr( mworld, pnode->pstr_name ));
284 continue;
285 }
286
287 mdl_submesh *sm = mdl_node_submesh( mworld, pnode, 0 );
288
289 if( sm )
290 {
291 glmesh surf;
292 mdl_unpack_submesh( mworld, &surf, sm );
293 water_init();
294 water_set_surface( &surf, pnode->co[1] );
295 }
296 }
297 else if( pnode->classtype == k_classtype_car_path )
298 {
299 struct classtype_car_path *p = mdl_get_entdata( mworld, pnode );
300 traffic_node *tn = &world.traffic[ world.traffic_count ];
301 tn->mn_next = NULL;
302 tn->mn_next1 = NULL;
303
304 if( p->target ) tn->mn_next = mdl_node_from_id( mworld, p->target );
305 if( p->target1 ) tn->mn_next1 = mdl_node_from_id( mworld, p->target1 );
306
307 m4x3f transform;
308 mdl_node_transform( pnode, transform );
309 m3x3_mulv( transform, (v3f){1.0f,0.0f,0.0f}, tn->h );
310 v3_copy( transform[3], tn->co );
311
312 pnode->sub_uid = world.traffic_count ++;
313 }
314 }
315
316 traffic_finalize( world.traffic, world.traffic_count );
317 for( int i=0; i<vg_list_size(world.van_man); i++ )
318 world.van_man[i].current =&world.traffic[vg_randint(world.traffic_count)];
319
320 scene_upload( &world.props );
321
322 bh_create( &world.bhcubes,
323 &bh_system_rigidbodies, world.temp_rbs, world.rb_count );
324
325 world_apply_foliage();
326 free( mworld );
327
328 /*
329 * Rendering the depth map
330 */
331 m4x4f ortho;
332 m4x3f camera;
333
334 v3f extent;
335 v3_sub( world.geo.bbx[1], world.geo.bbx[0], extent );
336
337 float fl = world.geo.bbx[0][0],
338 fr = world.geo.bbx[1][0],
339 fb = world.geo.bbx[0][2],
340 ft = world.geo.bbx[1][2],
341 rl = 1.0f / (fr-fl),
342 tb = 1.0f / (ft-fb);
343
344 m4x4_zero( ortho );
345 ortho[0][0] = 2.0f * rl;
346 ortho[2][1] = 2.0f * tb;
347 ortho[3][0] = (fr + fl) * -rl;
348 ortho[3][1] = (ft + fb) * -tb;
349 ortho[3][3] = 1.0f;
350 m4x3_identity( camera );
351
352 glViewport( 0, 0, 1024, 1024 );
353 glDisable(GL_DEPTH_TEST);
354 glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
355 shader_fscolour_use();
356 shader_fscolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
357 render_fsquad();
358
359 glEnable(GL_BLEND);
360 glBlendFunc(GL_ONE, GL_ONE);
361 glBlendEquation(GL_MAX);
362 render_world_depth( ortho, camera );
363 glDisable(GL_BLEND);
364 glEnable(GL_DEPTH_TEST);
365
366 /*
367 * TODO: World settings entity
368 */
369 struct ub_world_lighting *winfo = &gpipeline.ub_world_lighting;
370 v4_copy( wrender.plane, winfo->g_water_plane );
371
372 v4f bounds;
373 bounds[0] = world.geo.bbx[0][0];
374 bounds[1] = world.geo.bbx[0][2];
375 bounds[2] = 1.0f/ (world.geo.bbx[1][0]-world.geo.bbx[0][0]);
376 bounds[3] = 1.0f/ (world.geo.bbx[1][2]-world.geo.bbx[0][2]);
377 v4_copy( bounds, winfo->g_depth_bounds );
378
379 winfo->g_water_fog = 0.04f;
380 render_update_lighting_ub();
381
382 }
383
384 static void world_init(void)
385 {
386 vg_tex2d_init( (vg_tex2d *[]){ &tex_terrain_colours,
387 &tex_terrain_noise }, 2 );
388
389
390 mdl_header *msky = mdl_load("models/rs_skydome.mdl");
391 mdl_unpack_glmesh( msky, &world.skydome );
392
393 mdl_node *nlower = mdl_node_from_name( msky, "dome_lower" ),
394 *nupper = mdl_node_from_name( msky, "dome_upper" );
395
396 world.dome_lower = *mdl_node_submesh( msky, nlower, 0 );
397 world.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
398 free(msky);
399
400 mdl_header *mcars = mdl_load( "models/rs_cars.mdl" );
401 mdl_unpack_glmesh( mcars, &world.cars );
402 mdl_node *nholden = mdl_node_from_name( mcars, "holden" );
403 world.car_holden = *mdl_node_submesh( mcars, nholden, 0 );
404 free(mcars);
405 }
406
407 static void world_update(void)
408 {
409 for( int i=0; i<vg_list_size(world.van_man); i++ )
410 {
411 traffic_drive( &world.van_man[i] );
412 traffic_visualize_car( &world.van_man[i] );
413 }
414 }
415
416 /*
417 * Rendering
418 */
419
420 static void bind_terrain_textures(void)
421 {
422 vg_tex2d_bind( &tex_terrain_noise, 0 );
423 vg_tex2d_bind( &tex_terrain_colours, 1 );
424 }
425
426 static void render_props( m4x4f projection, v3f camera )
427 {
428 m4x3f identity_matrix;
429 m4x3_identity( identity_matrix );
430
431 shader_vblend_use();
432 shader_vblend_uTexGarbage(0);
433 shader_vblend_uTexGradients(1);
434 shader_link_standard_ub( _shader_vblend.id, 2 );
435 bind_terrain_textures();
436
437 shader_vblend_uPv( projection );
438 shader_vblend_uMdl( identity_matrix );
439 shader_vblend_uCamera( camera );
440
441 scene_bind( &world.props );
442 scene_draw( &world.props );
443
444 mesh_bind( &world.cars );
445
446 for( int i=0; i<vg_list_size(world.van_man); i++ )
447 {
448 shader_vblend_uMdl( world.van_man[i].transform );
449 mdl_draw_submesh( &world.car_holden );
450 }
451 }
452
453 static void render_terrain( m4x4f projection, v3f camera )
454 {
455 m4x3f identity_matrix;
456 m4x3_identity( identity_matrix );
457
458 shader_terrain_use();
459 shader_terrain_uTexGarbage(0);
460 shader_terrain_uTexGradients(1);
461 shader_link_standard_ub( _shader_terrain.id, 2 );
462 bind_terrain_textures();
463
464 shader_terrain_uPv( projection );
465 shader_terrain_uMdl( identity_matrix );
466 shader_terrain_uCamera( camera );
467
468 scene_bind( &world.geo );
469 scene_draw( &world.geo );
470
471 glDisable(GL_CULL_FACE);
472 scene_bind( &world.foliage );
473 scene_draw( &world.foliage );
474 glEnable(GL_CULL_FACE);
475 }
476
477 static void render_lowerdome( m4x3f camera )
478 {
479 m4x4f projection, full;
480 pipeline_projection( projection, 0.4f, 1000.0f );
481
482 m4x3f inverse;
483 m3x3_transpose( camera, inverse );
484 v3_copy((v3f){0.0f,0.0f,0.0f}, inverse[3]);
485 m4x3_expand( inverse, full );
486 m4x4_mul( projection, full, full );
487
488 m4x3f identity_matrix;
489 m4x3_identity( identity_matrix );
490
491 shader_planeinf_use();
492 shader_planeinf_uMdl(identity_matrix);
493 shader_planeinf_uPv(full);
494 shader_planeinf_uCamera(camera[3]);
495 shader_planeinf_uPlane( (v4f){0.0f,1.0f,0.0f, water_height()} );
496
497 mdl_draw_submesh( &world.dome_lower );
498 }
499
500 static void render_sky(m4x3f camera)
501 {
502 m4x4f projection, full;
503 pipeline_projection( projection, 0.4f, 1000.0f );
504
505 m4x3f inverse;
506 m3x3_transpose( camera, inverse );
507 v3_copy((v3f){0.0f,0.0f,0.0f}, inverse[3]);
508 m4x3_expand( inverse, full );
509 m4x4_mul( projection, full, full );
510
511 m4x3f identity_matrix;
512 m4x3_identity( identity_matrix );
513
514 shader_sky_use();
515 shader_sky_uMdl(identity_matrix);
516 shader_sky_uPv(full);
517 shader_sky_uTexGarbage(0);
518 shader_sky_uTime( vg_time );
519
520 vg_tex2d_bind( &tex_terrain_noise, 0 );
521
522 glDepthMask( GL_FALSE );
523 glDisable( GL_DEPTH_TEST );
524
525 mesh_bind( &world.skydome );
526 mdl_draw_submesh( &world.dome_upper );
527
528 glEnable( GL_DEPTH_TEST );
529 glDepthMask( GL_TRUE );
530 }
531
532 static void render_world( m4x4f projection, m4x3f camera )
533 {
534 render_sky( camera );
535 render_props( projection, camera[3] );
536 render_terrain( projection, camera[3] );
537 }
538
539 static void render_world_depth( m4x4f projection, m4x3f camera )
540 {
541 m4x3f identity_matrix;
542 m4x3_identity( identity_matrix );
543
544 shader_gpos_use();
545 shader_gpos_uCamera( camera[3] );
546 shader_gpos_uPv( projection );
547 shader_gpos_uMdl( identity_matrix );
548
549 scene_bind( &world.geo );
550 scene_draw( &world.geo );
551
552 #if 0
553 glDisable(GL_CULL_FACE);
554 scene_bind( &world.foliage );
555 scene_draw( &world.foliage );
556 glEnable(GL_CULL_FACE);
557 #endif
558
559 scene_bind( &world.props );
560 scene_draw( &world.props );
561 }
562
563 #endif /* WORLD_H */