added BIH_GetTriangleListForBox function
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 25 Jun 2010 11:53:41 +0000 (11:53 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 25 Jun 2010 11:53:41 +0000 (11:53 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10250 d7cf8633-e32d-0410-b094-e92efae38249

bih.c
bih.h

diff --git a/bih.c b/bih.c
index fdda1d6..58cc071 100644 (file)
--- a/bih.c
+++ b/bih.c
@@ -141,3 +141,51 @@ int BIH_Build(bih_t *bih, int numleafs, bih_leaf_t *leafs, int maxnodes, bih_nod
        bih->rootnode = BIH_BuildNode(bih, bih->numleafs, bih->leafsort, bih->mins, bih->maxs);
        return bih->error;
 }
+
+static void BIH_GetTriangleListForBox_Node(const bih_t *bih, int nodenum, int maxtriangles, int *trianglelist, int *numtrianglespointer, const float *mins, const float *maxs)
+{
+       int axis;
+       bih_node_t *node;
+       bih_leaf_t *leaf;
+       while (nodenum >= 0)
+       {
+               node = bih->nodes + nodenum;
+               axis = node->type - BIH_SPLITX;
+               if (mins[axis] < node->backmax)
+               {
+                       if (maxs[axis] > node->frontmin)
+                               BIH_GetTriangleListForBox_Node(bih, node->front, maxtriangles, trianglelist, numtrianglespointer, mins, maxs);
+                       nodenum = node->back;
+                       continue;
+               }
+               if (maxs[axis] > node->frontmin)
+               {
+                       nodenum = node->front;
+                       continue;
+               }
+               // fell between the child groups, nothing here
+               return;
+       }
+       leaf = bih->leafs + (-1-nodenum);
+       if (mins[0] > leaf->maxs[0] || maxs[0] < leaf->mins[0]
+        || mins[1] > leaf->maxs[1] || maxs[1] < leaf->mins[1]
+        || mins[2] > leaf->maxs[2] || maxs[2] < leaf->mins[2])
+               return;
+       switch(leaf->type)
+       {
+       case BIH_RENDERTRIANGLE:
+               if (*numtrianglespointer >= maxtriangles)
+                       return;
+               trianglelist[(*numtrianglespointer)++] = leaf->itemindex;
+               break;
+       default:
+               break;
+       }
+}
+
+int BIH_GetTriangleListForBox(const bih_t *bih, int maxtriangles, int *trianglelist, const float *mins, const float *maxs)
+{
+       int numtriangles = 0;
+       BIH_GetTriangleListForBox_Node(bih, 0, maxtriangles, trianglelist, &numtriangles, mins, maxs);
+       return numtriangles;
+}
diff --git a/bih.h b/bih.h
index 48a33d1..194a653 100644 (file)
--- a/bih.h
+++ b/bih.h
@@ -80,4 +80,6 @@ bih_t;
 
 int BIH_Build(bih_t *bih, int numleafs, bih_leaf_t *leafs, int maxnodes, bih_node_t *nodes, int *temp_leafsort, int *temp_leafsortscratch);
 
+int BIH_GetTriangleListForBox(const bih_t *bih, int maxtriangles, int *trianglelist, const float *mins, const float *maxs);
+
 #endif