From: hgn Date: Mon, 13 Mar 2023 16:16:35 +0000 (+0000) Subject: bricks X-Git-Url: https://harrygodden.com/git/?a=commitdiff_plain;h=000297f007a08b25f458656bfb8dfe4345f2ec32;p=carveJwlIkooP6JGAAIwe30JlM.git bricks --- diff --git a/bvh.h b/bvh.h index 52ce24a..94ea69b 100644 --- a/bvh.h +++ b/bvh.h @@ -143,16 +143,9 @@ VG_STATIC bh_tree *bh_create( void *lin_alloc, bh_system *system, { assert( max_per_leaf > 0 ); - if( item_count == 0 ) - { - bh_tree *bh = vg_linear_alloc( lin_alloc, sizeof(bh_tree) ); - bh->node_count = 0; - bh->system = system; - bh->user = user; - return bh; - } + u32 alloc_count = VG_MAX( 1, item_count ); - u32 totsize = sizeof(bh_tree) + sizeof(bh_node)*(item_count*2-1); + u32 totsize = sizeof(bh_tree) + sizeof(bh_node)*(alloc_count*2-1); bh_tree *bh = vg_linear_alloc( lin_alloc, vg_align8(totsize) ); bh->system = system; bh->user = user; @@ -167,7 +160,9 @@ VG_STATIC bh_tree *bh_create( void *lin_alloc, bh_system *system, root->start = 0; bh_update_bounds( bh, 0 ); - bh_subdivide( bh, 0 ); + + if( item_count > 2 ) + bh_subdivide( bh, 0 ); totsize = sizeof(bh_tree) + sizeof(bh_node) * bh->node_count; bh = vg_linear_resize( lin_alloc, bh, totsize ); diff --git a/maps_src/mp_home.mdl b/maps_src/mp_home.mdl index 29c0ae5..4684fc7 100644 Binary files a/maps_src/mp_home.mdl and b/maps_src/mp_home.mdl differ diff --git a/world.h b/world.h index 4352b6e..106b824 100644 --- a/world.h +++ b/world.h @@ -806,24 +806,40 @@ VG_STATIC void world_update( world_instance *world, v3f pos ) random_ticks ++; } + float radius = 25.0f; + boxf trigger_proximity; + v3_add( pos, (v3f){ radius, radius, radius }, trigger_proximity[1] ); + v3_sub( pos, (v3f){ radius, radius, radius }, trigger_proximity[0] ); + + bh_iter it; + bh_iter_init( 0, &it ); + int idx; + int in_trigger = 0; - for( int i=0; itrigger_count; i++ ) + + while( bh_next( world->trigger_bh, &it, trigger_proximity, &idx ) ) { - struct trigger_zone *zone = &world->triggers[i]; + struct trigger_zone *zone = &world->triggers[idx]; - for( int j=0; jclasstype == k_classtype_particle_box ) { - logic_packet packet; - packet.location = zone->target_logic_brick; - packet.function = 0; + vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f}, + { 1.0f, 1.0f, 1.0f}}, + 0xff00c0ff ); + for( int j=0; jtarget_logic_brick; + packet.function = 0; - packet.type = k_mdl_128bit_datatype_vec3; - packet.data._v4f[0] = vg_randf()*2.0f-1.0f; - packet.data._v4f[1] = vg_randf()*2.0f-1.0f; - packet.data._v4f[2] = vg_randf()*2.0f-1.0f; - m4x3_mulv( zone->transform, packet.data._v4f, packet.data._v4f ); + packet.type = k_mdl_128bit_datatype_vec3; + packet.data._v4f[0] = vg_randf()*2.0f-1.0f; + packet.data._v4f[1] = vg_randf()*2.0f-1.0f; + packet.data._v4f[2] = vg_randf()*2.0f-1.0f; + m4x3_mulv( zone->transform, packet.data._v4f, packet.data._v4f ); - logic_bricks_send_packet( world, &packet ); + logic_bricks_send_packet( world, &packet ); + } continue; } @@ -847,11 +863,17 @@ VG_STATIC void world_update( world_instance *world, v3f pos ) logic_bricks_send_packet( world, &packet ); } + + vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f}, + { 1.0f, 1.0f, 1.0f}}, + 0xff00ff00 ); + } + else + { + vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f}, + { 1.0f, 1.0f, 1.0f}}, + 0xff0000ff ); } - - vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f}, - { 1.0f, 1.0f, 1.0f}}, - 0xff00ff00 ); } if( k_debug_light_indices ) diff --git a/world_gen.h b/world_gen.h index cbf4728..a8f148c 100644 --- a/world_gen.h +++ b/world_gen.h @@ -241,6 +241,12 @@ VG_STATIC void world_ents_allocate( world_instance *world ) } logic_bricks_world_gen_allocate( world ); + + world->trigger_bh = bh_create( world_global.generic_heap, + &bh_system_triggers, + world, + world->trigger_count, + 1 ); } VG_STATIC void world_pct_spawn( world_instance *world, mdl_node *pnode ) diff --git a/world_logic_bricks.h b/world_logic_bricks.h index 2df236a..c4d5358 100644 --- a/world_logic_bricks.h +++ b/world_logic_bricks.h @@ -492,4 +492,68 @@ VG_STATIC void logic_bricks_world_gen_allocate( world_instance *world ) } } + + + + +/* + * BVH implementation + * ---------------------------------------------------------------------------- + */ + +VG_STATIC void trigger_bh_expand_bound( void *user, boxf bound, u32 item_index ) +{ + world_instance *world = user; + struct trigger_zone *trigger = &world->triggers[ item_index ]; + + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){ 1.0f, 1.0f, 1.0f} ); + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){ 1.0f, 1.0f,-1.0f} ); + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){ 1.0f,-1.0f, 1.0f} ); + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){ 1.0f,-1.0f,-1.0f} ); + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){-1.0f, 1.0f, 1.0f} ); + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){-1.0f, 1.0f,-1.0f} ); + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){-1.0f,-1.0f, 1.0f} ); + m4x3_expand_aabb_point( trigger->transform, bound, (v3f){-1.0f,-1.0f,-1.0f} ); +} + +VG_STATIC float trigger_bh_centroid( void *user, u32 item_index, int axis ) +{ + world_instance *world = user; + struct trigger_zone *trigger = &world->triggers[ item_index ]; + + return trigger->transform[3][axis]; +} + +VG_STATIC void trigger_bh_swap( void *user, u32 ia, u32 ib ) +{ + world_instance *world = user; + struct trigger_zone *a = &world->triggers[ ia ], + *b = &world->triggers[ ib ], + temp; + + temp = *a; + *a = *b; + *b = temp; +} + +VG_STATIC void trigger_bh_debug( void *user, u32 item_index ) +{ + world_instance *world = user; + struct trigger_zone *zone = &world->triggers[ item_index ]; + + vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f}, + { 1.0f, 1.0f, 1.0f}}, + 0xff00ff00 ); +} + +VG_STATIC bh_system bh_system_triggers = +{ + .expand_bound = trigger_bh_expand_bound, + .item_centroid = trigger_bh_centroid, + .item_closest = NULL, + .item_swap = trigger_bh_swap, + .item_debug = trigger_bh_debug, + .cast_ray = NULL +}; + #endif /* WORLD_LOGIC_BRICKS_H */