bricks
authorhgn <hgodden00@gmail.com>
Mon, 13 Mar 2023 16:16:35 +0000 (16:16 +0000)
committerhgn <hgodden00@gmail.com>
Mon, 13 Mar 2023 16:16:35 +0000 (16:16 +0000)
bvh.h
maps_src/mp_home.mdl
world.h
world_gen.h
world_logic_bricks.h

diff --git a/bvh.h b/bvh.h
index 52ce24ab75e39175923ab95a00e508882349afe8..94ea69bdf91d95c8ea86b0f79a44c3a33b940848 100644 (file)
--- 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 );
index 29c0ae5ac5722e3c2f3835026b1fdd70968ddd4d..4684fc7d8401c9f00cd02bcc503dde19a0a4b915 100644 (file)
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 4352b6e6031bd38f608b24061591e93e81ad72e1..106b8247334270807d2e8112095ae3fde022580b 100644 (file)
--- 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; 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;
       }
 
@@ -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 )
index cbf47287f9d1de2881a4a60de7b0287119281e74..a8f148c74f293e0a8b33c4eb878ed1320cfde4ad 100644 (file)
@@ -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 )
index 2df236a6457b670fd4e520f0083488c34a592327..c4d5358db05980376588053230928b822d1df647 100644 (file)
@@ -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 */