Browse Source

Slight changes in octree/bvh

Godzil 4 years ago
parent
commit
5651570c2b
2 changed files with 99 additions and 105 deletions
  1. 24 25
      source/worldoptimiser/bvhoptimisation.cpp
  2. 75 80
      source/worldoptimiser/octreeoptimisation.cpp

+ 24 - 25
source/worldoptimiser/bvhoptimisation.cpp

@@ -8,6 +8,7 @@
  */
 #include <worldoptimiser.h>
 #include <cube.h>
+#include <objfile.h>
 #include <transformation.h>
 
 void BVHOptimisation::makeTree(Group *leaf, int depth)
@@ -63,37 +64,35 @@ void BVHOptimisation::makeTree(Group *leaf, int depth)
     for (i = 0 ; i < leaf->getObjectCount(); i++)
     {
         Shape *shp = leaf->getObject(i);
-        if ((shp->getType() != Shape::GROUP) && (shp->getType() != Shape::OBJFILE))
-        {
-            BoundingBox objBB = shp->getBounds();
+        BoundingBox objBB = shp->getBounds();
 
-            for (sliceIdx = 0 ; sliceIdx < 2 ; sliceIdx++)
+        for (sliceIdx = 0 ; sliceIdx < 2 ; sliceIdx++)
+        {
+            if (SlicesBB[sliceIdx].fitsIn(objBB))
             {
-                if (SlicesBB[sliceIdx].fitsIn(objBB))
+                if (Slices[sliceIdx] == nullptr)
                 {
-                    if (Slices[sliceIdx] == nullptr)
-                    {
-                        char name[32];
-                        snprintf(name, 32, "%d_Slice %d", depth, sliceIdx);
-                        //for(int j=0; j < depth; j++) { printf(" "); }
-                        //printf("%s\n", name);
-                        Slices[sliceIdx] = new Group(name);
-
-                        Slices[sliceIdx]->setBounds(SlicesBB[sliceIdx]);
-                    }
-
-                    Slices[sliceIdx]->addObject(shp);
-                    leaf->removeObject(shp);
-
-                    i -= 1;
-                    break;
+                    char name[32];
+                    snprintf(name, 32, "%d_Slice %d", depth, sliceIdx);
+                    Slices[sliceIdx] = new Group(name);
+
+                    Slices[sliceIdx]->setBounds(SlicesBB[sliceIdx]);
                 }
+
+                Slices[sliceIdx]->addObject(shp);
+                leaf->removeObject(shp);
+
+                i -= 1;
+                break;
             }
         }
-        else
+        if (shp->getType() == Shape::GROUP)
+        {
+            this->makeTree((Group *)shp, depth + 1);
+        }
+        else if (shp->getType() == Shape::OBJFILE)
         {
-            leaf->removeObject(shp);
-            i -= 1;
+            this->makeTree((Group *)((OBJFile *)shp)->getBaseGroup(), depth + 1);
         }
     }
 
@@ -138,7 +137,7 @@ void BVHOptimisation::run()
     this->moveInfiniteObjects();
 
     /* Then let's have some fun! */
-    this->moveAllObjects();
+    //this->moveAllObjects();
 
     /* Now.. The fun start ! */
     makeTree(this->root, 0);

+ 75 - 80
source/worldoptimiser/octreeoptimisation.cpp

@@ -26,109 +26,104 @@ void OctreeOptimisation::makeTree(Group *leaf, int depth)
 
     int i;
 
-    //if (leaf->getObjectCount() > 2)
-    {
-        /* Split the main bounding box into 8 boxes */
-        octantBB[0] | Point(rootBB.min.x, rootBB.min.y, rootBB.min.z);
-        octantBB[0] | Point(midX, midY, midZ);
 
-        octantBB[1] | Point(midX, rootBB.min.y, rootBB.min.z);
-        octantBB[1] | Point(rootBB.max.x, midY, midZ);
+    /* Split the main bounding box into 8 boxes */
+    octantBB[0] | Point(rootBB.min.x, rootBB.min.y, rootBB.min.z);
+    octantBB[0] | Point(midX, midY, midZ);
 
-        octantBB[2] | Point(rootBB.min.x, midY, rootBB.min.z);
-        octantBB[2] | Point(midX, rootBB.max.y, midZ);
+    octantBB[1] | Point(midX, rootBB.min.y, rootBB.min.z);
+    octantBB[1] | Point(rootBB.max.x, midY, midZ);
 
-        octantBB[3] | Point(midX, midY, rootBB.min.z);
-        octantBB[3] | Point(rootBB.max.x, rootBB.max.y, midZ);
+    octantBB[2] | Point(rootBB.min.x, midY, rootBB.min.z);
+    octantBB[2] | Point(midX, rootBB.max.y, midZ);
 
-        octantBB[4] | Point(rootBB.min.x, midY, midZ);
-        octantBB[4] | Point(midX, rootBB.max.y, rootBB.max.z);
+    octantBB[3] | Point(midX, midY, rootBB.min.z);
+    octantBB[3] | Point(rootBB.max.x, rootBB.max.y, midZ);
 
-        octantBB[5] | Point(midX, midY, midZ);
-        octantBB[5] | Point(rootBB.max.x, rootBB.max.y, rootBB.max.z);
+    octantBB[4] | Point(rootBB.min.x, midY, midZ);
+    octantBB[4] | Point(midX, rootBB.max.y, rootBB.max.z);
 
-        octantBB[6] | Point(rootBB.min.x, rootBB.min.y, midZ);
-        octantBB[6] | Point(midX, midY, rootBB.max.z);
+    octantBB[5] | Point(midX, midY, midZ);
+    octantBB[5] | Point(rootBB.max.x, rootBB.max.y, rootBB.max.z);
 
-        octantBB[7] | Point(midX, rootBB.min.y, midZ);
-        octantBB[7] | Point(rootBB.max.x, midY, rootBB.max.z);
+    octantBB[6] | Point(rootBB.min.x, rootBB.min.y, midZ);
+    octantBB[6] | Point(midX, midY, rootBB.max.z);
 
-        for (octantIdx = 0 ; octantIdx < 8 ; octantIdx++)
-        {
-            octants[octantIdx] = nullptr;
-        }
+    octantBB[7] | Point(midX, rootBB.min.y, midZ);
+    octantBB[7] | Point(rootBB.max.x, midY, rootBB.max.z);
 
-        for (i = 0 ; i < leaf->getObjectCount(); i++)
-        {
-            Shape *shp = leaf->getObject(i);
+    for (octantIdx = 0 ; octantIdx < 8 ; octantIdx++)
+    {
+        octants[octantIdx] = nullptr;
+    }
 
-            BoundingBox objBB = shp->getBounds();
-            //if ((shp->getType() != Shape::GROUP) && (shp->getType() != Shape::OBJFILE))
-            for (octantIdx = 0 ; octantIdx < 8 ; octantIdx++)
+    for (i = 0 ; i < leaf->getObjectCount(); i++)
+    {
+        Shape *shp = leaf->getObject(i);
+
+        BoundingBox objBB = shp->getBounds();
+        //if ((shp->getType() != Shape::GROUP) && (shp->getType() != Shape::OBJFILE))
+        for (octantIdx = 0 ; octantIdx < 8 ; octantIdx++)
+        {
+            if (octantBB[octantIdx].fitsIn(objBB))
             {
-                if (octantBB[octantIdx].fitsIn(objBB))
+                if (octants[octantIdx] == nullptr)
                 {
-                    if (octants[octantIdx] == nullptr)
-                    {
-                        char name[32];
-                        snprintf(name, 32, "%d_Quadrant %d", depth, octantIdx);
-                        octants[octantIdx] = new Group(name);
+                    char name[32];
+                    snprintf(name, 32, "%d_Quadrant %d", depth, octantIdx);
+                    octants[octantIdx] = new Group(name);
 
-                        octants[octantIdx]->setBounds(octantBB[octantIdx]);
-                    }
+                    octants[octantIdx]->setBounds(octantBB[octantIdx]);
+                }
 
-                    octants[octantIdx]->addObject(shp);
-                    leaf->removeObject(shp);
+                octants[octantIdx]->addObject(shp);
+                leaf->removeObject(shp);
 
-                    i -= 1;
-                    break;
-                }
-            }
-            if (shp->getType() == Shape::GROUP)
-            {
-                this->makeTree((Group *)shp, depth + 1);
-            }
-            if (shp->getType() == Shape::OBJFILE)
-            {
-                this->makeTree((Group *)((OBJFile *)shp)->getBaseGroup(), depth + 1);
+                i -= 1;
+                break;
             }
         }
+        if (shp->getType() == Shape::GROUP)
+        {
+            this->makeTree((Group *)shp, depth + 1);
+        }
+        if (shp->getType() == Shape::OBJFILE)
+        {
+            this->makeTree((Group *)((OBJFile *)shp)->getBaseGroup(), depth + 1);
+        }
+    }
 
-        //if (depth < 1)
+    /* Now add the quadrant to the root and recurse in it */
+    for (octantIdx = 0 ; octantIdx < 8 ; octantIdx++)
+    {
+        if (octants[octantIdx] != nullptr)
         {
-            /* Now add the quadrant to the root and recurse in it */
-            for (octantIdx = 0 ; octantIdx < 8 ; octantIdx++)
-            {
-                if (octants[octantIdx] != nullptr)
-                {
-                    this->makeTree(octants[octantIdx], depth + 1);
+            this->makeTree(octants[octantIdx], depth + 1);
 
-                    octants[octantIdx]->updateBoundingBox();
+            octants[octantIdx]->updateBoundingBox();
 
-                    leaf->addObject(octants[octantIdx]);
+            leaf->addObject(octants[octantIdx]);
 #if 0
-                    Cube *cb = new Cube();
-                    double sx = octantBB[octantIdx].max.x - octantBB[octantIdx].min.x;
-                    double sy = octantBB[octantIdx].max.y - octantBB[octantIdx].min.y;
-                    double sz = octantBB[octantIdx].max.z - octantBB[octantIdx].min.z;
-
-                    cb->setTransform(translation(octantBB[octantIdx].min.x, octantBB[octantIdx].min.y,
-                                                 octantBB[octantIdx].min.z) * scaling(sx, sy, sz));
-                    cb->material.colour = Colour(0.01, 0.01, 0);
-                    cb->materialSet = true;
-                    cb->dropShadow = false;
-                    cb->material.ambient = 0.1;
-                    cb->material.reflective = 0;
-                    cb->material.transparency = 0.95;
-                    cb->material.refractiveIndex = 1;
-                    cb->material.specular = 0;
-                    leaf->addObject(cb);
-
-                    printf("%s: %d objs\n", octants[octantIdx]->getName(),
-                           octants[octantIdx]->getObjectCount());
+            Cube *cb = new Cube();
+            double sx = octantBB[octantIdx].max.x - octantBB[octantIdx].min.x;
+            double sy = octantBB[octantIdx].max.y - octantBB[octantIdx].min.y;
+            double sz = octantBB[octantIdx].max.z - octantBB[octantIdx].min.z;
+
+            cb->setTransform(translation(octantBB[octantIdx].min.x, octantBB[octantIdx].min.y,
+                                         octantBB[octantIdx].min.z) * scaling(sx, sy, sz));
+            cb->material.colour = Colour(0.01, 0.01, 0);
+            cb->materialSet = true;
+            cb->dropShadow = false;
+            cb->material.ambient = 0.1;
+            cb->material.reflective = 0;
+            cb->material.transparency = 0.95;
+            cb->material.refractiveIndex = 1;
+            cb->material.specular = 0;
+            leaf->addObject(cb);
+
+            printf("%s: %d objs\n", octants[octantIdx]->getName(),
+                   octants[octantIdx]->getObjectCount());
 #endif
-                }
-            }
         }
     }
 }