c6d07144cbb39215d08cc2ef844d2c9052762912
[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
18 #include "traffic.h" /*TODO: -> world_traffic.h */
19 #include "world_routes.h"
20 #include "world_sfd.h"
21
22 #include "shaders/terrain.h"
23 #include "shaders/sky.h"
24 #include "shaders/planeinf.h"
25 #include "shaders/standard.h"
26 #include "shaders/vblend.h"
27 #include "shaders/gpos.h"
28 #include "shaders/fscolour.h"
29 #include "shaders/alphatest.h"
30
31 static struct gworld
32 {
33 /* gameplay */
34 struct respawn_point
35 {
36 v3f co;
37 v4f q;
38 char name[32];
39 }
40 spawns[32];
41 u32 spawn_count;
42
43 struct subworld_routes routes;
44 struct subworld_sfd sfd;
45
46 /* ...
47 struct subworld_spawns system_spawns;
48 struct subworld_physics system_physics;
49 */
50
51 /* Paths */
52 traffic_node traffic[128];
53 u32 traffic_count;
54
55 #if 0
56 traffic_driver van_man[6];
57 #endif
58
59 /* Physics */
60
61 /* Rendering & geometry */
62 scene geo, foliage;
63 rigidbody rb_geo;
64
65 mdl_submesh sm_geo_std_oob, sm_geo_std, sm_geo_vb,
66 sm_foliage_main, sm_foliage_alphatest;
67
68 glmesh skybox, skydome;
69 mdl_submesh dome_upper, dome_lower;
70
71 glmesh cars;
72 mdl_submesh car_holden;
73
74 rigidbody mr_ball;
75
76 /* Load time */
77
78 struct instance_cache
79 {
80 mdl_header *mdl;
81 u32 pstr_file;
82 }
83 * instance_cache;
84 u32 instance_cache_count,
85 instance_cache_cap;
86 }
87 world;
88
89 static struct subworld_routes *subworld_routes(void) { return &world.routes; }
90 static struct subworld_sfd *subworld_sfd(void) { return &world.sfd; }
91
92
93 vg_tex2d tex_terrain_colours = { .path = "textures/gradients.qoi",
94 .flags = VG_TEXTURE_CLAMP|VG_TEXTURE_NEAREST };
95
96 vg_tex2d tex_terrain_noise = { .path = "textures/garbage.qoi",
97 .flags = VG_TEXTURE_NEAREST };
98
99 vg_tex2d tex_alphatest = { .path = "textures/alphatest.qoi",
100 .flags = VG_TEXTURE_NEAREST };
101
102 static void ray_world_get_tri( ray_hit *hit, v3f tri[3] )
103 {
104 for( int i=0; i<3; i++ )
105 v3_copy( world.geo.verts[ hit->tri[i] ].co, tri[i] );
106 }
107
108 static int ray_world( v3f pos, v3f dir, ray_hit *hit )
109 {
110 return scene_raycast( &world.geo, pos, dir, hit );
111 }
112
113 static int ray_hit_is_ramp( ray_hit *hit )
114 {
115 return hit->tri[0] > world.sm_geo_std_oob.vertex_count;
116 }
117
118 static void world_register(void)
119 {
120 shader_terrain_register();
121 shader_sky_register();
122 shader_planeinf_register();
123 shader_gpos_register();
124 shader_fscolour_register();
125 shader_alphatest_register();
126
127 world_routes_register();
128 world_sfd_register();
129 }
130
131 static void world_free(void)
132 {
133 /* TODO.. */
134
135 world_sfd_free();
136 }
137
138 static void render_world_depth( m4x4f projection, m4x3f camera );
139
140 static void add_all_if_material( m4x3f transform, scene *pscene,
141 mdl_header *mdl, u32 id )
142 {
143 for( int i=0; i<mdl->node_count; i++ )
144 {
145 mdl_node *pnode = mdl_node_from_id( mdl, i );
146
147 for( int j=0; j<pnode->submesh_count; j++ )
148 {
149 mdl_submesh *sm = mdl_node_submesh( mdl, pnode, j );
150
151 if( sm->material_id == id )
152 {
153 m4x3f transform2;
154 mdl_node_transform( pnode, transform2 );
155 m4x3_mul( transform, transform2, transform2 );
156
157 scene_add_submesh( pscene, mdl, sm, transform2 );
158 }
159 }
160
161 if( pnode->classtype == k_classtype_instance )
162 {
163 if( pnode->sub_uid )
164 {
165 u32 instance_id = pnode->sub_uid -1;
166 struct instance_cache *cache = &world.instance_cache[instance_id];
167 mdl_header *mdl2 = cache->mdl;
168
169 m4x3f transform2;
170 mdl_node_transform( pnode, transform2 );
171 m4x3_mul( transform, transform2, transform2 );
172
173 add_all_if_material( transform2, pscene, mdl2, id );
174 }
175 }
176 }
177 }
178
179 static void world_apply_procedural_foliage(void)
180 {
181 mdl_header *mfoliage = mdl_load("models/rs_foliage.mdl");
182
183 v3f volume;
184 v3_sub( world.geo.bbx[1], world.geo.bbx[0], volume );
185 volume[1] = 1.0f;
186
187 m4x3f transform;
188 mdl_node *mblob = mdl_node_from_name( mfoliage, "blob" );
189 mdl_submesh *sm_blob = mdl_node_submesh( mfoliage, mblob, 0 );
190
191 for( int i=0;i<100000;i++ )
192 {
193 v3f pos;
194 v3_mul( volume, (v3f){ vg_randf(), 1000.0f, vg_randf() }, pos );
195 pos[1] = 1000.0f;
196 v3_add( pos, world.geo.bbx[0], pos );
197
198 ray_hit hit;
199 hit.dist = INFINITY;
200
201 if( ray_world( pos, (v3f){0.0f,-1.0f,0.0f}, &hit ))
202 {
203 if( hit.normal[1] > 0.8f && !ray_hit_is_ramp(&hit) &&
204 hit.pos[1] > water_height()+10.0f )
205 {
206 v4f qsurface, qrandom;
207 v3f axis;
208
209 v3_cross( (v3f){0.0f,1.0f,0.0f}, hit.normal, axis );
210
211 float angle = v3_dot(hit.normal,(v3f){0.0f,1.0f,0.0f});
212 q_axis_angle( qsurface, axis, angle );
213 q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf()*VG_TAUf );
214 q_mul( qsurface, qrandom, qsurface );
215 q_m3x3( qsurface, transform );
216
217 v3_copy( hit.pos, transform[3] );
218 scene_add_submesh( &world.foliage, mfoliage, sm_blob, transform);
219 }
220 }
221 }
222 free( mfoliage );
223 }
224
225 static void world_load(void)
226 {
227 mdl_header *mworld = mdl_load( "models/mp_dev.mdl" );
228
229 world.spawn_count = 0;
230 world.traffic_count = 0;
231 world.instance_cache = NULL;
232
233 /*
234 * Process entities
235 */
236 for( int i=0; i<mworld->node_count; i++ )
237 {
238 mdl_node *pnode = mdl_node_from_id( mworld, i );
239
240 if( pnode->classtype == k_classtype_none )
241 {}
242 else if( pnode->classtype == k_classtype_spawn )
243 {
244 struct respawn_point *rp = &world.spawns[ world.spawn_count ++ ];
245
246 v3_copy( pnode->co, rp->co );
247 v4_copy( pnode->q, rp->q );
248 strcpy( rp->name, mdl_pstr( mworld, pnode->pstr_name ) );
249 }
250 else if( pnode->classtype == k_classtype_water )
251 {
252 if( wrender.enabled )
253 {
254 vg_warn( "Multiple water surfaces in level! ('%s')\n",
255 mdl_pstr( mworld, pnode->pstr_name ));
256 continue;
257 }
258
259 mdl_submesh *sm = mdl_node_submesh( mworld, pnode, 0 );
260
261 if( sm )
262 {
263 glmesh surf;
264 mdl_unpack_submesh( mworld, &surf, sm );
265 water_init();
266 water_set_surface( &surf, pnode->co[1] );
267 }
268 }
269 else if( pnode->classtype == k_classtype_car_path )
270 {
271 struct classtype_car_path *p = mdl_get_entdata( mworld, pnode );
272 traffic_node *tn = &world.traffic[ world.traffic_count ];
273 tn->mn_next = NULL;
274 tn->mn_next1 = NULL;
275
276 if( p->target ) tn->mn_next = mdl_node_from_id( mworld, p->target );
277 if( p->target1 ) tn->mn_next1 = mdl_node_from_id( mworld, p->target1 );
278
279 m4x3f transform;
280 mdl_node_transform( pnode, transform );
281 m3x3_mulv( transform, (v3f){1.0f,0.0f,0.0f}, tn->h );
282 v3_copy( transform[3], tn->co );
283
284 pnode->sub_uid = world.traffic_count ++;
285 }
286 else if( pnode->classtype == k_classtype_instance )
287 {
288 struct classtype_instance *inst = mdl_get_entdata( mworld, pnode );
289 pnode->sub_uid = 0;
290
291 int cached = 0;
292 for( int i=0; i<world.instance_cache_count; i++ )
293 {
294 struct instance_cache *cache = &world.instance_cache[i];
295 if( inst->pstr_file == cache->pstr_file )
296 {
297 cached = 1;
298 pnode->sub_uid = i+1;
299 break;
300 }
301 }
302
303 if( !cached )
304 {
305 world.instance_cache = buffer_reserve(
306 world.instance_cache, world.instance_cache_count,
307 &world.instance_cache_cap, 1,
308 sizeof(struct instance_cache) );
309
310 struct instance_cache *cache =
311 &world.instance_cache[world.instance_cache_count];
312
313 const char *filename = mdl_pstr(mworld, inst->pstr_file);
314
315 cache->pstr_file = inst->pstr_file;
316 cache->mdl = mdl_load( filename );
317
318 if( cache->mdl )
319 {
320 world.instance_cache_count ++;
321 pnode->sub_uid = world.instance_cache_count;
322 mdl_link_materials( mworld, cache->mdl );
323 vg_success( "Cached %s\n", filename );
324 }
325 else
326 {
327 vg_warn( "Failed to cache %s\n", filename );
328 }
329 }
330 }
331 }
332
333 world.instance_cache = buffer_fix( world.instance_cache,
334 world.instance_cache_count,
335 &world.instance_cache_cap,
336 sizeof( struct instance_cache ) );
337
338 #if 0
339 traffic_finalize( world.traffic, world.traffic_count );
340 for( int i=0; i<vg_list_size(world.van_man); i++ )
341 world.van_man[i].current =&world.traffic[vg_randint(world.traffic_count)];
342 #endif
343
344 /*
345 * Compile meshes into the world scenes
346 */
347 scene_init( &world.geo );
348
349 u32 mat_surf = 0,
350 mat_surf_oob = 0,
351 mat_vertex_blend = 0,
352 mat_alphatest = 0;
353
354 for( int i=1; i<mworld->material_count; i++ )
355 {
356 mdl_material *mat = mdl_material_from_id( mworld, i );
357 const char *mat_name = mdl_pstr( mworld, mat->pstr_name );
358
359 if( !strcmp( "surf", mat_name ))
360 mat_surf = i;
361 else if( !strcmp( "surf_oob", mat_name ))
362 mat_surf_oob = i;
363 else if( !strcmp( "vertex_blend", mat_name ))
364 mat_vertex_blend = i;
365 else if( !strcmp( "alphatest", mat_name ))
366 mat_alphatest = i;
367 }
368
369 m4x3f midentity;
370 m4x3_identity( midentity );
371
372 if( mat_surf_oob )
373 add_all_if_material( midentity, &world.geo, mworld, mat_surf_oob );
374 else
375 vg_warn( "No OOB surface\n" );
376 scene_copy_slice( &world.geo, &world.sm_geo_std_oob );
377
378 if( mat_surf )
379 add_all_if_material( midentity, &world.geo, mworld, mat_surf );
380 scene_copy_slice( &world.geo, &world.sm_geo_std );
381
382 if( mat_vertex_blend )
383 add_all_if_material( midentity, &world.geo, mworld, mat_vertex_blend );
384 scene_copy_slice( &world.geo, &world.sm_geo_vb );
385
386 scene_upload( &world.geo );
387 scene_bh_create( &world.geo );
388
389
390 /* Foliage /nocollide layer.
391 * TODO: Probably should have material traits for this
392 */
393 scene_init( &world.foliage );
394
395 world_apply_procedural_foliage();
396 scene_copy_slice( &world.foliage, &world.sm_foliage_main );
397
398 add_all_if_material( midentity, &world.foliage, mworld, mat_alphatest );
399 scene_copy_slice( &world.foliage, &world.sm_foliage_alphatest );
400
401 scene_upload( &world.foliage );
402 world_routes_loadfrom( mworld );
403
404 for( int i=0; i<world.instance_cache_count; i++ )
405 free( world.instance_cache[i].mdl );
406
407 free( world.instance_cache );
408 free( mworld );
409
410 /*
411 * Rendering the depth map
412 */
413 m4x4f ortho;
414 m4x3f camera;
415
416 v3f extent;
417 v3_sub( world.geo.bbx[1], world.geo.bbx[0], extent );
418
419 float fl = world.geo.bbx[0][0],
420 fr = world.geo.bbx[1][0],
421 fb = world.geo.bbx[0][2],
422 ft = world.geo.bbx[1][2],
423 rl = 1.0f / (fr-fl),
424 tb = 1.0f / (ft-fb);
425
426 m4x4_zero( ortho );
427 ortho[0][0] = 2.0f * rl;
428 ortho[2][1] = 2.0f * tb;
429 ortho[3][0] = (fr + fl) * -rl;
430 ortho[3][1] = (ft + fb) * -tb;
431 ortho[3][3] = 1.0f;
432 m4x3_identity( camera );
433
434 glViewport( 0, 0, 1024, 1024 );
435 glDisable(GL_DEPTH_TEST);
436 glBindFramebuffer( GL_FRAMEBUFFER, gpipeline.fb_depthmap );
437 shader_fscolour_use();
438 shader_fscolour_uColour( (v4f){-9999.0f,-9999.0f,-9999.0f,-9999.0f} );
439 render_fsquad();
440
441 glEnable(GL_BLEND);
442 glBlendFunc(GL_ONE, GL_ONE);
443 glBlendEquation(GL_MAX);
444 render_world_depth( ortho, camera );
445 glDisable(GL_BLEND);
446 glEnable(GL_DEPTH_TEST);
447
448 /*
449 * TODO: World settings entity
450 */
451 struct ub_world_lighting *winfo = &gpipeline.ub_world_lighting;
452 v4_copy( wrender.plane, winfo->g_water_plane );
453
454 v4f bounds;
455 bounds[0] = world.geo.bbx[0][0];
456 bounds[1] = world.geo.bbx[0][2];
457 bounds[2] = 1.0f/ (world.geo.bbx[1][0]-world.geo.bbx[0][0]);
458 bounds[3] = 1.0f/ (world.geo.bbx[1][2]-world.geo.bbx[0][2]);
459 v4_copy( bounds, winfo->g_depth_bounds );
460
461 winfo->g_water_fog = 0.04f;
462 render_update_lighting_ub();
463
464
465 world.mr_ball.type = k_rb_shape_sphere;
466 world.mr_ball.inf.sphere.radius = 2.0f;
467 v3_copy( (v3f){ 0.0f, 110.0f, 0.0f }, world.mr_ball.co );
468
469 q_identity(world.mr_ball.q);
470 rb_init( &world.mr_ball );
471
472 /*
473 * Setup scene collider
474 */
475 v3_zero( world.rb_geo.co );
476 q_identity( world.rb_geo.q );
477
478 world.rb_geo.type = k_rb_shape_scene;
479 world.rb_geo.inf.scene.pscene = &world.geo;
480 world.rb_geo.is_world = 1;
481 rb_init( &world.rb_geo );
482 }
483
484 static void world_init(void)
485 {
486 vg_tex2d_init( (vg_tex2d *[]){ &tex_terrain_colours,
487 &tex_terrain_noise,
488 &tex_alphatest }, 3 );
489
490 mdl_header *mcars = mdl_load( "models/rs_cars.mdl" );
491 mdl_unpack_glmesh( mcars, &world.cars );
492 mdl_node *nholden = mdl_node_from_name( mcars, "holden" );
493 world.car_holden = *mdl_node_submesh( mcars, nholden, 0 );
494 free(mcars);
495
496
497 mdl_header *msky = mdl_load("models/rs_skydome.mdl");
498 mdl_unpack_glmesh( msky, &world.skydome );
499
500 mdl_node *nlower = mdl_node_from_name( msky, "dome_lower" ),
501 *nupper = mdl_node_from_name( msky, "dome_upper" );
502
503 world.dome_lower = *mdl_node_submesh( msky, nlower, 0 );
504 world.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
505 free(msky);
506
507
508 /* Other systems */
509 world_sfd_init();
510 }
511
512 static void world_update(void)
513 {
514 world_routes_update();
515 world_routes_debug();
516 sfd_update( &world.sfd.tester );
517
518 #if 0
519 rb_solver_reset();
520 rb_build_manifold_terrain_sphere( &world.mr_ball );
521
522 for( int i=0; i<5; i++ )
523 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
524
525 rb_iter( &world.mr_ball );
526 rb_update_transform( &world.mr_ball );
527 rb_debug( &world.mr_ball, 0 );
528
529 for( int i=0; i<vg_list_size(world.van_man); i++ )
530 {
531 traffic_drive( &world.van_man[i] );
532 traffic_visualize_car( &world.van_man[i] );
533 }
534 #endif
535 }
536
537 /*
538 * Rendering
539 */
540
541 static void bind_terrain_textures(void)
542 {
543 vg_tex2d_bind( &tex_terrain_noise, 0 );
544 vg_tex2d_bind( &tex_terrain_colours, 1 );
545 }
546
547 static void render_world_vb( m4x4f projection, v3f camera )
548 {
549 m4x3f identity_matrix;
550 m4x3_identity( identity_matrix );
551
552 shader_vblend_use();
553 shader_vblend_uTexGarbage(0);
554 shader_vblend_uTexGradients(1);
555 shader_link_standard_ub( _shader_vblend.id, 2 );
556 bind_terrain_textures();
557
558 shader_vblend_uPv( projection );
559 shader_vblend_uMdl( identity_matrix );
560 shader_vblend_uCamera( camera );
561
562 scene_bind( &world.geo );
563 mdl_draw_submesh( &world.sm_geo_vb );
564
565 mesh_bind( &world.cars );
566
567 #if 0
568 for( int i=0; i<vg_list_size(world.van_man); i++ )
569 {
570 shader_vblend_uMdl( world.van_man[i].transform );
571 mdl_draw_submesh( &world.car_holden );
572 }
573 #endif
574 }
575
576 static void render_world_alphatest( m4x4f projection, v3f camera )
577 {
578 m4x3f identity_matrix;
579 m4x3_identity( identity_matrix );
580
581 shader_alphatest_use();
582 shader_alphatest_uTexGarbage(0);
583 shader_alphatest_uTexMain(1);
584 shader_link_standard_ub( _shader_alphatest.id, 2 );
585
586 vg_tex2d_bind( &tex_terrain_noise, 0 );
587 vg_tex2d_bind( &tex_alphatest, 1 );
588
589 shader_alphatest_uPv( projection );
590 shader_alphatest_uMdl( identity_matrix );
591 shader_alphatest_uCamera( camera );
592
593 glDisable(GL_CULL_FACE);
594 scene_bind( &world.foliage );
595 mdl_draw_submesh( &world.sm_foliage_alphatest );
596 glEnable(GL_CULL_FACE);
597 }
598
599 static void render_terrain( m4x4f projection, v3f camera )
600 {
601 m4x3f identity_matrix;
602 m4x3_identity( identity_matrix );
603
604 shader_terrain_use();
605 shader_terrain_uTexGarbage(0);
606 shader_terrain_uTexGradients(1);
607 shader_link_standard_ub( _shader_terrain.id, 2 );
608 bind_terrain_textures();
609
610 shader_terrain_uPv( projection );
611 shader_terrain_uMdl( identity_matrix );
612 shader_terrain_uCamera( camera );
613
614 scene_bind( &world.geo );
615 mdl_draw_submesh( &world.sm_geo_std_oob );
616 mdl_draw_submesh( &world.sm_geo_std );
617
618 /* TODO: Dont draw in reflection */
619 glDisable(GL_CULL_FACE);
620 scene_bind( &world.foliage );
621 mdl_draw_submesh( &world.sm_foliage_main );
622 glEnable(GL_CULL_FACE);
623 }
624
625 static void render_lowerdome( m4x3f camera )
626 {
627 m4x4f projection, full;
628 pipeline_projection( projection, 0.4f, 1000.0f );
629
630 m4x3f inverse;
631 m3x3_transpose( camera, inverse );
632 v3_copy((v3f){0.0f,0.0f,0.0f}, inverse[3]);
633 m4x3_expand( inverse, full );
634 m4x4_mul( projection, full, full );
635
636 m4x3f identity_matrix;
637 m4x3_identity( identity_matrix );
638
639 shader_planeinf_use();
640 shader_planeinf_uMdl(identity_matrix);
641 shader_planeinf_uPv(full);
642 shader_planeinf_uCamera(camera[3]);
643 shader_planeinf_uPlane( (v4f){0.0f,1.0f,0.0f, water_height()} );
644
645 mdl_draw_submesh( &world.dome_lower );
646 }
647
648 static void render_sky(m4x3f camera)
649 {
650 m4x4f projection, full;
651 pipeline_projection( projection, 0.4f, 1000.0f );
652
653 m4x3f inverse;
654 m3x3_transpose( camera, inverse );
655 v3_copy((v3f){0.0f,0.0f,0.0f}, inverse[3]);
656 m4x3_expand( inverse, full );
657 m4x4_mul( projection, full, full );
658
659 m4x3f identity_matrix;
660 m4x3_identity( identity_matrix );
661
662 shader_sky_use();
663 shader_sky_uMdl(identity_matrix);
664 shader_sky_uPv(full);
665 shader_sky_uTexGarbage(0);
666 shader_sky_uTime( vg_time );
667
668 vg_tex2d_bind( &tex_terrain_noise, 0 );
669
670 glDepthMask( GL_FALSE );
671 glDisable( GL_DEPTH_TEST );
672
673 mesh_bind( &world.skydome );
674 mdl_draw_submesh( &world.dome_upper );
675
676 glEnable( GL_DEPTH_TEST );
677 glDepthMask( GL_TRUE );
678 }
679
680 static void render_world_gates( m4x4f projection, m4x3f camera )
681 {
682 int count = 0;
683 for( int i=0; i<world.routes.gate_count; i++ )
684 {
685 struct route_gate *rg = &world.routes.gates[i];
686 count += render_gate( &rg->gate, camera );
687
688 if( count == 2 )
689 return;
690 }
691 }
692
693 static void render_world( m4x4f projection, m4x3f camera )
694 {
695 render_sky( camera );
696 render_world_routes( projection, camera[3] );
697 render_world_vb( projection, camera[3] );
698 render_world_alphatest( projection, camera[3] );
699 render_terrain( projection, camera[3] );
700
701 m4x3f identity_matrix;
702 m4x3_identity( identity_matrix );
703 identity_matrix[3][1] = 125.0f;
704
705 v4f t;
706 q_axis_angle( t, (v3f){0.0f,1.0f,0.0f}, 2.3f );
707 q_m3x3( t, identity_matrix );
708
709 sfd_render( &world.sfd.tester, projection, camera[3], identity_matrix );
710 }
711
712 static void render_world_depth( m4x4f projection, m4x3f camera )
713 {
714 m4x3f identity_matrix;
715 m4x3_identity( identity_matrix );
716
717 shader_gpos_use();
718 shader_gpos_uCamera( camera[3] );
719 shader_gpos_uPv( projection );
720 shader_gpos_uMdl( identity_matrix );
721
722 scene_bind( &world.geo );
723 scene_draw( &world.geo );
724
725 #if 0
726 glDisable(GL_CULL_FACE);
727 scene_bind( &world.foliage );
728 scene_draw( &world.foliage );
729 glEnable(GL_CULL_FACE);
730 #endif
731 }
732
733 #endif /* WORLD_H */