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