{
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;
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 );
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; i<world->trigger_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; j<random_ticks; j++ )
+ if( zone->classtype == 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; j<random_ticks; j++ )
+ {
+ logic_packet packet;
+ packet.location = zone->target_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;
}
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 )
}
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 )
}
}
+
+
+
+
+/*
+ * 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 */