diff options
-rw-r--r-- | BSPTree.cpp | 26 | ||||
-rw-r--r-- | BSPTree.h | 7 | ||||
-rw-r--r-- | DisplayClass.cpp | 31 | ||||
-rw-r--r-- | DisplayClass.h | 6 |
4 files changed, 58 insertions, 12 deletions
diff --git a/BSPTree.cpp b/BSPTree.cpp index 3254446..6da2531 100644 --- a/BSPTree.cpp +++ b/BSPTree.cpp @@ -54,11 +54,11 @@ void BSPTree::Plane::partition(const Triangle &t, std::list<Triangle> *front, st BSPTree::BSPTree(const std::list<Triangle> &triangles) : frontTree(0), backTree(0) { - if(triangles.empty()) - return; - const Triangle *planeT = findNearestTriangle(triangles, findCenter(triangles)); + if(!planeT) + return; + plane = Plane(*planeT); std::list<Triangle> front, back; @@ -93,6 +93,26 @@ BSPTree::BSPTree(const std::list<Triangle> &triangles) : frontTree(0), backTree( backTree = new BSPTree(back); } +BSPTree& BSPTree::operator=(const BSPTree &tree) { + if(frontTree) { + delete frontTree; + frontTree = 0; + } + + if(backTree) { + delete backTree; + backTree = 0; + } + + plane = tree.plane; + triangles = tree.triangles; + + if(tree.frontTree) + frontTree = new BSPTree(*tree.frontTree); + + if(tree.backTree) + backTree = new BSPTree(*tree.backTree); +} vmml::vec3f BSPTree::findCenter(const std::list<Triangle> &triangles) { vmml::vec3f v; @@ -69,6 +69,11 @@ class BSPTree { public: BSPTree(const std::list<Triangle> &triangles); + + BSPTree(const BSPTree &tree) : frontTree(0), backTree(0) { + *this = tree; + } + virtual ~BSPTree() { if(frontTree) delete frontTree; @@ -77,6 +82,8 @@ class BSPTree { delete backTree; } + BSPTree& operator=(const BSPTree &tree); + template<typename T> void visit(const T& visitor, const vmml::vec3f &p) { doVisit<const T>(visitor, p); diff --git a/DisplayClass.cpp b/DisplayClass.cpp index b47fc48..2c4489b 100644 --- a/DisplayClass.cpp +++ b/DisplayClass.cpp @@ -1,11 +1,10 @@ #include "DisplayClass.h" #include "gl.h" -#include "BSPTree.h" DisplayClass::Renderer DisplayClass::renderer; -DisplayClass::DisplayClass() { +DisplayClass::DisplayClass() : angle(0) { cubehole0 = Cubehole(3.0, 6.0, 3.0, 0.0, 0.0, 0.0, 2.5, 2.5, vmml::vec4f(1.0, 0.85, 0.06, 0.5), vmml::vec4f(1.0, 0.85, 0.06, 0.5), @@ -36,11 +35,27 @@ DisplayClass::DisplayClass() { vmml::vec4f(1.0, 0.0, 0.06, 0.5), vmml::vec4f(1.0, 0.0, 0.06, 0.5), vmml::vec4f(1.0, 0.0, 0.06, 0.5)); + + std::list<Triangle> triangles = cubehole0.getTriangles(); + std::list<Triangle> t1 = cubehole1.getTriangles(); + triangles.splice(triangles.end(), t1); + std::list<Triangle> t2 = cubehole2.getTriangles(); + triangles.splice(triangles.end(), t2); + std::list<Triangle> t3 = cubehole3.getTriangles(); + triangles.splice(triangles.end(), t3); + std::list<Triangle> t4 = cubehole4.getTriangles(); + triangles.splice(triangles.end(), t4); + std::list<Triangle> t5 = cubehole5.getTriangles(); + triangles.splice(triangles.end(), t5); + + tree = new BSPTree(triangles); +} + +DisplayClass::~DisplayClass() { + delete tree; } -void DisplayClass::renderScene(unsigned long delta) -{ - static float angle = 0.0; +void DisplayClass::renderScene(unsigned long delta) { angle += delta*0.025; if(angle >= 360) angle -= 360; @@ -55,7 +70,7 @@ void DisplayClass::renderScene(unsigned long delta) glRotatef(-angle*5, 1.0, 1.0, 1.0); - std::list<Triangle> triangles = cubehole0.getTriangles(); + /*std::list<Triangle> triangles = cubehole0.getTriangles(); std::list<Triangle> t1 = cubehole1.getTriangles(); triangles.splice(triangles.end(), t1); std::list<Triangle> t2 = cubehole2.getTriangles(); @@ -67,7 +82,7 @@ void DisplayClass::renderScene(unsigned long delta) std::list<Triangle> t5 = cubehole5.getTriangles(); triangles.splice(triangles.end(), t5); - BSPTree tree(triangles); + BSPTree tree(triangles);*/ vmml::mat4f transform, inverseTransform; glGetFloatv(GL_MODELVIEW_MATRIX, transform.array); @@ -77,7 +92,7 @@ void DisplayClass::renderScene(unsigned long delta) vmml::vec3f viewPoint = inverseTransform*vmml::vec3f::ZERO; glBegin(GL_TRIANGLES); - tree.visit(renderer, viewPoint); + tree->visit(renderer, viewPoint); glEnd(); glFlush(); diff --git a/DisplayClass.h b/DisplayClass.h index 406239a..693587d 100644 --- a/DisplayClass.h +++ b/DisplayClass.h @@ -2,11 +2,13 @@ #define _DISPLAYCLASS_H_ #include "Cubehole.h" +#include "BSPTree.h" class DisplayClass
{
public: DisplayClass(); + virtual ~DisplayClass(); void renderScene(unsigned long delta); @@ -19,7 +21,9 @@ class DisplayClass static Renderer renderer; - Cubehole cubehole0, cubehole1, cubehole2, cubehole3, cubehole4, cubehole5;
+ float angle; + Cubehole cubehole0, cubehole1, cubehole2, cubehole3, cubehole4, cubehole5; + BSPTree *tree;
};
#endif /*_DISPLAYCLASS_H_*/ |