bricks
[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 static const float k_light_cube_size = 8.0f;
57
58 struct world_instance
59 {
60 /* This is a small flag we use to changelevel.
61 * It will not be cleared until all sounds stop playing
62 */
63
64 /* Fixed items
65 * -------------------------------------------------------
66 */
67
68 char world_name[ 64 ];
69
70 struct
71 {
72 boxf depthbounds;
73 int depth_computed;
74
75 float height;
76 int enabled;
77 v4f plane;
78 }
79 water;
80
81 /* STD140 */
82 struct ub_world_lighting
83 {
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
98 float g_water_fog;
99 float g_time;
100 float g_shadow_length;
101 float g_shadow_spread;
102
103 float g_time_of_day;
104 float g_day_phase;
105 float g_sunset_phase;
106
107 int g_light_preview;
108 int g_shadow_samples;
109
110 int g_debug_indices;
111 int g_debug_complexity;
112 }
113 ub_lighting;
114 GLuint ubo_lighting;
115 int ubo_bind_point;
116
117 GLuint tbo_light_entities,
118 tex_light_entities,
119 tex_light_cubes;
120
121 v3i light_cubes;
122
123 struct framebuffer heightmap;
124
125 /*
126 * Dynamically allocated when world_load is called.
127 *
128 * the following arrays index somewhere into this linear
129 * allocator
130 *
131 * (world_gen.h)
132 * --------------------------------------------------------------------------
133 */
134 /*
135 * Main world .mdl
136 */
137 mdl_context *meta;
138
139 /*
140 * Materials / textures
141 */
142
143 GLuint *textures;
144 u32 texture_count;
145
146 struct world_material
147 {
148 mdl_material info;
149 mdl_submesh sm_geo,
150 sm_no_collide;
151 }
152 * materials;
153 u32 material_count;
154
155 /*
156 * Named safe places to respawn
157 */
158 struct respawn_point
159 {
160 v3f co;
161 v4f q;
162 const char *name;
163 }
164 * spawns;
165 u32 spawn_count;
166
167 /*
168 * Audio player entities
169 */
170 struct world_audio_thing
171 {
172 v3f pos;
173 float volume, range;
174 u32 flags;
175
176 #if 0
177 audio_player player;
178 #endif
179
180 audio_clip temp_embedded_clip;
181 }
182 * audio_things;
183 u32 audio_things_count;
184
185 #if 0
186 /*
187 * Relays [ DEPRECATED ]
188 */
189 struct logic_relay
190 {
191 v3f pos;
192
193 struct relay_target
194 {
195 u32 sub_id;
196 enum classtype classtype;
197 }
198 targets[4];
199 u32 target_count;
200 }
201 * logic_relays;
202 u32 relay_count;
203 #endif
204
205 struct soundscape
206 {
207 /* locking */
208 audio_channel *channels[4];
209
210 /* accessable without locking */
211 v3f spawn_position;
212
213 u32 usage_count;
214 u32 max_instances;
215 u32 allow_transitions;
216 float transition_duration;
217 const char *label;
218 }
219 * soundscapes;
220 u32 soundscape_count;
221
222 struct logic_brick_ref
223 {
224 mdl_node *node;
225 float usage;
226 u32 internal_id; /* used for things like soundscapes where another
227 allocation is made on top */
228 }
229 * logic_bricks;
230 u32 logic_brick_count;
231
232 /*
233 * Box trigger entities
234 */
235 struct trigger_zone
236 {
237 m4x3f transform, inv_transform;
238
239 u32 target_logic_brick;
240 enum classtype classtype;
241 }
242 * triggers;
243 u32 trigger_count;
244
245 /*
246 * Achievements
247 */
248 struct logic_achievement
249 {
250 v3f pos;
251 const char *achievement_id;
252 u32 achieved;
253 }
254 * logic_achievements;
255 u32 achievement_count;
256
257 /*
258 * Lights
259 */
260 struct world_light
261 {
262 mdl_node *node;
263 struct classtype_world_light *inf;
264 m4x3f inverse_world;
265 v2f angle_sin_cos;
266
267 /* enabled.. etc?
268 * TODO: we should order entities in the binary by their type */
269 }
270 * lights;
271 u32 light_count;
272
273 /*
274 * Routes (world_routes.h)
275 * --------------------------------------------------------------------------
276 */
277 struct route_node
278 {
279 v3f co, right, up, h;
280 u32 next[2];
281
282 u32 special_type, special_id, current_refs, ref_count;
283 u32 route_ids[4]; /* Gates can be linked into up to four routes */
284 }
285 *nodes;
286 u32 node_count;
287
288 struct route
289 {
290 u32 track_id;
291 v4f colour;
292
293 u32 start;
294 mdl_submesh sm;
295
296 int active;
297 float factive;
298
299 double best_lap, latest_pass; /* Session */
300
301 m4x3f scoreboard_transform;
302 }
303 *routes;
304 u32 route_count;
305
306 struct route_gate
307 {
308 struct teleport_gate
309 {
310 v3f co[2];
311 v4f q[2];
312 v2f dims;
313
314 m4x3f to_world, transport;
315 }
316 gate;
317
318 u32 node_id;
319
320 struct route_timing
321 {
322 u32 version; /* Incremented on every teleport */
323 double time;
324 }
325 timing;
326 }
327 *gates;
328 u32 gate_count;
329
330 struct nonlocal_gate
331 {
332 struct teleport_gate gate;
333 mdl_node *node;
334
335 u32 target_map_index, working;
336 }
337 *nonlocal_gates;
338 u32 nonlocalgate_count;
339
340 struct route_collector
341 {
342 struct route_timing timing;
343 }
344 *collectors;
345 u32 collector_count;
346
347
348 /* logic
349 * ----------------------------------------------------
350 */
351
352 /* world geometry */
353 scene *scene_geo,
354 *scene_no_collide,
355 *scene_lines;
356
357 /* spacial mappings */
358 bh_tree *audio_bh,
359 *trigger_bh,
360 *geo_bh;
361
362 /* graphics */
363 glmesh mesh_route_lines;
364 glmesh mesh_geo,
365 mesh_no_collide,
366 mesh_water;
367
368 rigidbody rb_geo; /* todo.. ... */
369 };
370
371 VG_STATIC struct world_global
372 {
373 /*
374 * Allocated as system memory
375 * --------------------------------------------------------------------------
376 */
377 void *generic_heap;
378
379 /* rendering */
380 glmesh skydome;
381 mdl_submesh dome_upper, dome_lower;
382
383 glmesh mesh_gate_surface;
384
385 double sky_time, sky_rate, sky_target_rate;
386
387 /* gates, TODO: active_gate should also know which instance */
388 u32 active_gate,
389 current_run_version;
390 double time, rewind_from, rewind_to, last_use;
391
392 /* water rendering */
393 struct
394 {
395 struct framebuffer fbreflect, fbdepth;
396 }
397 water;
398
399 /* split flap display */
400 struct
401 {
402 mdl_submesh *sm_module, *sm_card;
403 glmesh mesh_base, mesh_display;
404
405 u32 w, h;
406 float *buffer;
407 }
408 sfd;
409
410 /* timing bars, fixed maximum amount */
411 struct route_ui_bar
412 {
413 GLuint vao, vbo, ebo;
414
415 u32 indices_head;
416 u32 vertex_head;
417
418 struct route_ui_segment
419 {
420 float length;
421 u32 vertex_start, vertex_count,
422 index_start, index_count, notches;
423 }
424 segments[k_max_ui_segments];
425
426 u32 segment_start, segment_count, fade_start, fade_count;
427 double fade_timer_start;
428 float xpos;
429 }
430 ui_bars[16];
431
432 v3f render_gate_pos;
433 int active_route_board;
434 int in_trigger;
435
436 int switching_to_new_world;
437
438 world_instance worlds[4];
439 u32 world_count;
440 u32 active_world;
441 }
442 world_global;
443
444 VG_STATIC world_instance *get_active_world( void )
445 {
446 return &world_global.worlds[ world_global.active_world ];
447 }
448
449 /*
450 * API
451 */
452
453 VG_STATIC
454 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
455
456 VG_STATIC
457 struct world_material *ray_hit_material( world_instance *world, ray_hit *hit );
458
459 VG_STATIC
460 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
461
462 VG_STATIC
463 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
464
465 /*
466 * Submodules
467 */
468
469 #include "world_routes.h"
470 #include "world_sfd.h"
471 #include "world_render.h"
472 #include "world_water.h"
473 #include "world_logic_bricks.h"
474 #include "world_gen.h"
475 #include "world_gate.h"
476
477 /*
478 * -----------------------------------------------------------------------------
479 * Events
480 * -----------------------------------------------------------------------------
481 */
482
483 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
484 {
485 world_instance *world = get_active_world();
486
487 /*
488 * None of our world audio runs as one shots, they always have a player.
489 * Therefore it is safe to delete clip data after the players are
490 * disconnected
491 */
492 #if 0
493 audio_lock();
494 for( int i=0; i<world->audio_things_count; i++ )
495 {
496 struct world_audio_thing *at = &world->audio_things[i];
497
498 if( audio_player_is_playing( &at->player ) )
499 {
500 u32 cflags = audio_player_get_flags( &at->player );
501 audio_player_set_flags( &at->player, cflags | AUDIO_FLAG_KILL );
502 }
503 }
504 audio_unlock();
505 #endif
506
507 return 0;
508 }
509
510 VG_STATIC int world_change_world( int argc, const char *argv[] )
511 {
512 #if 0
513 world_instance *world = get_active_world();
514
515 if( argc == 0 )
516 {
517 vg_info( "%s\n", world.world_name );
518 return 0;
519 }
520 else
521 {
522 vg_info( "Switching world...\n" );
523 strcpy( world.world_name, argv[0] );
524 world.switching_to_new_world = 1;
525 world_stop_sound( 0, NULL );
526 }
527 #endif
528
529 return 0;
530 }
531
532 VG_STATIC void world_init(void)
533 {
534 #if 0
535 vg_var_push( (struct vg_var){
536 .name = "water_enable",
537 .data = &world.water.enabled,
538 .data_type = k_var_dtype_i32,
539 .opt_i32 = { .min=0, .max=1, .clamp=1 },
540 .persistent = 0
541 });
542 #endif
543
544 vg_function_push( (struct vg_cmd)
545 {
546 .name = "world_stop_sound",
547 .function = world_stop_sound
548 });
549
550 vg_function_push( (struct vg_cmd)
551 {
552 .name = "world",
553 .function = world_change_world
554 });
555
556 world_global.sky_rate = 1.0;
557 world_global.sky_target_rate = 1.0;
558
559 shader_scene_standard_register();
560 shader_scene_standard_alphatest_register();
561 shader_scene_vertex_blend_register();
562 shader_scene_terrain_register();
563 shader_scene_depth_register();
564 shader_scene_position_register();
565
566 shader_model_sky_register();
567
568 vg_info( "Loading world resources\n" );
569
570 vg_linear_clear( vg_mem.scratch );
571 mdl_context *msky = mdl_load_full( vg_mem.scratch, "models/rs_skydome.mdl" );
572
573 mdl_node *nupper = mdl_node_from_name( msky, "dome_complete" );
574 world_global.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
575
576 vg_acquire_thread_sync();
577 {
578 mdl_unpack_glmesh( msky, &world_global.skydome );
579 }
580 vg_release_thread_sync();
581
582 /* Other systems */
583 vg_info( "Loading other world systems\n" );
584
585 vg_loader_step( world_render_init, NULL );
586 vg_loader_step( world_sfd_init, NULL );
587 vg_loader_step( world_water_init, NULL );
588 vg_loader_step( world_gates_init, NULL );
589 vg_loader_step( world_routes_init, NULL );
590
591 /* Allocate dynamic world memory arena */
592 u32 max_size = 76*1024*1024;
593 world_global.generic_heap = vg_create_linear_allocator( vg_mem.rtmemory,
594 max_size,
595 VG_MEMORY_SYSTEM );
596 }
597
598 VG_STATIC void world_audio_init(void)
599 {
600 #if 0
601 u32 size = vg_linear_remaining( vg_audio.audio_pool )
602 - sizeof(vg_linear_allocator);
603
604 world_global.audio_heap = vg_create_linear_allocator( vg_audio.audio_pool,
605 size,
606 VG_MEMORY_SYSTEM );
607 #endif
608 }
609
610 #if 0
611 VG_STATIC void world_trigger_achievement( world_instance *world, u32 uid )
612 {
613 struct logic_achievement *ach = &world->logic_achievements[ uid ];
614
615 if( ach->achieved )
616 return;
617
618 steam_set_achievement( ach->achievement_id );
619 steam_store_achievements();
620
621 ach->achieved = 1;
622 }
623 #endif
624
625 #if 0
626 VG_STATIC void world_run_relay( world_instance *world,
627 struct relay_target *rt );
628
629 VG_STATIC void world_trigger_relay( world_instance *world, u32 uid )
630 {
631 struct logic_relay *relay = &world->logic_relays[ uid ];
632
633 for( int i=0; i<relay->target_count; i++ )
634 {
635 world_run_relay( world, &relay->targets[i] );
636 }
637 }
638 #endif
639
640 #if 0
641 VG_STATIC void world_trigger_audio( world_instance *world, u32 uid )
642 {
643 struct world_audio_thing *wat = &world->audio_things[ uid ];
644
645 audio_lock();
646 audio_player_playclip( &wat->player,
647 &wat->temp_embedded_clip );
648 audio_unlock();
649 }
650 #endif
651
652 #if 0
653 VG_STATIC void world_run_relay( world_instance *world,
654 struct relay_target *rt )
655 {
656 struct entity_instruction
657 {
658 enum classtype classtype;
659 void (*p_trigger)( world_instance *world, u32 uid );
660 }
661 entity_instructions[] =
662 {
663 { k_classtype_logic_achievement, world_trigger_achievement },
664 { k_classtype_logic_relay, world_trigger_relay },
665 { k_classtype_audio, world_trigger_audio },
666 { k_classtype_logic_wire, logic_bricks_trigger_brick }
667 };
668
669 for( int i=0; i<vg_list_size(entity_instructions); i++ )
670 {
671 struct entity_instruction *instr = &entity_instructions[i];
672
673 if( instr->classtype == rt->classtype )
674 {
675 instr->p_trigger( world, rt->sub_id );
676 return;
677 }
678 }
679
680 vg_error( "Don't know how to trigger classtype %d\n", rt->classtype );
681 }
682 #endif
683
684 VG_STATIC void world_update( world_instance *world, v3f pos )
685 {
686 /* TEMP!!!!!! */
687 static double g_time = 0.0;
688 g_time += vg.time_delta * (1.0/(k_day_length*60.0));
689
690
691 struct ub_world_lighting *state = &world->ub_lighting;
692
693 state->g_time = g_time;
694 state->g_debug_indices = k_debug_light_indices;
695 state->g_light_preview = k_light_preview;
696 state->g_debug_complexity = k_debug_light_complexity;
697
698 state->g_time_of_day = vg_fractf( g_time );
699 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
700 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
701
702 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
703 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
704
705 float a = state->g_time_of_day * VG_PIf * 2.0f;
706 state->g_sun_dir[0] = sinf( a );
707 state->g_sun_dir[1] = cosf( a );
708 state->g_sun_dir[2] = 0.2f;
709 v3_normalize( state->g_sun_dir );
710
711
712
713 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
714 glBufferSubData( GL_UNIFORM_BUFFER, 0,
715 sizeof(struct ub_world_lighting), &world->ub_lighting );
716 /* TEMP!!!!!! */
717
718
719 #if 0
720 if( world.switching_to_new_world )
721 {
722 int all_stopped = 1;
723
724 audio_lock();
725 for( int i=0; i<world.audio_things_count; i++ )
726 {
727 struct world_audio_thing *at = &world.audio_things[i];
728
729 if( audio_player_is_playing( &at->player ) )
730 {
731 all_stopped = 0;
732 break;
733 }
734 }
735 audio_unlock();
736
737 if( all_stopped )
738 {
739 world.switching_to_new_world = 0;
740 world_unload();
741 vg_loader_start( world_load );
742 return;
743 }
744 }
745
746 #endif
747 world_global.sky_time += world_global.sky_rate * vg.time_delta;
748 world_global.sky_rate = vg_lerp( world_global.sky_rate,
749 world_global.sky_target_rate,
750 vg.time_delta * 5.0 );
751
752 world_routes_update( world );
753 #if 0
754 world_routes_debug();
755 #endif
756
757 if( world->route_count > 0 )
758 {
759 int closest = 0;
760 float min_dist = INFINITY;
761
762 for( int i=0; i<world->route_count; i++ )
763 {
764 float d = v3_dist2( world->routes[i].scoreboard_transform[3], pos );
765
766 if( d < min_dist )
767 {
768 min_dist = d;
769 closest = i;
770 }
771 }
772
773 if( (world_global.active_route_board != closest)
774 || network_scores_updated )
775 {
776 network_scores_updated = 0;
777 world_global.active_route_board = closest;
778
779 struct route *route = &world->routes[closest];
780
781 u32 id = route->track_id;
782
783 if( id != 0xffffffff )
784 {
785 struct netmsg_board *local_board =
786 &scoreboard_client_data.boards[id];
787
788 for( int i=0; i<13; i++ )
789 {
790 sfd_encode( i, &local_board->data[27*i] );
791 }
792 }
793 }
794 }
795
796 /* TODO: Bvh */
797
798 static float random_accum = 0.0f;
799 random_accum += vg.time_delta;
800
801 u32 random_ticks = 0;
802
803 while( random_accum > 0.1f )
804 {
805 random_accum -= 0.1f;
806 random_ticks ++;
807 }
808
809 float radius = 25.0f;
810 boxf trigger_proximity;
811 v3_add( pos, (v3f){ radius, radius, radius }, trigger_proximity[1] );
812 v3_sub( pos, (v3f){ radius, radius, radius }, trigger_proximity[0] );
813
814 bh_iter it;
815 bh_iter_init( 0, &it );
816 int idx;
817
818 int in_trigger = 0;
819
820 while( bh_next( world->trigger_bh, &it, trigger_proximity, &idx ) )
821 {
822 struct trigger_zone *zone = &world->triggers[idx];
823
824 if( zone->classtype == k_classtype_particle_box )
825 {
826 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
827 { 1.0f, 1.0f, 1.0f}},
828 0xff00c0ff );
829 for( int j=0; j<random_ticks; j++ )
830 {
831 logic_packet packet;
832 packet.location = zone->target_logic_brick;
833 packet.function = 0;
834
835 packet.type = k_mdl_128bit_datatype_vec3;
836 packet.data._v4f[0] = vg_randf()*2.0f-1.0f;
837 packet.data._v4f[1] = vg_randf()*2.0f-1.0f;
838 packet.data._v4f[2] = vg_randf()*2.0f-1.0f;
839 m4x3_mulv( zone->transform, packet.data._v4f, packet.data._v4f );
840
841 logic_bricks_send_packet( world, &packet );
842 }
843 continue;
844 }
845
846 v3f local;
847 m4x3_mulv( zone->inv_transform, pos, local );
848
849 if( (fabsf(local[0]) <= 1.0f) &&
850 (fabsf(local[1]) <= 1.0f) &&
851 (fabsf(local[2]) <= 1.0f) )
852 {
853 in_trigger = 1;
854
855 if( !world_global.in_trigger )
856 {
857 logic_packet packet;
858 packet.location = zone->target_logic_brick;
859 packet.function = 0;
860
861 packet.type = k_mdl_128bit_datatype_vec3;
862 v3_copy( pos, packet.data._v4f );
863
864 logic_bricks_send_packet( world, &packet );
865 }
866
867 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
868 { 1.0f, 1.0f, 1.0f}},
869 0xff00ff00 );
870 }
871 else
872 {
873 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
874 { 1.0f, 1.0f, 1.0f}},
875 0xff0000ff );
876 }
877 }
878
879 if( k_debug_light_indices )
880 {
881 for( int i=0; i<world->light_count; i++ )
882 {
883 struct world_light *light = &world->lights[i];
884 struct classtype_world_light *inf = light->inf;
885
886 u32 colour = 0xff000000;
887 u8 r = inf->colour[0] * 255.0f,
888 g = inf->colour[1] * 255.0f,
889 b = inf->colour[2] * 255.0f;
890
891 colour |= r;
892 colour |= g << 8;
893 colour |= b << 16;
894
895 vg_line_pt3( light->node->co, 0.25f, colour );
896 }
897 }
898
899 world_global.in_trigger = in_trigger;
900 sfd_update();
901
902 if( debug_logic_bricks )
903 logic_bricks_debug( world );
904
905 /* process soundscape transactions */
906 audio_lock();
907 for( int i=0; i<world->soundscape_count; i++ )
908 {
909 struct soundscape *s = &world->soundscapes[i];
910 s->usage_count = 0;
911
912 for( int j=0; j<s->max_instances; j++ )
913 {
914 if( s->channels[j] )
915 {
916 if( audio_channel_finished(s->channels[j]) )
917 s->channels[j] = audio_relinquish_channel( s->channels[j] );
918 else
919 s->usage_count ++;
920 }
921 }
922 }
923 audio_unlock();
924 }
925
926 /*
927 * -----------------------------------------------------------------------------
928 * API implementation
929 * -----------------------------------------------------------------------------
930 */
931
932 VG_STATIC void ray_world_get_tri( world_instance *world,
933 ray_hit *hit, v3f tri[3] )
934 {
935 for( int i=0; i<3; i++ )
936 v3_copy( world->scene_geo->arrvertices[ hit->tri[i] ].co, tri[i] );
937 }
938
939 VG_STATIC int ray_world( world_instance *world,
940 v3f pos, v3f dir, ray_hit *hit )
941 {
942 return scene_raycast( world->scene_geo, world->geo_bh, pos, dir, hit );
943 }
944
945 /*
946 * Cast a sphere from a to b and see what time it hits
947 */
948 VG_STATIC int spherecast_world( world_instance *world,
949 v3f pa, v3f pb, float r, float *t, v3f n )
950 {
951 bh_iter it;
952 bh_iter_init( 0, &it );
953
954 boxf region;
955 box_init_inf( region );
956 box_addpt( region, pa );
957 box_addpt( region, pb );
958
959 v3_add( (v3f){ r, r, r}, region[1], region[1] );
960 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
961
962 v3f dir;
963 v3_sub( pb, pa, dir );
964
965 v3f dir_inv;
966 dir_inv[0] = 1.0f/dir[0];
967 dir_inv[1] = 1.0f/dir[1];
968 dir_inv[2] = 1.0f/dir[2];
969
970 int hit = -1;
971 float min_t = 1.0f;
972
973 int idx;
974 while( bh_next( world->geo_bh, &it, region, &idx ) )
975 {
976 u32 *ptri = &world->scene_geo->arrindices[ idx*3 ];
977 v3f tri[3];
978
979 boxf box;
980 box_init_inf( box );
981
982 for( int j=0; j<3; j++ )
983 {
984 v3_copy( world->scene_geo->arrvertices[ptri[j]].co, tri[j] );
985 box_addpt( box, tri[j] );
986 }
987
988 v3_add( (v3f){ r, r, r}, box[1], box[1] );
989 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
990
991 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
992 continue;
993
994 float t;
995 v3f n1;
996 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
997 {
998 if( t < min_t )
999 {
1000 min_t = t;
1001 hit = idx;
1002 v3_copy( n1, n );
1003 }
1004 }
1005 }
1006
1007 *t = min_t;
1008 return hit;
1009 }
1010
1011 VG_STATIC
1012 struct world_material *world_tri_index_material( world_instance *world,
1013 u32 index )
1014 {
1015 for( int i=1; i<world->material_count; i++ )
1016 {
1017 struct world_material *mat = &world->materials[i];
1018
1019 if( (index >= mat->sm_geo.vertex_start) &&
1020 (index < mat->sm_geo.vertex_start+mat->sm_geo.vertex_count ) )
1021 {
1022 return mat;
1023 }
1024 }
1025
1026 /* error material */
1027 return &world->materials[0];
1028 }
1029
1030 VG_STATIC struct world_material *world_contact_material( world_instance *world,
1031 rb_ct *ct )
1032 {
1033 return world_tri_index_material( world, ct->element_id );
1034 }
1035
1036 VG_STATIC struct world_material *ray_hit_material( world_instance *world,
1037 ray_hit *hit )
1038 {
1039 return world_tri_index_material( world, hit->tri[0] );
1040 }
1041
1042 #endif /* WORLD_H */