shader vacuuming
[carveJwlIkooP6JGAAIwe30JlM.git] / world.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "common.h"
6
7 #ifndef WORLD_H
8 #define WORLD_H
9
10 typedef struct world_instance world_instance;
11
12 #include "vg/vg_loader.h"
13
14 #include "network.h"
15 #include "network_msg.h"
16 #include "scene.h"
17 #include "render.h"
18 #include "rigidbody.h"
19 #include "bvh.h"
20 #include "model.h"
21
22 #include "shaders/scene_standard.h"
23 #include "shaders/scene_standard_alphatest.h"
24 #include "shaders/scene_vertex_blend.h"
25 #include "shaders/scene_terrain.h"
26 #include "shaders/scene_depth.h"
27 #include "shaders/scene_position.h"
28
29 #include "shaders/model_sky.h"
30
31 typedef struct teleport_gate teleport_gate;
32
33 enum { k_max_ui_segments = 8 };
34
35 enum { k_max_ui_elements = k_max_ui_segments };
36 enum { k_max_element_verts = 10 };
37 enum { k_max_element_indices = 20 };
38
39 enum { k_route_ui_max_verts = k_max_ui_elements*k_max_element_verts };
40 enum { k_route_ui_max_indices = k_max_ui_elements*k_max_element_indices };
41
42 enum logic_type
43 {
44 k_logic_type_relay = 1,
45 k_logic_type_chance = 2,
46 k_logic_type_achievement = 3
47 };
48
49 enum geo_type
50 {
51 k_geo_type_solid = 0,
52 k_geo_type_nonsolid = 1,
53 k_geo_type_water = 2
54 };
55
56 struct world_instance
57 {
58 /* This is a small flag we use to changelevel.
59 * It will not be cleared until all sounds stop playing
60 */
61
62 /* Fixed items
63 * -------------------------------------------------------
64 */
65
66 char world_name[ 64 ];
67
68 struct
69 {
70 boxf depthbounds;
71 int depth_computed;
72
73 float height;
74 int enabled;
75 v4f plane;
76 }
77 water;
78
79 /* STD140 */
80 struct ub_world_lighting
81 {
82 /* v3f (padded) */
83 v4f g_light_colours[3],
84 g_light_directions[3],
85 g_ambient_colour;
86
87 v4f g_water_plane,
88 g_depth_bounds;
89
90 float g_water_fog;
91 int g_light_count;
92 int g_light_preview;
93 int g_shadow_samples;
94
95 v4f g_point_light_positions[32];
96 v4f g_point_light_colours[32];
97 }
98 ub_lighting;
99 GLuint ubo_lighting;
100 int ubo_bind_point;
101
102 struct framebuffer heightmap;
103
104 /*
105 * Dynamically allocated when world_load is called.
106 *
107 * the following arrays index somewhere into this linear
108 * allocator
109 *
110 * (world_gen.h)
111 * --------------------------------------------------------------------------
112 */
113 /*
114 * Main world .mdl
115 */
116 mdl_context *meta;
117
118 /*
119 * Materials / textures
120 */
121
122 GLuint *textures;
123 u32 texture_count;
124
125 struct world_material
126 {
127 mdl_material info;
128 mdl_submesh sm_geo,
129 sm_no_collide;
130 }
131 * materials;
132 u32 material_count;
133
134 /*
135 * Named safe places to respawn
136 */
137 struct respawn_point
138 {
139 v3f co;
140 v4f q;
141 const char *name;
142 }
143 * spawns;
144 u32 spawn_count;
145
146 /*
147 * Audio player entities
148 */
149 struct world_audio_thing
150 {
151 v3f pos;
152 float volume;
153 u32 flags;
154
155 audio_player player;
156 audio_clip temp_embedded_clip;
157 }
158 * audio_things;
159 u32 audio_things_count;
160
161 /*
162 * Relays
163 */
164 struct logic_relay
165 {
166 v3f pos;
167
168 struct relay_target
169 {
170 u32 sub_id;
171 enum classtype classtype;
172 }
173 targets[4];
174 u32 target_count;
175 }
176 * logic_relays;
177 u32 relay_count;
178
179 /*
180 * Box trigger entities
181 */
182 struct trigger_zone
183 {
184 m4x3f transform, inv_transform;
185
186 struct relay_target target;
187 }
188 * triggers;
189 u32 trigger_count;
190
191 /*
192 * Achievements
193 */
194 struct logic_achievement
195 {
196 v3f pos;
197 const char *achievement_id;
198 u32 achieved;
199 }
200 * logic_achievements;
201 u32 achievement_count;
202
203 /*
204 * Lights
205 */
206 struct world_light
207 {
208 v3f co;
209 v4f colour;
210 }
211 * lights;
212 u32 light_count;
213
214 /*
215 * Routes (world_routes.h)
216 * --------------------------------------------------------------------------
217 */
218 struct route_node
219 {
220 v3f co, right, up, h;
221 u32 next[2];
222
223 u32 special_type, special_id, current_refs, ref_count;
224 u32 route_ids[4]; /* Gates can be linked into up to four routes */
225 }
226 *nodes;
227 u32 node_count;
228
229 struct route
230 {
231 u32 track_id;
232 v4f colour;
233
234 u32 start;
235 mdl_submesh sm;
236
237 int active;
238 float factive;
239
240 double best_lap, latest_pass; /* Session */
241
242 m4x3f scoreboard_transform;
243 }
244 *routes;
245 u32 route_count;
246
247 struct route_gate
248 {
249 struct teleport_gate
250 {
251 v3f co[2];
252 v4f q[2];
253 v2f dims;
254
255 m4x3f to_world, transport;
256 }
257 gate;
258
259 u32 node_id;
260
261 struct route_timing
262 {
263 u32 version; /* Incremented on every teleport */
264 double time;
265 }
266 timing;
267 }
268 *gates;
269 u32 gate_count;
270
271 struct nonlocal_gate
272 {
273 struct teleport_gate gate;
274 mdl_node *node;
275
276 u32 target_map_index, working;
277 }
278 *nonlocal_gates;
279 u32 nonlocalgate_count;
280
281 struct route_collector
282 {
283 struct route_timing timing;
284 }
285 *collectors;
286 u32 collector_count;
287
288
289 /* logic
290 * ----------------------------------------------------
291 */
292
293 /* world geometry */
294 scene *scene_geo,
295 *scene_no_collide,
296 *scene_lines;
297
298 /* spacial mappings */
299 bh_tree *audio_bh,
300 *trigger_bh,
301 *geo_bh;
302
303 /* graphics */
304 glmesh mesh_route_lines;
305 glmesh mesh_geo,
306 mesh_no_collide,
307 mesh_water;
308
309 rigidbody rb_geo; /* todo.. ... */
310 };
311
312 VG_STATIC struct world_global
313 {
314 /*
315 * Allocated as system memory
316 * --------------------------------------------------------------------------
317 */
318 void *generic_heap,
319 *audio_heap; /* sub buffer of the audio buffer */
320
321 /* rendering */
322 glmesh skydome;
323 mdl_submesh dome_upper, dome_lower;
324
325 glmesh mesh_gate_surface;
326
327 double sky_time, sky_rate, sky_target_rate;
328
329 /* gates, TODO: active_gate should also know which instance */
330 u32 active_gate,
331 current_run_version;
332 double time, rewind_from, rewind_to, last_use;
333
334 /* water rendering */
335 struct
336 {
337 struct framebuffer fbreflect, fbdepth;
338 }
339 water;
340
341 /* split flap display */
342 struct
343 {
344 mdl_submesh *sm_module, *sm_card;
345 glmesh mesh_base, mesh_display;
346
347 u32 w, h;
348 float *buffer;
349 }
350 sfd;
351
352 /* timing bars, fixed maximum amount */
353 struct route_ui_bar
354 {
355 GLuint vao, vbo, ebo;
356
357 u32 indices_head;
358 u32 vertex_head;
359
360 struct route_ui_segment
361 {
362 float length;
363 u32 vertex_start, vertex_count,
364 index_start, index_count, notches;
365 }
366 segments[k_max_ui_segments];
367
368 u32 segment_start, segment_count, fade_start, fade_count;
369 double fade_timer_start;
370 float xpos;
371 }
372 ui_bars[16];
373
374 v3f render_gate_pos;
375 int active_route_board;
376 int in_trigger;
377
378 int switching_to_new_world;
379
380 world_instance worlds[4];
381 u32 world_count;
382 u32 active_world;
383 }
384 world_global;
385
386 VG_STATIC world_instance *get_active_world( void )
387 {
388 return &world_global.worlds[ world_global.active_world ];
389 }
390
391 /*
392 * API
393 */
394
395 VG_STATIC
396 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
397
398 VG_STATIC
399 struct world_material *ray_hit_material( world_instance *world, ray_hit *hit );
400
401 VG_STATIC
402 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
403
404 VG_STATIC
405 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
406
407 /*
408 * Submodules
409 */
410
411 #include "world_routes.h"
412 #include "world_sfd.h"
413 #include "world_render.h"
414 #include "world_water.h"
415 #include "world_gen.h"
416 #include "world_gate.h"
417
418 /*
419 * -----------------------------------------------------------------------------
420 * Events
421 * -----------------------------------------------------------------------------
422 */
423
424 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
425 {
426 world_instance *world = get_active_world();
427
428 /*
429 * None of our world audio runs as one shots, they always have a player.
430 * Therefore it is safe to delete clip data after the players are
431 * disconnected
432 */
433 audio_lock();
434 for( int i=0; i<world->audio_things_count; i++ )
435 {
436 struct world_audio_thing *at = &world->audio_things[i];
437
438 if( audio_player_is_playing( &at->player ) )
439 {
440 u32 cflags = audio_player_get_flags( &at->player );
441 audio_player_set_flags( &at->player, cflags | AUDIO_FLAG_KILL );
442 }
443 }
444 audio_unlock();
445
446 return 0;
447 }
448
449 VG_STATIC int world_change_world( int argc, const char *argv[] )
450 {
451 #if 0
452 world_instance *world = get_active_world();
453
454 if( argc == 0 )
455 {
456 vg_info( "%s\n", world.world_name );
457 return 0;
458 }
459 else
460 {
461 vg_info( "Switching world...\n" );
462 strcpy( world.world_name, argv[0] );
463 world.switching_to_new_world = 1;
464 world_stop_sound( 0, NULL );
465 }
466 #endif
467
468 return 0;
469 }
470
471 VG_STATIC void world_init(void)
472 {
473 #if 0
474 vg_var_push( (struct vg_var){
475 .name = "water_enable",
476 .data = &world.water.enabled,
477 .data_type = k_var_dtype_i32,
478 .opt_i32 = { .min=0, .max=1, .clamp=1 },
479 .persistent = 0
480 });
481 #endif
482
483 vg_function_push( (struct vg_cmd)
484 {
485 .name = "world_stop_sound",
486 .function = world_stop_sound
487 });
488
489 vg_function_push( (struct vg_cmd)
490 {
491 .name = "world",
492 .function = world_change_world
493 });
494
495 world_global.sky_rate = 1.0;
496 world_global.sky_target_rate = 1.0;
497
498 shader_scene_standard_register();
499 shader_scene_standard_alphatest_register();
500 shader_scene_vertex_blend_register();
501 shader_scene_terrain_register();
502 shader_scene_depth_register();
503 shader_scene_position_register();
504
505 shader_model_sky_register();
506
507 vg_info( "Loading world resources\n" );
508
509 vg_linear_clear( vg_mem.scratch );
510 mdl_context *msky = mdl_load_full( vg_mem.scratch, "models/rs_skydome.mdl" );
511
512 mdl_node *nupper = mdl_node_from_name( msky, "dome_complete" );
513 world_global.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
514
515 vg_acquire_thread_sync();
516 {
517 mdl_unpack_glmesh( msky, &world_global.skydome );
518 }
519 vg_release_thread_sync();
520
521 /* Other systems */
522 vg_info( "Loading other world systems\n" );
523
524 vg_loader_step( world_render_init, NULL );
525 vg_loader_step( world_sfd_init, NULL );
526 vg_loader_step( world_water_init, NULL );
527 vg_loader_step( world_gates_init, NULL );
528 vg_loader_step( world_routes_init, NULL );
529
530 /* Allocate dynamic world memory arena */
531 u32 max_size = 76*1024*1024;
532 world_global.generic_heap = vg_create_linear_allocator( vg_mem.rtmemory,
533 max_size,
534 VG_MEMORY_SYSTEM );
535 }
536
537 VG_STATIC void world_audio_init(void)
538 {
539 u32 size = vg_linear_remaining( vg_audio.audio_pool )
540 - sizeof(vg_linear_allocator);
541
542 world_global.audio_heap = vg_create_linear_allocator( vg_audio.audio_pool,
543 size,
544 VG_MEMORY_SYSTEM );
545 }
546
547 VG_STATIC void world_trigger_achievement( world_instance *world, u32 uid )
548 {
549 struct logic_achievement *ach = &world->logic_achievements[ uid ];
550
551 if( ach->achieved )
552 return;
553
554 steam_set_achievement( ach->achievement_id );
555 steam_store_achievements();
556
557 ach->achieved = 1;
558 }
559
560 VG_STATIC void world_run_relay( world_instance *world,
561 struct relay_target *rt );
562
563 VG_STATIC void world_trigger_relay( world_instance *world, u32 uid )
564 {
565 struct logic_relay *relay = &world->logic_relays[ uid ];
566
567 for( int i=0; i<relay->target_count; i++ )
568 {
569 world_run_relay( world, &relay->targets[i] );
570 }
571 }
572
573 VG_STATIC void world_trigger_audio( world_instance *world, u32 uid )
574 {
575 struct world_audio_thing *wat = &world->audio_things[ uid ];
576
577 audio_lock();
578 audio_player_playclip( &wat->player,
579 &wat->temp_embedded_clip );
580 audio_unlock();
581 }
582
583 VG_STATIC void world_run_relay( world_instance *world,
584 struct relay_target *rt )
585 {
586 struct entity_instruction
587 {
588 enum classtype classtype;
589 void (*p_trigger)( world_instance *world, u32 uid );
590 }
591 entity_instructions[] =
592 {
593 { k_classtype_logic_achievement, world_trigger_achievement },
594 { k_classtype_logic_relay, world_trigger_relay },
595 { k_classtype_audio, world_trigger_audio }
596 };
597
598 for( int i=0; i<vg_list_size(entity_instructions); i++ )
599 {
600 struct entity_instruction *instr = &entity_instructions[i];
601
602 if( instr->classtype == rt->classtype )
603 {
604 instr->p_trigger( world, rt->sub_id );
605 return;
606 }
607 }
608
609 vg_error( "Don't know how to trigger classtype %d\n", rt->classtype );
610 }
611
612 VG_STATIC void world_update( world_instance *world, v3f pos )
613 {
614 #if 0
615 if( world.switching_to_new_world )
616 {
617 int all_stopped = 1;
618
619 audio_lock();
620 for( int i=0; i<world.audio_things_count; i++ )
621 {
622 struct world_audio_thing *at = &world.audio_things[i];
623
624 if( audio_player_is_playing( &at->player ) )
625 {
626 all_stopped = 0;
627 break;
628 }
629 }
630 audio_unlock();
631
632 if( all_stopped )
633 {
634 world.switching_to_new_world = 0;
635 world_unload();
636 vg_loader_start( world_load );
637 return;
638 }
639 }
640
641 world.sky_time += world.sky_rate * vg.time_delta;
642 world.sky_rate = vg_lerp( world.sky_rate, world.sky_target_rate,
643 vg.time_delta * 5.0 );
644 #endif
645
646 world_routes_update( world );
647 #if 0
648 world_routes_debug();
649 #endif
650
651 if( world->route_count > 0 )
652 {
653 int closest = 0;
654 float min_dist = INFINITY;
655
656 for( int i=0; i<world->route_count; i++ )
657 {
658 float d = v3_dist2( world->routes[i].scoreboard_transform[3], pos );
659
660 if( d < min_dist )
661 {
662 min_dist = d;
663 closest = i;
664 }
665 }
666
667 if( (world_global.active_route_board != closest)
668 || network_scores_updated )
669 {
670 network_scores_updated = 0;
671 world_global.active_route_board = closest;
672
673 struct route *route = &world->routes[closest];
674
675 u32 id = route->track_id;
676
677 if( id != 0xffffffff )
678 {
679 struct netmsg_board *local_board =
680 &scoreboard_client_data.boards[id];
681
682 for( int i=0; i<13; i++ )
683 {
684 sfd_encode( i, &local_board->data[27*i] );
685 }
686 }
687 }
688 }
689
690 int in_trigger = 0;
691 for( int i=0; i<world->trigger_count; i++ )
692 {
693 struct trigger_zone *zone = &world->triggers[i];
694
695 v3f local;
696 m4x3_mulv( zone->inv_transform, pos, local );
697
698 if( (fabsf(local[0]) <= 1.0f) &&
699 (fabsf(local[1]) <= 1.0f) &&
700 (fabsf(local[2]) <= 1.0f) )
701 {
702 in_trigger = 1;
703
704 if( !world_global.in_trigger )
705 {
706 world_run_relay( world, &zone->target );
707 }
708 }
709
710 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
711 { 1.0f, 1.0f, 1.0f}},
712 0xff00ff00 );
713 }
714
715 for( int i=0; i<world->light_count; i++ )
716 {
717 struct world_light *light = &world->lights[i];
718
719 u32 colour = 0xff000000;
720 u8 r = light->colour[0] * 255.0f,
721 g = light->colour[1] * 255.0f,
722 b = light->colour[2] * 255.0f;
723
724 colour |= r;
725 colour |= g << 8;
726 colour |= b << 16;
727
728 vg_line_pt3( light->co, 0.25f, colour );
729 }
730
731 world_global.in_trigger = in_trigger;
732 sfd_update();
733 }
734
735 /*
736 * -----------------------------------------------------------------------------
737 * API implementation
738 * -----------------------------------------------------------------------------
739 */
740
741 VG_STATIC void ray_world_get_tri( world_instance *world,
742 ray_hit *hit, v3f tri[3] )
743 {
744 for( int i=0; i<3; i++ )
745 v3_copy( world->scene_geo->arrvertices[ hit->tri[i] ].co, tri[i] );
746 }
747
748 VG_STATIC int ray_world( world_instance *world,
749 v3f pos, v3f dir, ray_hit *hit )
750 {
751 return scene_raycast( world->scene_geo, world->geo_bh, pos, dir, hit );
752 }
753
754 /*
755 * Cast a sphere from a to b and see what time it hits
756 */
757 VG_STATIC int spherecast_world( world_instance *world,
758 v3f pa, v3f pb, float r, float *t, v3f n )
759 {
760 bh_iter it;
761 bh_iter_init( 0, &it );
762
763 boxf region;
764 box_init_inf( region );
765 box_addpt( region, pa );
766 box_addpt( region, pb );
767
768 v3_add( (v3f){ r, r, r}, region[1], region[1] );
769 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
770
771 v3f dir;
772 v3_sub( pb, pa, dir );
773
774 v3f dir_inv;
775 dir_inv[0] = 1.0f/dir[0];
776 dir_inv[1] = 1.0f/dir[1];
777 dir_inv[2] = 1.0f/dir[2];
778
779 int hit = -1;
780 float min_t = 1.0f;
781
782 int idx;
783 while( bh_next( world->geo_bh, &it, region, &idx ) )
784 {
785 u32 *ptri = &world->scene_geo->arrindices[ idx*3 ];
786 v3f tri[3];
787
788 boxf box;
789 box_init_inf( box );
790
791 for( int j=0; j<3; j++ )
792 {
793 v3_copy( world->scene_geo->arrvertices[ptri[j]].co, tri[j] );
794 box_addpt( box, tri[j] );
795 }
796
797 v3_add( (v3f){ r, r, r}, box[1], box[1] );
798 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
799
800 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
801 continue;
802
803 float t;
804 v3f n1;
805 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
806 {
807 if( t < min_t )
808 {
809 min_t = t;
810 hit = idx;
811 v3_copy( n1, n );
812 }
813 }
814 }
815
816 *t = min_t;
817 return hit;
818 }
819
820 VG_STATIC
821 struct world_material *world_tri_index_material( world_instance *world,
822 u32 index )
823 {
824 for( int i=1; i<world->material_count; i++ )
825 {
826 struct world_material *mat = &world->materials[i];
827
828 if( (index >= mat->sm_geo.vertex_start) &&
829 (index < mat->sm_geo.vertex_start+mat->sm_geo.vertex_count ) )
830 {
831 return mat;
832 }
833 }
834
835 /* error material */
836 return &world->materials[0];
837 }
838
839 VG_STATIC struct world_material *world_contact_material( world_instance *world,
840 rb_ct *ct )
841 {
842 return world_tri_index_material( world, ct->element_id );
843 }
844
845 VG_STATIC struct world_material *ray_hit_material( world_instance *world,
846 ray_hit *hit )
847 {
848 return world_tri_index_material( world, hit->tri[0] );
849 }
850
851 #endif /* WORLD_H */