summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--BSPGenerator.cpp2
-rw-r--r--BSPGenerator.h9
-rw-r--r--BSPTree.cpp25
-rw-r--r--BSPTree.h58
-rw-r--r--CMakeLists.txt3
-rw-r--r--DisplayClass.cpp81
-rw-r--r--Triangle.h4
-rw-r--r--Vector.h8
-rw-r--r--Vertex.h4
9 files changed, 141 insertions, 53 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;
+}
diff --git a/BSPTree.h b/BSPTree.h
index 345f143..dc7e2df 100644
--- a/BSPTree.h
+++ b/BSPTree.h
@@ -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 35083d9..cefa6ae 100644
--- a/DisplayClass.cpp
+++ b/DisplayClass.cpp
@@ -1,24 +1,25 @@
#include "DisplayClass.h"
-#include "Matrix.h"
+#include "Matrix.h"
#include "gl.h"
//#include <algorithm>
-#include "Trapezocube.h"
-
-
-DisplayClass::DisplayClass() {
- static const float pos[5] = {-3.0, -2.0, 0.0, 2.0, 3.0};
-
- for(int i = 0; i < 5; ++i) {
- for(int j = 0; j < 5; ++j) {
- for(int k = 0; k < 5; ++k) {
- cubes[i][j][k].setSize(0.5, 0.5, 0.5);
- cubes[i][j][k].setPos(pos[k], pos[j], pos[i]);
- }
- }
- }
-}
-
-void DisplayClass::renderScene(unsigned long delta)
+#include "Trapezocube.h"
+#include "BSPTree.h"
+
+
+DisplayClass::DisplayClass() {
+ static const float pos[5] = {-3.0, -2.0, 0.0, 2.0, 3.0};
+
+ for(int i = 0; i < 5; ++i) {
+ for(int j = 0; j < 5; ++j) {
+ for(int k = 0; k < 5; ++k) {
+ cubes[i][j][k].setSize(0.5, 0.5, 0.5);
+ cubes[i][j][k].setPos(pos[k], pos[j], pos[i]);
+ }
+ }
+ }
+}
+
+void DisplayClass::renderScene(unsigned long delta)
{
//Cubehole cubeing(3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5);
Trapezocube cubeing1(11.0, 10.0, 10.0, 0.5, 0.0, 0.0, 1.75, 0);
@@ -29,32 +30,32 @@ void DisplayClass::renderScene(unsigned long delta)
Trapezocube cubeing6(6.0, 5.0, 5.0, 0.5, 0.0, 0.0, 4.25, 90);
Trapezocube cubeing7(5.0, 4.0, 4.0, 0.5, 0.0, 0.0, 4.75, 180);
Trapezocube cubeing8(4.0, 3.0, 3.0, 0.5, 0.0, 0.0, 5.25, 270);
- static float angle = 0.0;
- angle += delta*0.025;
- if(angle >= 360)
- angle -= 360;
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+ static float angle = 0.0;
+ angle += delta*0.025;
+ if(angle >= 360)
+ angle -= 360;
+
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ //Matrix p(GL_PROJECTION_MATRIX);
+ //Matrix m;
- //atrix p(GL_PROJECTION_MATRIX);
- //Matrix m;
-
std::list<Triangle> triangles;
- glLoadIdentity(); // Clean up matrix
- glTranslatef(0.0, 0.0, -30.0); // Then set up transformation
- glRotatef(angle, 0.0, 1.0, 0.0);
- glRotatef(angle*2, 1.0, 0.0, 0.0);
- glRotatef(angle*3, 0.0, 0.0, 1.0);
+ glLoadIdentity(); // Clean up matrix
+ glTranslatef(0.0, 0.0, -30.0); // Then set up transformation
+ glRotatef(angle, 0.0, 1.0, 0.0);
+ glRotatef(angle*2, 1.0, 0.0, 0.0);
+ glRotatef(angle*3, 0.0, 0.0, 1.0);
glRotatef(-angle*5, 1.0, 1.0, 1.0);
//m.store(); // Save current transformation
/*for(int i = 0; i < 5; ++i) {
- for(int j = 0; j < 5; ++j) {
+ for(int j = 0; j < 5; ++j) {
for(int k = 0; k < 5; ++k) {
- std::list<Triangle> ct = cubes[i][j][k].getTriangles(m);
- triangles.splice(triangles.end(), ct);
+ std::list<Triangle> ct = cubes[i][j][k].getTriangles(m);
+ triangles.splice(triangles.end(), ct);
}
}
}*/
@@ -74,14 +75,14 @@ void DisplayClass::renderScene(unsigned long delta)
triangles.splice(triangles.end(), ct7);
std::list<Triangle> ct8 = cubeing8.getTriangles();
triangles.splice(triangles.end(), ct8);
-
- //glLoadIdentity();
+
+ //glLoadIdentity();
glBegin(GL_TRIANGLES);
for(std::list<Triangle>::reverse_iterator t = triangles.rbegin(); t != triangles.rend(); ++t) {
t->render();
}
glEnd();
-
- glFlush();
-}
+
+ glFlush();
+}
diff --git a/Triangle.h b/Triangle.h
index 3ce5845..ddb9d97 100644
--- a/Triangle.h
+++ b/Triangle.h
@@ -53,6 +53,10 @@ class Triangle
}
}
+ Vertex getCenter() const {
+ return (Vector(v[0])+Vector(v[1])+Vector(v[2]))/3;
+ }
+
private:
Vertex v[3];
Color c;
diff --git a/Vector.h b/Vector.h
index cb55637..f03c4b4 100644
--- a/Vector.h
+++ b/Vector.h
@@ -13,6 +13,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);
}
diff --git a/Vertex.h b/Vertex.h
index c82bd59..665430d 100644
--- a/Vertex.h
+++ b/Vertex.h
@@ -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;
};