diff options
-rw-r--r-- | BSPGenerator.cpp | 2 | ||||
-rw-r--r-- | BSPGenerator.h | 9 | ||||
-rw-r--r-- | BSPTree.cpp | 25 | ||||
-rw-r--r-- | BSPTree.h | 58 | ||||
-rw-r--r-- | CMakeLists.txt | 3 | ||||
-rw-r--r-- | DisplayClass.cpp | 5 | ||||
-rw-r--r-- | Triangle.h | 4 | ||||
-rw-r--r-- | Vector.h | 8 | ||||
-rw-r--r-- | Vertex.h | 4 |
9 files changed, 104 insertions, 14 deletions
diff --git a/BSPGenerator.cpp b/BSPGenerator.cpp deleted file mode 100644 index a4cab6e..0000000 --- a/BSPGenerator.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "BSPGenerator.h" - diff --git a/BSPGenerator.h b/BSPGenerator.h deleted file mode 100644 index f6195e8..0000000 --- a/BSPGenerator.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _BSPGENERATOR_H_ -#define _BSPGENERATOR_H_ - -class BSPGenerator { - public: - -}; - -#endif /* _BSPGENERATOR_H_ */ diff --git a/BSPTree.cpp b/BSPTree.cpp new file mode 100644 index 0000000..b30c907 --- /dev/null +++ b/BSPTree.cpp @@ -0,0 +1,25 @@ +#include "BSPTree.h" +#include <iostream> + +BSPTree::BSPTree(const std::list<Triangle> &t) { + Vertex center = findCenter(t); + std::cout << "Center at (" << center.getX() << ", " << center.getY() << ", " << center.getZ() << std::endl; + + +} + + +Vertex BSPTree::findCenter(const std::list<Triangle> &triangles) { + Vector v; + + for(std::list<Triangle>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) { + v += Vector(t->getCenter()); + } + + return v/triangles.size(); +} + +const Triangle* BSPTree::findNearestTriangle(const std::list<Triangle> &triangles, const Vertex &v) { + Triangle *current = 0; + float distance; +} @@ -3,10 +3,68 @@ #include "Triangle.h" #include <list> +#include <cmath> class BSPTree { private: + class Plane { + public: + Plane() : d(0) {} + Plane(const Vector &n, float d0) : normal(n), d(d0) {} + bool contains(Vertex v) { + return (fabsf(normal.dot(Vector(v)) - d) < 1E-6); + } + + bool isBehind(Vertex v) { + return (normal.dot(Vector(v)) - d) < 0; + } + + const Vector& getNormal() { + return normal; + } + + private: + Vector normal; + float d; + }; + + public: + BSPTree(const std::list<Triangle> &triangles); + + template<typename T> + void visit(T& visitor, const Vector &v) { + if(plane.getNormal().dot(v) > 0) { + if(frontTree) + frontTree->visit(visitor, v); + + for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { + visitor(*t); + } + + if(backTree) + backTree->visit(visitor, v); + } + else { + if(backTree) + backTree->visit(visitor, v); + + for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { + visitor(*t); + } + + if(frontTree) + frontTree->visit(visitor, v); + } + } + + private: + Plane plane; + std::list<Triangle> triangles; + BSPTree *frontTree, *backTree; + + static Vertex findCenter(const std::list<Triangle> &triangles); + static const Triangle* findNearestTriangle(const std::list<Triangle> &triangles, const Vertex &v); }; #endif /* _BSPTREE_H_ */ diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f656ee..de4bf01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,7 @@ find_package(OpenGL REQUIRED) include_directories(${OPENGL_INCLUDE_DIR}) add_executable(c3d - BSPGenerator.cpp BSPGenerator.h - BSPTree.h + BSPTree.cpp BSPTree.h Color.h Cubehole.cpp Cubehole.h Cuboid.cpp Cuboid.h diff --git a/DisplayClass.cpp b/DisplayClass.cpp index 2741fce..5c97cc4 100644 --- a/DisplayClass.cpp +++ b/DisplayClass.cpp @@ -2,7 +2,8 @@ #include "Matrix.h"
#include "gl.h" #include <algorithm> -#include "Trapezocube.h"
+#include "Trapezocube.h" +#include "BSPTree.h"
DisplayClass::DisplayClass() {
@@ -53,6 +54,8 @@ void DisplayClass::renderScene(unsigned long delta) }*/ std::list<Triangle> ct = cubeing.getTriangles(m); triangles.splice(triangles.end(), ct); + + BSPTree tree(triangles); glLoadIdentity();
@@ -36,6 +36,10 @@ class Triangle } } + Vertex getCenter() const { + return (Vector(v[0])+Vector(v[1])+Vector(v[2]))/3; + } + private: Vertex v[3]; Color c; @@ -12,6 +12,14 @@ class Vector : public Vertex { return Vector(x+v.x, y+v.y, z+v.z); } + Vector& operator+=(const Vector &v) { + x += v.x; + y += v.y; + z += v.z; + + return *this; + } + Vector operator*(float f) const { return Vector(x*f, y*f, z*f); } @@ -10,6 +10,10 @@ class Vertex float getY() const {return y;} float getZ() const {return z;} + float distanceSq(const Vertex &v) const { + return x*v.x + y*v.y + z*v.z; + } + protected: float x, y, z; }; |