sfd
[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 enum { k_max_ui_segments = 8 };
32
33 enum { k_max_ui_elements = k_max_ui_segments };
34 enum { k_max_element_verts = 10 };
35 enum { k_max_element_indices = 20 };
36
37 enum { k_route_ui_max_verts = k_max_ui_elements*k_max_element_verts };
38 enum { k_route_ui_max_indices = k_max_ui_elements*k_max_element_indices };
39
40 enum logic_type
41 {
42 k_logic_type_relay = 1,
43 k_logic_type_chance = 2,
44 k_logic_type_achievement = 3
45 };
46
47 enum geo_type
48 {
49 k_geo_type_solid = 0,
50 k_geo_type_nonsolid = 1,
51 k_geo_type_water = 2
52 };
53
54 static const float k_light_cube_size = 8.0f;
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 v4f g_cube_min,
83 g_cube_inv_range;
84
85 v4f g_water_plane,
86 g_depth_bounds;
87
88 v4f g_daysky_colour;
89 v4f g_nightsky_colour;
90 v4f g_sunset_colour;
91 v4f g_ambient_colour;
92 v4f g_sunset_ambient;
93 v4f g_sun_colour;
94 v4f g_sun_dir;
95
96 float g_water_fog;
97 float g_time;
98 float g_realtime;
99 float g_shadow_length;
100 float g_shadow_spread;
101
102 float g_time_of_day;
103 float g_day_phase;
104 float g_sunset_phase;
105
106 int g_light_preview;
107 int g_shadow_samples;
108
109 int g_debug_indices;
110 int g_debug_complexity;
111 }
112 ub_lighting;
113 GLuint ubo_lighting;
114 int ubo_bind_point;
115
116 GLuint tbo_light_entities,
117 tex_light_entities,
118 tex_light_cubes;
119
120 v3i light_cubes;
121
122 struct framebuffer heightmap;
123
124 /*
125 * Dynamically allocated when world_load is called.
126 *
127 * the following arrays index somewhere into this linear
128 * allocator
129 *
130 * (world_gen.h)
131 * --------------------------------------------------------------------------
132 */
133 /*
134 * Main world .mdl
135 */
136 mdl_context meta;
137
138 GLuint *textures;
139 u32 texture_count;
140
141 struct world_surface
142 {
143 mdl_material info;
144 mdl_submesh sm_geo,
145 sm_no_collide;
146 }
147 * surfaces;
148 u32 surface_count;
149
150 mdl_array_ptr ent_spawn,
151 ent_gate,
152 ent_light,
153 ent_route_node,
154 ent_path_index,
155 ent_checkpoint,
156 ent_route;
157
158 #if 0
159 /*
160 * Named safe places to respawn
161 */
162 struct respawn_point
163 {
164 v3f co;
165 v4f q;
166 const char *name;
167 }
168 * spawns;
169 u32 spawn_count;
170
171 /*
172 * Audio player entities
173 */
174 struct world_audio_thing
175 {
176 v3f pos;
177 float volume, range;
178 u32 flags;
179 audio_clip temp_embedded_clip;
180 }
181 * audio_things;
182 u32 audio_things_count;
183
184 struct soundscape
185 {
186 /* locking */
187 audio_channel *channels[4];
188
189 /* accessable without locking */
190 v3f spawn_position;
191
192 u32 usage_count;
193 u32 max_instances;
194 u32 allow_transitions;
195 float transition_duration;
196 const char *label;
197 }
198 * soundscapes;
199 u32 soundscape_count;
200
201 /*
202 * Box volume entities
203 */
204 struct world_volume
205 {
206 m4x3f transform, inv_transform;
207 mdl_node *node;
208 }
209 * volumes;
210 u32 volume_count;
211
212 /*
213 * Lights
214 */
215 struct world_light
216 {
217 mdl_node *node;
218 struct classtype_world_light *inf;
219 m4x3f inverse_world;
220 v2f angle_sin_cos;
221 }
222 * lights;
223 u32 light_count;
224
225 /*
226 * Routes (world_routes.h)
227 * --------------------------------------------------------------------------
228 */
229
230 struct route_node
231 {
232 v3f co, right, up, h;
233 u32 next[2];
234
235 u32 special_type, special_id, current_refs, ref_count;
236 u32 route_ids[4]; /* Gates can be linked into up to four routes */
237 }
238 *nodes;
239 u32 node_count;
240
241 struct route
242 {
243 u32 track_id;
244 v4f colour;
245
246 u32 start;
247 mdl_submesh sm;
248
249 int active;
250 float factive;
251
252 double best_lap, latest_pass; /* Session */
253
254 m4x3f scoreboard_transform;
255 }
256 *routes;
257 u32 route_count;
258
259 struct route_gate
260 {
261 struct teleport_gate
262 {
263 v3f co[2];
264 v4f q[2];
265 v2f dims;
266
267 m4x3f to_world, transport;
268 }
269 gate;
270
271 u32 node_id;
272
273 struct route_timing
274 {
275 u32 version; /* Incremented on every teleport */
276 double time;
277 }
278 timing;
279 }
280 *gates;
281 u32 gate_count;
282
283 struct nonlocal_gate
284 {
285 struct teleport_gate gate;
286 mdl_node *node;
287
288 u32 target_map_index, working;
289 }
290 *nonlocal_gates;
291 u32 nonlocalgate_count;
292
293 struct route_collector
294 {
295 struct route_timing timing;
296 }
297 *collectors;
298 u32 collector_count;
299 #endif
300
301
302 /* logic
303 * ----------------------------------------------------
304 */
305
306 /* world geometry */
307 scene *scene_geo,
308 *scene_no_collide,
309 *scene_lines;
310
311 /* spacial mappings */
312 bh_tree *audio_bh,
313 *volume_bh,
314 *geo_bh;
315
316 /* graphics */
317 glmesh mesh_route_lines;
318 glmesh mesh_geo,
319 mesh_no_collide,
320 mesh_water;
321
322 rigidbody rb_geo; /* todo.. ... */
323 };
324
325 VG_STATIC struct world_global
326 {
327 /*
328 * Allocated as system memory
329 * --------------------------------------------------------------------------
330 */
331 void *generic_heap;
332
333 /* rendering */
334 glmesh skydome;
335 glmesh mesh_gate;
336 mdl_submesh sm_gate_surface,
337 sm_gate_marker[4];
338
339 double sky_time, sky_rate, sky_target_rate;
340
341 u32 current_run_version;
342 double time, rewind_from, rewind_to, last_use;
343
344 /* water rendering */
345 struct
346 {
347 struct framebuffer fbreflect, fbdepth;
348 }
349 water;
350
351 /* split flap display */
352 struct
353 {
354 glmesh mesh_base, mesh_display;
355 mdl_submesh sm_base;
356 u32 active_route_board;
357
358 u32 w, h;
359 float *buffer;
360 }
361 sfd;
362
363 v3f render_gate_pos;
364 int in_volume;
365
366 int switching_to_new_world;
367
368 world_instance worlds[4];
369 u32 world_count;
370 u32 active_world;
371 }
372 world_global;
373
374 VG_STATIC world_instance *get_active_world( void )
375 {
376 return &world_global.worlds[ world_global.active_world ];
377 }
378
379 /*
380 * API
381 */
382
383 VG_STATIC
384 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
385
386 VG_STATIC
387 struct world_surface *ray_hit_surface( world_instance *world, ray_hit *hit );
388
389 VG_STATIC
390 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
391
392 VG_STATIC
393 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
394
395 /*
396 * Submodules
397 */
398
399 #include "world_routes.h"
400 #include "world_sfd.h"
401 #include "world_render.h"
402 #include "world_water.h"
403 #include "world_volumes.h"
404 #include "world_gen.h"
405 #include "world_gate.h"
406
407 /*
408 * -----------------------------------------------------------------------------
409 * Events
410 * -----------------------------------------------------------------------------
411 */
412
413 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
414 {
415 world_instance *world = get_active_world();
416 return 0;
417 }
418
419 VG_STATIC void world_init(void)
420 {
421 world_global.sky_rate = 1.0;
422 world_global.sky_target_rate = 1.0;
423
424 shader_scene_standard_register();
425 shader_scene_standard_alphatest_register();
426 shader_scene_vertex_blend_register();
427 shader_scene_terrain_register();
428 shader_scene_depth_register();
429 shader_scene_position_register();
430
431 shader_model_sky_register();
432
433 vg_info( "Loading world resources\n" );
434
435 vg_linear_clear( vg_mem.scratch );
436
437 mdl_context msky;
438 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
439 mdl_load_metadata_block( &msky, vg_mem.scratch );
440 mdl_load_mesh_block( &msky, vg_mem.scratch );
441 mdl_close( &msky );
442
443 vg_acquire_thread_sync();
444 {
445 mdl_unpack_glmesh( &msky, &world_global.skydome );
446 }
447 vg_release_thread_sync();
448
449 /* Other systems */
450 vg_info( "Loading other world systems\n" );
451
452 vg_loader_step( world_render_init, NULL );
453 vg_loader_step( world_sfd_init, NULL );
454 vg_loader_step( world_water_init, NULL );
455 vg_loader_step( world_gates_init, NULL );
456 vg_loader_step( world_routes_init, NULL );
457
458 /* Allocate dynamic world memory arena */
459 u32 max_size = 76*1024*1024;
460 world_global.generic_heap = vg_create_linear_allocator( vg_mem.rtmemory,
461 max_size,
462 VG_MEMORY_SYSTEM );
463 }
464
465 VG_STATIC void world_update( world_instance *world, v3f pos )
466 {
467 /* TEMP!!!!!! */
468 static double g_time = 0.0;
469 g_time += vg.time_delta * (1.0/(k_day_length*60.0));
470
471
472 struct ub_world_lighting *state = &world->ub_lighting;
473
474 state->g_time = g_time;
475 state->g_realtime = vg.time;
476 state->g_debug_indices = k_debug_light_indices;
477 state->g_light_preview = k_light_preview;
478 state->g_debug_complexity = k_debug_light_complexity;
479
480 state->g_time_of_day = vg_fractf( g_time );
481 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
482 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
483
484 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
485 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
486
487 float a = state->g_time_of_day * VG_PIf * 2.0f;
488 state->g_sun_dir[0] = sinf( a );
489 state->g_sun_dir[1] = cosf( a );
490 state->g_sun_dir[2] = 0.2f;
491 v3_normalize( state->g_sun_dir );
492
493
494
495 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
496 glBufferSubData( GL_UNIFORM_BUFFER, 0,
497 sizeof(struct ub_world_lighting), &world->ub_lighting );
498 /* TEMP!!!!!! */
499
500 world_global.sky_time += world_global.sky_rate * vg.time_delta;
501 world_global.sky_rate = vg_lerp( world_global.sky_rate,
502 world_global.sky_target_rate,
503 vg.time_delta * 5.0 );
504
505 world_routes_update( world );
506 world_routes_debug( world );
507
508 /* ---- SFD ------------ */
509
510 if( mdl_arrcount( &world->ent_route ) ){
511 u32 closest = 0;
512 float min_dist = INFINITY;
513
514 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
515 ent_route *route = mdl_arritm( &world->ent_route, i );
516 float dist = v3_dist2( route->board_transform[3], pos );
517
518 if( dist < min_dist ){
519 min_dist = dist;
520 closest = i;
521 }
522 }
523
524 if( (world_global.sfd.active_route_board != closest)
525 || network_scores_updated )
526 {
527 network_scores_updated = 0;
528 world_global.sfd.active_route_board = closest;
529
530 ent_route *route = mdl_arritm( &world->ent_route, closest );
531 u32 id = route->official_track_id;
532
533 if( id != 0xffffffff ){
534 struct netmsg_board *local_board =
535 &scoreboard_client_data.boards[id];
536
537 for( int i=0; i<13; i++ ){
538 sfd_encode( i, &local_board->data[27*i] );
539 }
540 }else{
541 sfd_encode( 0, mdl_pstr( &world->meta, route->pstr_name ) );
542 sfd_encode( 1, "No data" );
543 }
544 }
545 }
546 sfd_update();
547
548 #if 0
549 /* TODO: Bvh */
550
551 static float random_accum = 0.0f;
552 random_accum += vg.time_delta;
553
554 u32 random_ticks = 0;
555
556 while( random_accum > 0.1f ){
557 random_accum -= 0.1f;
558 random_ticks ++;
559 }
560
561 float radius = 25.0f;
562 boxf volume_proximity;
563 v3_add( pos, (v3f){ radius, radius, radius }, volume_proximity[1] );
564 v3_sub( pos, (v3f){ radius, radius, radius }, volume_proximity[0] );
565
566 bh_iter it;
567 bh_iter_init( 0, &it );
568 int idx;
569
570 int in_volume = 0;
571
572 while( bh_next( world->volume_bh, &it, volume_proximity, &idx ) )
573 {
574 struct world_volume *zone = &world->volumes[idx];
575
576 if( zone->node->classtype == k_classtype_volume_audio )
577 {
578 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
579 { 1.0f, 1.0f, 1.0f}},
580 0xff00c0ff );
581 #if 0
582 for( int j=0; j<random_ticks; j++ )
583 {
584 logic_packet packet;
585 packet.location = zone->target_logic_brick;
586 packet.function = 0;
587
588 packet.type = k_mdl_128bit_datatype_vec3;
589 packet.data._v4f[0] = vg_randf()*2.0f-1.0f;
590 packet.data._v4f[1] = vg_randf()*2.0f-1.0f;
591 packet.data._v4f[2] = vg_randf()*2.0f-1.0f;
592 m4x3_mulv( zone->transform, packet.data._v4f, packet.data._v4f );
593
594 logic_bricks_send_packet( world, &packet );
595 }
596 #endif
597 continue;
598 }
599
600 v3f local;
601 m4x3_mulv( zone->inv_transform, pos, local );
602
603 if( (fabsf(local[0]) <= 1.0f) &&
604 (fabsf(local[1]) <= 1.0f) &&
605 (fabsf(local[2]) <= 1.0f) )
606 {
607 in_volume = 1;
608
609 if( !world_global.in_volume )
610 {
611 #if 0
612 logic_packet packet;
613 packet.location = zone->target_logic_brick;
614 packet.function = 0;
615
616 packet.type = k_mdl_128bit_datatype_vec3;
617 v3_copy( pos, packet.data._v4f );
618
619 logic_bricks_send_packet( world, &packet );
620 #endif
621 }
622
623 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
624 { 1.0f, 1.0f, 1.0f}},
625 0xff00ff00 );
626 }
627 else
628 {
629 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
630 { 1.0f, 1.0f, 1.0f}},
631 0xff0000ff );
632 }
633 }
634 #endif
635
636 #if 0
637 if( k_debug_light_indices )
638 {
639 for( int i=0; i<world->light_count; i++ ){
640 struct world_light *light = &world->lights[i];
641 struct classtype_world_light *inf = light->inf;
642
643 u32 colour = 0xff000000;
644 u8 r = inf->colour[0] * 255.0f,
645 g = inf->colour[1] * 255.0f,
646 b = inf->colour[2] * 255.0f;
647
648 colour |= r;
649 colour |= g << 8;
650 colour |= b << 16;
651
652 vg_line_pt3( light->node->co, 0.25f, colour );
653 }
654 }
655
656 world_global.in_volume = in_volume;
657 #endif
658
659 #if 0
660
661 /* process soundscape transactions */
662 audio_lock();
663 for( int i=0; i<world->soundscape_count; i++ )
664 {
665 struct soundscape *s = &world->soundscapes[i];
666 s->usage_count = 0;
667
668 for( int j=0; j<s->max_instances; j++ )
669 {
670 if( s->channels[j] )
671 {
672 if( audio_channel_finished(s->channels[j]) )
673 s->channels[j] = audio_relinquish_channel( s->channels[j] );
674 else
675 s->usage_count ++;
676 }
677 }
678 }
679 audio_unlock();
680 #endif
681 }
682
683 /*
684 * -----------------------------------------------------------------------------
685 * API implementation
686 * -----------------------------------------------------------------------------
687 */
688
689 VG_STATIC void ray_world_get_tri( world_instance *world,
690 ray_hit *hit, v3f tri[3] )
691 {
692 for( int i=0; i<3; i++ )
693 v3_copy( world->scene_geo->arrvertices[ hit->tri[i] ].co, tri[i] );
694 }
695
696 VG_STATIC int ray_world( world_instance *world,
697 v3f pos, v3f dir, ray_hit *hit )
698 {
699 return scene_raycast( world->scene_geo, world->geo_bh, pos, dir, hit );
700 }
701
702 /*
703 * Cast a sphere from a to b and see what time it hits
704 */
705 VG_STATIC int spherecast_world( world_instance *world,
706 v3f pa, v3f pb, float r, float *t, v3f n )
707 {
708 bh_iter it;
709 bh_iter_init( 0, &it );
710
711 boxf region;
712 box_init_inf( region );
713 box_addpt( region, pa );
714 box_addpt( region, pb );
715
716 v3_add( (v3f){ r, r, r}, region[1], region[1] );
717 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
718
719 v3f dir;
720 v3_sub( pb, pa, dir );
721
722 v3f dir_inv;
723 dir_inv[0] = 1.0f/dir[0];
724 dir_inv[1] = 1.0f/dir[1];
725 dir_inv[2] = 1.0f/dir[2];
726
727 int hit = -1;
728 float min_t = 1.0f;
729
730 int idx;
731 while( bh_next( world->geo_bh, &it, region, &idx ) )
732 {
733 u32 *ptri = &world->scene_geo->arrindices[ idx*3 ];
734 v3f tri[3];
735
736 boxf box;
737 box_init_inf( box );
738
739 for( int j=0; j<3; j++ )
740 {
741 v3_copy( world->scene_geo->arrvertices[ptri[j]].co, tri[j] );
742 box_addpt( box, tri[j] );
743 }
744
745 v3_add( (v3f){ r, r, r}, box[1], box[1] );
746 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
747
748 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
749 continue;
750
751 float t;
752 v3f n1;
753 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
754 {
755 if( t < min_t )
756 {
757 min_t = t;
758 hit = idx;
759 v3_copy( n1, n );
760 }
761 }
762 }
763
764 *t = min_t;
765 return hit;
766 }
767
768 VG_STATIC
769 struct world_surface *world_tri_index_surface( world_instance *world,
770 u32 index )
771 {
772 for( int i=1; i<world->surface_count; i++ )
773 {
774 struct world_surface *mat = &world->surfaces[i];
775
776 if( (index >= mat->sm_geo.vertex_start) &&
777 (index < mat->sm_geo.vertex_start+mat->sm_geo.vertex_count ) )
778 {
779 return mat;
780 }
781 }
782
783 /* error surface */
784 return &world->surfaces[0];
785 }
786
787 VG_STATIC struct world_surface *world_contact_surface( world_instance *world,
788 rb_ct *ct )
789 {
790 return world_tri_index_surface( world, ct->element_id );
791 }
792
793 VG_STATIC struct world_surface *ray_hit_surface( world_instance *world,
794 ray_hit *hit )
795 {
796 return world_tri_index_surface( world, hit->tri[0] );
797 }
798
799 #endif /* WORLD_H */