loader stuff
[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 #include "entity.h"
22 #include "font.h"
23
24 #include "shaders/scene_standard.h"
25 #include "shaders/scene_standard_alphatest.h"
26 #include "shaders/scene_vertex_blend.h"
27 #include "shaders/scene_terrain.h"
28 #include "shaders/scene_depth.h"
29 #include "shaders/scene_position.h"
30
31 #include "shaders/model_sky.h"
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 static const float k_light_cube_size = 8.0f;
57
58 struct world_instance {
59
60 /* Fixed items
61 * -------------------------------------------------------
62 */
63
64 void *heap;
65 enum world_status{
66 k_world_status_unloaded = 0,
67 k_world_status_loading = 1,
68 k_world_status_loaded = 2,
69 k_world_status_unloading = 3 /* dont spawn sounds and stuff */
70 }
71 status;
72
73 struct{
74 boxf depthbounds;
75 int depth_computed;
76
77 float height;
78 int enabled;
79 v4f plane;
80 }
81 water;
82
83 f64 time;
84
85 /* STD140 */
86 struct ub_world_lighting{
87 v4f g_cube_min,
88 g_cube_inv_range;
89
90 v4f g_water_plane,
91 g_depth_bounds;
92
93 v4f g_daysky_colour;
94 v4f g_nightsky_colour;
95 v4f g_sunset_colour;
96 v4f g_ambient_colour;
97 v4f g_sunset_ambient;
98 v4f g_sun_colour;
99 v4f g_sun_dir;
100 v4f g_board_0;
101 v4f g_board_1;
102
103 float g_water_fog;
104 float g_time;
105 float g_realtime;
106 float g_shadow_length;
107 float g_shadow_spread;
108
109 float g_time_of_day;
110 float g_day_phase;
111 float g_sunset_phase;
112
113 int g_light_preview;
114 int g_shadow_samples;
115
116 int g_debug_indices;
117 int g_debug_complexity;
118 }
119 ub_lighting;
120 GLuint ubo_lighting;
121 int ubo_bind_point;
122
123 GLuint tbo_light_entities,
124 tex_light_entities,
125 tex_light_cubes;
126
127 float probabilities[3];
128
129 v3i light_cubes;
130 struct framebuffer heightmap;
131
132 /*
133 * Dynamically allocated when world_load is called.
134 *
135 * the following arrays index somewhere into this linear
136 * allocator
137 *
138 * (world_gen.h)
139 * --------------------------------------------------------------------------
140 */
141
142 /*
143 * Main world .mdl
144 */
145 mdl_context meta;
146
147 GLuint *textures;
148 u32 texture_count;
149
150 struct world_surface{
151 mdl_material info;
152 mdl_submesh sm_geo,
153 sm_no_collide;
154 }
155 * surfaces;
156 u32 surface_count;
157
158 ent_worldinfo info;
159 mdl_array_ptr ent_spawn,
160 ent_gate,
161 ent_light,
162 ent_route_node,
163 ent_path_index,
164 ent_checkpoint,
165 ent_route,
166 ent_water,
167
168 ent_audio_clip,
169 ent_audio,
170 ent_volume,
171 ent_traffic,
172 ent_skateshop,
173 ent_marker,
174 ent_camera,
175 ent_swspreview;
176
177 ent_gate *rendering_gate;
178
179 /* logic
180 * ----------------------------------------------------
181 */
182
183 /* world geometry */
184 scene_context scene_geo,
185 scene_no_collide,
186 scene_lines;
187
188 /* spacial mappings */
189 bh_tree *audio_bh,
190 *volume_bh,
191 *geo_bh;
192
193 /* graphics */
194 glmesh mesh_route_lines;
195 glmesh mesh_geo,
196 mesh_no_collide,
197 mesh_water;
198
199 rb_object rb_geo;
200 };
201
202 struct world_global{
203 /*
204 * Allocated as system memory
205 * --------------------------------------------------------------------------
206 */
207 void *heap;
208 char *load_target;
209
210 /* rendering */
211 glmesh skydome;
212 glmesh mesh_gate;
213 mdl_submesh sm_gate_surface,
214 sm_gate_marker[4];
215
216 double sky_time, sky_rate, sky_target_rate;
217
218 u32 current_run_version;
219 double time, rewind_from, rewind_to, last_use;
220
221 /* water rendering */
222 struct{
223 struct framebuffer fbreflect, fbdepth;
224 }
225 water;
226
227 /* split flap display */
228 struct{
229 glmesh mesh_base, mesh_display;
230 mdl_submesh sm_base;
231 u32 active_route_board;
232 scene_context scene;
233
234 u32 w, h;
235 float *buffer;
236 }
237 sfd;
238
239 v3f render_gate_pos;
240 int in_volume;
241
242 world_instance worlds[4];
243 u32 active_world;
244
245 /* text particles */
246 font3d font;
247
248 struct timer_text{
249 char text[8];
250 m4x3f transform;
251 ent_gate *gate;
252 ent_route *route;
253 }
254 timer_texts[4];
255 u32 timer_text_count;
256
257 struct text_particle{
258 rb_object obj;
259 m4x3f mlocal;
260 ent_glyph *glyph;
261 v4f colour;
262
263 m4x3f mdl;
264 }
265 text_particles[6*4];
266 u32 text_particle_count;
267 }
268 static world_global;
269 VG_STATIC void entity_call( world_instance *world, ent_call *call );
270
271 VG_STATIC world_instance *get_active_world( void )
272 {
273 return &world_global.worlds[ world_global.active_world ];
274 }
275
276 /*
277 * API
278 */
279
280 VG_STATIC
281 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
282
283 VG_STATIC
284 struct world_surface *ray_hit_surface( world_instance *world, ray_hit *hit );
285
286 VG_STATIC
287 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
288
289 VG_STATIC
290 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
291
292 VG_STATIC
293 ent_spawn *world_find_closest_spawn( world_instance *world, v3f position )
294 {
295 ent_spawn *rp = NULL, *r;
296 float min_dist = INFINITY;
297
298 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
299 r = mdl_arritm( &world->ent_spawn, i );
300 float d = v3_dist2( r->transform.co, position );
301
302 if( d < min_dist ){
303 min_dist = d;
304 rp = r;
305 }
306 }
307
308 if( !rp ){
309 if( mdl_arrcount(&world->ent_spawn) ){
310 vg_warn( "Invalid distances to spawns.. defaulting to first one.\n" );
311 return mdl_arritm( &world->ent_spawn, 0 );
312 }
313 else{
314 vg_error( "There are no spawns in the level!\n" );
315 }
316 }
317
318 return rp;
319 }
320
321 VG_STATIC
322 ent_spawn *world_find_spawn_by_name( world_instance *world, const char *name )
323 {
324 ent_spawn *rp = NULL, *r;
325 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
326 r = mdl_arritm( &world->ent_spawn, i );
327 if( !strcmp( mdl_pstr(&world->meta, r->pstr_name), name ) ){
328 rp = r;
329 break;
330 }
331 }
332
333 if( !rp )
334 vg_warn( "No spawn named '%s'\n", name );
335
336 return rp;
337 }
338
339 /*
340 * Submodules
341 */
342
343 VG_STATIC float
344 k_day_length = 30.0f; /* minutes */
345
346 VG_STATIC int k_debug_light_indices = 0,
347 k_debug_light_complexity = 0,
348 k_light_preview = 0;
349
350 #include "world_routes.h"
351 #include "world_sfd.h"
352 #include "world_render.h"
353 #include "world_water.h"
354 #include "world_volumes.h"
355 #include "world_gen.h"
356 #include "world_gate.h"
357
358 /*
359 * -----------------------------------------------------------------------------
360 * Events
361 * -----------------------------------------------------------------------------
362 */
363
364 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
365 {
366 world_instance *world = get_active_world();
367 return 0;
368 }
369
370 VG_STATIC void world_init(void)
371 {
372 VG_VAR_F32( k_day_length );
373 VG_VAR_I32( k_debug_light_indices );
374 VG_VAR_I32( k_debug_light_complexity );
375 VG_VAR_I32( k_light_preview );
376
377 world_global.sky_rate = 1.0;
378 world_global.sky_target_rate = 1.0;
379
380 shader_scene_standard_register();
381 shader_scene_standard_alphatest_register();
382 shader_scene_vertex_blend_register();
383 shader_scene_terrain_register();
384 shader_scene_depth_register();
385 shader_scene_position_register();
386
387 shader_model_sky_register();
388
389 vg_info( "Loading world resources\n" );
390
391 vg_linear_clear( vg_mem.scratch );
392
393 mdl_context msky;
394 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
395 mdl_load_metadata_block( &msky, vg_mem.scratch );
396 mdl_async_load_glmesh( &msky, &world_global.skydome );
397 mdl_close( &msky );
398
399 /* Other systems */
400 vg_info( "Loading other world systems\n" );
401
402 vg_loader_step( world_render_init, NULL );
403 vg_loader_step( world_sfd_init, NULL );
404 vg_loader_step( world_water_init, NULL );
405 vg_loader_step( world_gates_init, NULL );
406 vg_loader_step( world_routes_init, NULL );
407
408 /* Allocate dynamic world memory arena */
409 u32 max_size = 76*1024*1024;
410 world_global.heap = vg_create_linear_allocator( vg_mem.rtmemory, max_size,
411 VG_MEMORY_SYSTEM );
412 }
413
414 VG_STATIC void ent_volume_call( world_instance *world, ent_call *call )
415 {
416 u32 index = mdl_entity_id_id( call->id );
417 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
418 if( !volume->target ) return;
419
420 if( call->function == k_ent_function_trigger ){
421 call->id = volume->target;
422
423 if( volume->type == k_volume_subtype_particle ){
424 float *co = alloca( sizeof(float)*3 );
425 co[0] = vg_randf64()*2.0f-1.0f;
426 co[1] = vg_randf64()*2.0f-1.0f;
427 co[2] = vg_randf64()*2.0f-1.0f;
428 m4x3_mulv( volume->to_world, co, co );
429
430 call->function = k_ent_function_particle_spawn;
431 call->data = co;
432 entity_call( world, call );
433 }
434 else{
435 entity_call( world, call );
436 }
437 }
438 }
439
440 VG_STATIC void ent_audio_call( world_instance *world, ent_call *call )
441 {
442 if( world->status == k_world_status_unloading ){
443 vg_warn( "cannot modify audio while unloading world\n" );
444 return;
445 }
446
447 u8 world_id = (world - world_global.worlds) + 1;
448 u32 index = mdl_entity_id_id( call->id );
449 ent_audio *audio = mdl_arritm( &world->ent_audio, index );
450
451 v3f sound_co;
452
453 if( call->function == k_ent_function_particle_spawn ){
454 v3_copy( call->data, sound_co );
455 }
456 else if( call->function == k_ent_function_trigger ){
457 v3_copy( audio->transform.co, sound_co );
458 }
459 else
460 vg_fatal_error( "ent_audio_call (invalid function id)" );
461
462 float chance = vg_randf64()*100.0f,
463 bar = 0.0f;
464
465 for( u32 i=0; i<audio->clip_count; i++ ){
466 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
467 audio->clip_start+i );
468
469 float mod = world->probabilities[ audio->probability_curve ],
470 p = clip->probability * mod;
471
472 bar += p;
473
474 if( chance < bar ){
475 audio_lock();
476
477 if( audio->behaviour == k_channel_behaviour_unlimited ){
478 audio_oneshot_3d( &clip->clip, sound_co,
479 audio->transform.s[0],
480 audio->volume );
481 }
482 else if( audio->behaviour == k_channel_behaviour_discard_if_full ){
483 audio_channel *ch =
484 audio_get_group_idle_channel( audio->group,
485 audio->max_channels );
486
487 if( ch ){
488 audio_channel_init( ch, &clip->clip, audio->flags );
489 audio_channel_group( ch, audio->group );
490 audio_channel_world( ch, world_id );
491 audio_channel_set_spacial( ch, sound_co, audio->transform.s[0] );
492 audio_channel_edit_volume( ch, audio->volume, 1 );
493 ch = audio_relinquish_channel( ch );
494 }
495 }
496 else if( audio->behaviour == k_channel_behaviour_crossfade_if_full){
497 audio_channel *ch =
498 audio_get_group_idle_channel( audio->group,
499 audio->max_channels );
500
501 /* group is full */
502 if( !ch ){
503 audio_channel *existing =
504 audio_get_group_first_active_channel( audio->group );
505
506 if( existing ){
507 if( existing->source == &clip->clip ){
508 audio_unlock();
509 return;
510 }
511
512 existing->group = 0;
513 existing = audio_channel_fadeout(existing, audio->crossfade);
514 }
515
516 ch = audio_get_first_idle_channel();
517 }
518
519 if( ch ){
520 audio_channel_init( ch, &clip->clip, audio->flags );
521 audio_channel_group( ch, audio->group );
522 audio_channel_world( ch, world_id );
523 audio_channel_fadein( ch, audio->crossfade );
524 ch = audio_relinquish_channel( ch );
525 }
526 }
527
528 audio_unlock();
529 return;
530 }
531 }
532 }
533
534 /* finds any active playing in world and fades them out, we can only do this
535 * while unloading */
536 VG_STATIC void world_fadeout_audio( world_instance *world )
537 {
538 if( world->status != k_world_status_unloading ){
539 vg_fatal_error( "World status must be set to 'unloading', to fadeout"
540 " audio.\n" );
541 }
542
543 u8 world_id = (world - world_global.worlds) + 1;
544
545 audio_lock();
546 for( u32 i=0; i<AUDIO_CHANNELS; i++ ){
547 audio_channel *ch = &vg_audio.channels[i];
548
549 if( ch->allocated && (ch->world_id == world_id) ){
550 ch = audio_channel_fadeout( ch, 1.0f );
551 }
552 }
553 audio_unlock();
554 }
555
556 VG_STATIC void world_update( world_instance *world, v3f pos )
557 {
558 world_global.sky_time += world_global.sky_rate * vg.time_delta;
559 world_global.sky_rate = vg_lerp( world_global.sky_rate,
560 world_global.sky_target_rate,
561 vg.time_delta * 5.0 );
562
563 world_routes_update_timer_texts( world );
564 world_routes_update( world );
565 //world_routes_debug( world );
566
567 /* ---- traffic -------- */
568
569 for( u32 i=0; i<mdl_arrcount( &world->ent_traffic ); i++ ){
570 ent_traffic *traffic = mdl_arritm( &world->ent_traffic, i );
571
572 u32 i1 = traffic->index,
573 i0,
574 i2 = i1+1;
575
576 if( i1 == 0 ) i0 = traffic->node_count-1;
577 else i0 = i1-1;
578
579 if( i2 >= traffic->node_count ) i2 = 0;
580
581 i0 += traffic->start_node;
582 i1 += traffic->start_node;
583 i2 += traffic->start_node;
584
585 v3f h[3];
586
587 ent_route_node *rn0 = mdl_arritm( &world->ent_route_node, i0 ),
588 *rn1 = mdl_arritm( &world->ent_route_node, i1 ),
589 *rn2 = mdl_arritm( &world->ent_route_node, i2 );
590
591 v3_copy( rn1->co, h[1] );
592 v3_lerp( rn0->co, rn1->co, 0.5f, h[0] );
593 v3_lerp( rn1->co, rn2->co, 0.5f, h[2] );
594
595 float const k_sample_dist = 0.0025f;
596 v3f pc, pd;
597 eval_bezier3( h[0], h[1], h[2], traffic->t, pc );
598 eval_bezier3( h[0], h[1], h[2], traffic->t+k_sample_dist, pd );
599
600 v3f v0;
601 v3_sub( pd, pc, v0 );
602 float length = vg_maxf( 0.0001f, v3_length( v0 ) );
603 v3_muls( v0, 1.0f/length, v0 );
604
605 float mod = k_sample_dist / length;
606
607 traffic->t += traffic->speed * vg.time_delta * mod;
608
609 if( traffic->t > 1.0f ){
610 traffic->t -= 1.0f;
611
612 if( traffic->t > 1.0f ) traffic->t = 0.0f;
613
614 traffic->index ++;
615
616 if( traffic->index >= traffic->node_count )
617 traffic->index = 0;
618 }
619
620 v3_copy( pc, traffic->transform.co );
621
622 float a = atan2f( -v0[0], v0[2] );
623 q_axis_angle( traffic->transform.q, (v3f){0.0f,1.0f,0.0f}, -a );
624
625 vg_line_pt3( traffic->transform.co, 0.3f, VG__BLUE );
626 }
627
628 /* ---- SFD ------------ */
629
630 if( mdl_arrcount( &world->ent_route ) ){
631 u32 closest = 0;
632 float min_dist = INFINITY;
633
634 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
635 ent_route *route = mdl_arritm( &world->ent_route, i );
636 float dist = v3_dist2( route->board_transform[3], pos );
637
638 if( dist < min_dist ){
639 min_dist = dist;
640 closest = i;
641 }
642 }
643
644 if( (world_global.sfd.active_route_board != closest)
645 || network_scores_updated )
646 {
647 network_scores_updated = 0;
648 world_global.sfd.active_route_board = closest;
649
650 ent_route *route = mdl_arritm( &world->ent_route, closest );
651 u32 id = route->official_track_id;
652
653 if( id != 0xffffffff ){
654 struct netmsg_board *local_board =
655 &scoreboard_client_data.boards[id];
656
657 for( int i=0; i<13; i++ ){
658 sfd_encode( i, &local_board->data[27*i] );
659 }
660 }else{
661 sfd_encode( 0, mdl_pstr( &world->meta, route->pstr_name ) );
662 sfd_encode( 1, "No data" );
663 }
664 }
665 }
666 sfd_update();
667
668 static float random_accum = 0.0f;
669 random_accum += vg.time_delta;
670
671 u32 random_ticks = 0;
672
673 while( random_accum > 0.1f ){
674 random_accum -= 0.1f;
675 random_ticks ++;
676 }
677
678 float radius = 25.0f;
679 boxf volume_proximity;
680 v3_add( pos, (v3f){ radius, radius, radius }, volume_proximity[1] );
681 v3_sub( pos, (v3f){ radius, radius, radius }, volume_proximity[0] );
682
683 bh_iter it;
684 bh_iter_init_box( 0, &it, volume_proximity );
685 i32 idx;
686
687 int in_volume = 0;
688
689 while( bh_next( world->volume_bh, &it, &idx ) ){
690 ent_volume *volume = mdl_arritm( &world->ent_volume, idx );
691
692 boxf cube = {{-1.0f,-1.0f,-1.0f},{1.0f,1.0f,1.0f}};
693
694 if( volume->type == k_volume_subtype_trigger ){
695 v3f local;
696 m4x3_mulv( volume->to_local, 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_volume = 1;
703 vg_line_boxf_transformed( volume->to_world, cube, 0xff00ff00 );
704
705 if( !world_global.in_volume ){
706 ent_call basecall;
707 basecall.function = k_ent_function_trigger;
708 basecall.id = mdl_entity_id( k_ent_volume, idx );
709 basecall.data = NULL;
710
711 entity_call( world, &basecall );
712 }
713 }
714 else
715 vg_line_boxf_transformed( volume->to_world, cube, 0xff0000ff );
716 }
717 else if( volume->type == k_volume_subtype_particle ){
718 vg_line_boxf_transformed( volume->to_world, cube, 0xff00c0ff );
719
720 for( int j=0; j<random_ticks; j++ ){
721 ent_call basecall;
722 basecall.id = mdl_entity_id( k_ent_volume, idx );
723 basecall.data = NULL;
724
725 entity_call( world, &basecall );
726 }
727 }
728 }
729 world_global.in_volume = in_volume;
730
731 #if 0
732 if( k_debug_light_indices )
733 {
734 for( int i=0; i<world->light_count; i++ ){
735 struct world_light *light = &world->lights[i];
736 struct classtype_world_light *inf = light->inf;
737
738 u32 colour = 0xff000000;
739 u8 r = inf->colour[0] * 255.0f,
740 g = inf->colour[1] * 255.0f,
741 b = inf->colour[2] * 255.0f;
742
743 colour |= r;
744 colour |= g << 8;
745 colour |= b << 16;
746
747 vg_line_pt3( light->node->co, 0.25f, colour );
748 }
749 }
750
751 #endif
752 }
753
754 /*
755 * -----------------------------------------------------------------------------
756 * API implementation
757 * -----------------------------------------------------------------------------
758 */
759
760 VG_STATIC void ray_world_get_tri( world_instance *world,
761 ray_hit *hit, v3f tri[3] )
762 {
763 for( int i=0; i<3; i++ )
764 v3_copy( world->scene_geo.arrvertices[ hit->tri[i] ].co, tri[i] );
765 }
766
767 VG_STATIC int ray_world( world_instance *world,
768 v3f pos, v3f dir, ray_hit *hit )
769 {
770 return scene_raycast( &world->scene_geo, world->geo_bh, pos, dir, hit );
771 }
772
773 /*
774 * Cast a sphere from a to b and see what time it hits
775 */
776 VG_STATIC int spherecast_world( world_instance *world,
777 v3f pa, v3f pb, float r, float *t, v3f n )
778 {
779 boxf region;
780 box_init_inf( region );
781 box_addpt( region, pa );
782 box_addpt( region, pb );
783
784 v3_add( (v3f){ r, r, r}, region[1], region[1] );
785 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
786
787 v3f dir;
788 v3_sub( pb, pa, dir );
789
790 v3f dir_inv;
791 dir_inv[0] = 1.0f/dir[0];
792 dir_inv[1] = 1.0f/dir[1];
793 dir_inv[2] = 1.0f/dir[2];
794
795 int hit = -1;
796 float min_t = 1.0f;
797
798 bh_iter it;
799 bh_iter_init_box( 0, &it, region );
800 i32 idx;
801 while( bh_next( world->geo_bh, &it, &idx ) ){
802 u32 *ptri = &world->scene_geo.arrindices[ idx*3 ];
803 v3f tri[3];
804
805 boxf box;
806 box_init_inf( box );
807
808 for( int j=0; j<3; j++ ){
809 v3_copy( world->scene_geo.arrvertices[ptri[j]].co, tri[j] );
810 box_addpt( box, tri[j] );
811 }
812
813 v3_add( (v3f){ r, r, r}, box[1], box[1] );
814 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
815
816 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
817 continue;
818
819 float t;
820 v3f n1;
821 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) ){
822 if( t < min_t ){
823 min_t = t;
824 hit = idx;
825 v3_copy( n1, n );
826 }
827 }
828 }
829
830 *t = min_t;
831 return hit;
832 }
833
834 VG_STATIC
835 struct world_surface *world_tri_index_surface( world_instance *world,
836 u32 index )
837 {
838 for( int i=1; i<world->surface_count; i++ ){
839 struct world_surface *surf = &world->surfaces[i];
840
841 if( (index >= surf->sm_geo.vertex_start) &&
842 (index < surf->sm_geo.vertex_start+surf->sm_geo.vertex_count ) )
843 {
844 return surf;
845 }
846 }
847
848 return &world->surfaces[0];
849 }
850
851 VG_STATIC struct world_surface *world_contact_surface( world_instance *world,
852 rb_ct *ct )
853 {
854 return world_tri_index_surface( world, ct->element_id );
855 }
856
857 VG_STATIC struct world_surface *ray_hit_surface( world_instance *world,
858 ray_hit *hit )
859 {
860 return world_tri_index_surface( world, hit->tri[0] );
861 }
862
863 /*
864 * -----------------------------------------------------------------------------
865 * Audio sampling
866 * -----------------------------------------------------------------------------
867 */
868
869 VG_STATIC
870 enum audio_sprite_type world_audio_sample_sprite_kandom(v3f origin, v3f output);
871 VG_STATIC void world_audio_sample_distances( v3f co, int *index, float *value );
872
873 #include "audio.h"
874
875 /*
876 * Trace out a random point, near the player to try and determine water areas
877 */
878 VG_STATIC
879 enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output)
880 {
881 v3f chance = { (vg_randf64()-0.5f) * 30.0f,
882 8.0f,
883 (vg_randf64()-0.5f) * 30.0f };
884
885 v3f pos;
886 v3_add( chance, origin, pos );
887
888 ray_hit contact;
889 contact.dist = vg_minf( 16.0f, pos[1] );
890
891 world_instance *world = get_active_world();
892
893 if( ray_world( world, pos, (v3f){0.0f,-1.0f,0.0f}, &contact ) ){
894 struct world_surface *mat = ray_hit_surface( world, &contact );
895
896 if( mat->info.surface_prop == k_surface_prop_grass){
897 v3_copy( contact.pos, output );
898 return k_audio_sprite_type_grass;
899 }
900 else{
901 return k_audio_sprite_type_none;
902 }
903 }
904
905 output[0] = pos[0];
906 output[1] = 0.0f;
907 output[2] = pos[2];
908
909 float dist = fabsf(output[1] - origin[1]);
910
911 if( world->water.enabled && dist<=40.0f )
912 return k_audio_sprite_type_water;
913 else
914 return k_audio_sprite_type_none;
915 }
916
917 VG_STATIC void world_audio_sample_distances( v3f co, int *index, float *value )
918 {
919 float inr3 = 0.57735027,
920 inr2 = 0.70710678118;
921
922 v3f sample_directions[] = {
923 { -1.0f, 0.0f, 0.0f },
924 { 1.0f, 0.0f, 0.0f },
925 { 0.0f, 0.0f, 1.0f },
926 { 0.0f, 0.0f, -1.0f },
927 { 0.0f, 1.0f, 0.0f },
928 { 0.0f, -1.0f, 0.0f },
929 { -inr3, inr3, inr3 },
930 { inr3, inr3, inr3 },
931 { -inr3, inr3, -inr3 },
932 { inr3, inr3, -inr3 },
933 { -inr2, 0.0f, inr2 },
934 { inr2, 0.0f, inr2 },
935 { -inr2, 0.0f, -inr2 },
936 { inr2, 0.0f, -inr2 },
937 };
938
939 static int si = 0;
940 static float distances[16];
941
942 ray_hit ray;
943 ray.dist = 5.0f;
944
945 v3f rc, rd, ro;
946 v3_copy( sample_directions[ si ], rd );
947 v3_add( co, (v3f){0.0f,1.5f,0.0f}, ro );
948 v3_copy( ro, rc );
949
950 float dist = 200.0f;
951
952 for( int i=0; i<10; i++ ){
953 if( ray_world( get_active_world(), rc, rd, &ray ) ){
954 dist = (float)i*5.0f + ray.dist;
955 break;
956 }
957 else{
958 v3_muladds( rc, rd, ray.dist, rc );
959 }
960 }
961
962 distances[si] = dist;
963
964 if( vg_lines.draw ){
965 for( int i=0; i<14; i++ ){
966 if( distances[i] != 200.0f ){
967 u32 colours[] = { VG__RED, VG__BLUE, VG__GREEN,
968 VG__CYAN, VG__YELOW, VG__PINK,
969 VG__WHITE };
970
971 u32 colour = colours[i%7];
972
973 v3f p1;
974 v3_muladds( ro, sample_directions[i], distances[i], p1 );
975 vg_line( ro, p1, colour );
976 vg_line_pt3( p1, 0.1f, colour );
977 }
978 }
979 }
980
981 *index = si;
982 *value = dist;
983
984 si ++;
985 if( si >= 14 )
986 si = 0;
987 }
988
989 #endif /* WORLD_H */