summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-12-11 19:33:50 +0100
committerMatthias Schiffer <matthias@gamezock.de>2009-12-11 19:33:50 +0100
commit7f41448de9242dc21582231e29cc0e8b25d0f159 (patch)
treed3fe4d2764834c9ae61dc305aeb9571b31271ea0
parenta27f871aecf07d4624ad443e763e25d684536ba8 (diff)
downloadc3d-7f41448de9242dc21582231e29cc0e8b25d0f159.tar
c3d-7f41448de9242dc21582231e29cc0e8b25d0f159.zip
Einige Verbesserungen am BSPTree und der DisplayClass
-rw-r--r--BSPTree.cpp26
-rw-r--r--BSPTree.h7
-rw-r--r--DisplayClass.cpp31
-rw-r--r--DisplayClass.h6
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;
diff --git a/BSPTree.h b/BSPTree.h
index 50f18df..c005507 100644
--- a/BSPTree.h
+++ b/BSPTree.h
@@ -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_*/