walk manifold clipping
[carveJwlIkooP6JGAAIwe30JlM.git] / scene.h
diff --git a/scene.h b/scene.h
index ff970007dfb5fce17dbfc9c03f2a3be2b27c9bbb..42ca838cc7e28ca3a5e1468f3c9997374ba06e95 100644 (file)
--- a/scene.h
+++ b/scene.h
@@ -1047,6 +1047,56 @@ static int bvh_raycast( scene *s, v3f co, v3f dir, ray_hit *hit )
    return count;
 }
 
+static int bvh_select_triangles( scene *s, boxf box, u32 *triangles, int len )
+{
+   vg_line_boxf( box, 0xffffff00 );
+
+   /* TODO: use this stack system on the raycast function */
+   int count = 0;
+   u32 stack[100];
+   u32 depth = 1;
+
+   stack[0] = 0;
+   stack[1] = s->bvh.nodes[0].il;
+   stack[2] = s->bvh.nodes[0].ir;
+   
+   while(depth)
+   {
+      bvh_node *inode = &s->bvh.nodes[ stack[depth] ];
+      if( box_overlap( inode->bbx, box ) )
+      {
+         if( inode->count )
+         {
+            if( count + inode->count >= len )
+               return count;
+
+            for( u32 i=0; i<inode->count; i++ )
+               triangles[ count ++ ] = (inode->start+i)*3;
+
+            depth --;
+         }
+         else
+         {
+            if( depth+1 >= vg_list_size(stack) )
+            {
+               vg_error( "Maximum stack reached!" );
+               return count;
+            }
+
+            stack[depth] = inode->il;
+            stack[depth+1] = inode->ir;
+            depth ++;
+         }
+      }
+      else
+      {
+         depth --;
+      }
+   }
+
+   return count;
+}
+
 static int bvh_scene_sample_node_h( scene *s, u32 inode, v3f pos, v3f norm )
 {
    bvh_node *node = &s->bvh.nodes[ inode ];