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