summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/BSPTree.cpp42
-rw-r--r--src/BSPTree.h21
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/Game.h2
-rw-r--r--src/Level.cpp26
-rw-r--r--src/Level.h8
-rw-r--r--src/Renderer.h27
-rw-r--r--src/TriangleRecord.h58
8 files changed, 114 insertions, 71 deletions
diff --git a/src/BSPTree.cpp b/src/BSPTree.cpp
index 91efb6d..7b23e79 100644
--- a/src/BSPTree.cpp
+++ b/src/BSPTree.cpp
@@ -24,18 +24,18 @@ namespace Zoom {
void BSPTree::partition(const TriangleRecord &t, std::list<TriangleRecord> *front, std::list<TriangleRecord> *back) const {
for(int i = 0; i < 3; ++i) {
- if(plane.contains(t.triangle.getVertex(i))) {
- const vmml::vec3f *v[3] = {&t.triangle.getVertex(i), &t.triangle.getVertex((i+1)%3), &t.triangle.getVertex((i+2)%3)};
+ if(plane.contains(t.getTriangle().getVertex(i))) {
+ const vmml::vec3f *v[3] = {&t.getTriangle().getVertex(i), &t.getTriangle().getVertex((i+1)%3), &t.getTriangle().getVertex((i+2)%3)};
vmml::vec3f is = plane.intersection(*v[1], *v[2]-*v[1]);
if(plane.isInFront(*v[1])) {
- front->push_back(TriangleRecord(Triangle(*v[0], *v[1], is, t.triangle.getColor()), t.data));
- back->push_back(TriangleRecord(Triangle(*v[0], is, *v[2], t.triangle.getColor()), t.data));
+ front->push_back(TriangleRecord(Triangle(*v[0], *v[1], is, t.getTriangle().getColor()), t.getData()));
+ back->push_back(TriangleRecord(Triangle(*v[0], is, *v[2], t.getTriangle().getColor()), t.getData()));
}
else {
- back->push_back(TriangleRecord(Triangle(*v[0], *v[1], is, t.triangle.getColor()), t.data));
- front->push_back(TriangleRecord(Triangle(*v[0], is, *v[2], t.triangle.getColor()), t.data));
+ back->push_back(TriangleRecord(Triangle(*v[0], *v[1], is, t.getTriangle().getColor()), t.getData()));
+ front->push_back(TriangleRecord(Triangle(*v[0], is, *v[2], t.getTriangle().getColor()), t.getData()));
}
return;
@@ -43,7 +43,7 @@ void BSPTree::partition(const TriangleRecord &t, std::list<TriangleRecord> *fron
}
for(int i = 0; i < 3; ++i) {
- const vmml::vec3f *v[3] = {&t.triangle.getVertex(i), &t.triangle.getVertex((i+1)%3), &t.triangle.getVertex((i+2)%3)};
+ const vmml::vec3f *v[3] = {&t.getTriangle().getVertex(i), &t.getTriangle().getVertex((i+1)%3), &t.getTriangle().getVertex((i+2)%3)};
if((plane.isInFront(*v[0]) && plane.isBehind(*v[1]) && plane.isBehind(*v[2]))
|| (plane.isBehind(*v[0]) && plane.isInFront(*v[1]) && plane.isInFront(*v[2]))) {
@@ -51,14 +51,14 @@ void BSPTree::partition(const TriangleRecord &t, std::list<TriangleRecord> *fron
vmml::vec3f is2 = plane.intersection(*v[0], *v[2]-*v[0]);
if(plane.isInFront(*v[0])) {
- front->push_back(TriangleRecord(Triangle(*v[0], is1, is2, t.triangle.getColor()), t.data));
- back->push_back(TriangleRecord(Triangle(is1, *v[1], is2, t.triangle.getColor()), t.data));
- back->push_back(TriangleRecord(Triangle(*v[1], *v[2], is2, t.triangle.getColor()), t.data));
+ front->push_back(TriangleRecord(Triangle(*v[0], is1, is2, t.getTriangle().getColor()), t.getData()));
+ back->push_back(TriangleRecord(Triangle(is1, *v[1], is2, t.getTriangle().getColor()), t.getData()));
+ back->push_back(TriangleRecord(Triangle(*v[1], *v[2], is2, t.getTriangle().getColor()), t.getData()));
}
else {
- back->push_back(TriangleRecord(Triangle(*v[0], is1, is2, t.triangle.getColor()), t.data));
- front->push_back(TriangleRecord(Triangle(is1, *v[1], is2, t.triangle.getColor()), t.data));
- front->push_back(TriangleRecord(Triangle(*v[1], *v[2], is2, t.triangle.getColor()), t.data));
+ back->push_back(TriangleRecord(Triangle(*v[0], is1, is2, t.getTriangle().getColor()), t.getData()));
+ front->push_back(TriangleRecord(Triangle(is1, *v[1], is2, t.getTriangle().getColor()), t.getData()));
+ front->push_back(TriangleRecord(Triangle(*v[1], *v[2], is2, t.getTriangle().getColor()), t.getData()));
}
return;
@@ -78,18 +78,18 @@ BSPTree::BSPTree(const std::list<TriangleRecord> &triangles) : frontTree(0), bac
std::list<TriangleRecord> front, back;
for(std::list<TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
- if(t->triangle.isDegenerate())
+ if(t->getTriangle().isDegenerate())
continue;
- if(plane.contains(t->triangle)) {
+ if(plane.contains(t->getTriangle())) {
this->triangles.push_back(*t);
continue;
}
- else if(plane.isInFront(t->triangle)) {
+ else if(plane.isInFront(t->getTriangle())) {
front.push_back(*t);
continue;
}
- else if(plane.isBehind(t->triangle)) {
+ else if(plane.isBehind(t->getTriangle())) {
back.push_back(*t);
continue;
}
@@ -131,7 +131,7 @@ vmml::vec3f BSPTree::findCenter(const std::list<TriangleRecord> &triangles) {
vmml::vec3f v;
for(std::list<TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
- v += t->triangle.getCenter();
+ v += t->getTriangle().getCenter();
}
return v/triangles.size();
@@ -142,13 +142,13 @@ const Triangle* BSPTree::findNearestTriangle(const std::list<TriangleRecord> &tr
float distanceSq;
for(std::list<TriangleRecord>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
- if(t->triangle.isDegenerate())
+ if(t->getTriangle().isDegenerate())
continue;
- float d = t->triangle.getCenter().squared_distance(v);
+ float d = t->getTriangle().getCenter().squared_distance(v);
if(!current || d < distanceSq) {
- current = &t->triangle;
+ current = &t->getTriangle();
distanceSq = d;
}
}
diff --git a/src/BSPTree.h b/src/BSPTree.h
index e5015d0..de4f2c6 100644
--- a/src/BSPTree.h
+++ b/src/BSPTree.h
@@ -21,6 +21,7 @@
#define ZOOM_BSPTREE_H_
#include "Triangle.h"
+#include "TriangleRecord.h"
#include "MathUtil.h"
#include <list>
@@ -33,26 +34,6 @@ namespace Zoom {
class BSPTree {
public:
- class TriangleData {
- protected:
- TriangleData() {}
-
- public:
- virtual ~TriangleData() {}
- };
-
- struct TriangleRecord {
- public:
- TriangleRecord(Triangle triangle0, boost::shared_ptr<TriangleData> data0)
- : triangle(triangle0), data(data0) {}
- TriangleRecord(Triangle triangle0) : triangle(triangle0) {}
- TriangleRecord() {}
-
- Triangle triangle;
- boost::shared_ptr<TriangleData> data;
- };
-
- public:
BSPTree(const std::list<TriangleRecord> &triangles);
BSPTree(const BSPTree &tree) : frontTree(0), backTree(0) {
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 6ec57fc..613e604 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -10,6 +10,7 @@ add_executable(zoom
ShadowVolume.cpp ShadowVolume.h
Texture.cpp Texture.h
Triangle.h
+ TriangleRecord.h
zoom.cpp
)
target_link_libraries(zoom ${Boost_LIBRARIES} ${OPENGL_gl_LIBRARY} ${GLEW_LIBRARY} ${GLPNG_LIBRARY} ${LIBXML2_LIBRARIES})
diff --git a/src/Game.h b/src/Game.h
index 103086d..b5c7ee7 100644
--- a/src/Game.h
+++ b/src/Game.h
@@ -57,7 +57,7 @@ class Game {
Renderer renderer;
boost::shared_ptr<Level> level;
- std::vector<BSPTree::TriangleRecord> triangles;
+ std::vector<TriangleRecord> triangles;
vmml::vec3f playerPos;
vmml::mat4f playerRotY;
diff --git a/src/Level.cpp b/src/Level.cpp
index 2867ab9..9b2aa78 100644
--- a/src/Level.cpp
+++ b/src/Level.cpp
@@ -53,14 +53,14 @@ boost::shared_ptr<Level> Level::loadLevel(const std::string &filename) {
}
for(std::list<Room>::iterator room = level->rooms.begin(); room != level->rooms.end(); ++room) {
- for(std::list<BSPTree::TriangleRecord>::iterator wall = room->walls.begin(); wall != room->walls.end(); ++wall) {
- boost::shared_ptr<WallData> wallData = boost::dynamic_pointer_cast<WallData>(wall->data);
+ for(std::list<TriangleRecord>::iterator wall = room->walls.begin(); wall != room->walls.end(); ++wall) {
+ boost::shared_ptr<WallData> wallData = boost::dynamic_pointer_cast<WallData>(wall->getData());
if(!wallData->texture.empty()) {
std::map<std::string, unsigned>::iterator texture = level->textures.find(wallData->texture);
if(texture != level->textures.end())
- wall->triangle.setTexture(texture->second);
+ wall->getTriangle().setTexture(texture->second);
}
}
}
@@ -149,11 +149,11 @@ void Level::loadTextures(xmlNodePtr texturesNode) {
}
}
-BSPTree::TriangleRecord Level::loadWall(xmlNodePtr wallNode) {
+TriangleRecord Level::loadWall(xmlNodePtr wallNode) {
boost::shared_ptr<WallData> wallData(new WallData);
- BSPTree::TriangleRecord wall;
- wall.data = wallData;
+ TriangleRecord wall;
+ wall.setData(wallData);
xmlChar *data = xmlGetProp(wallNode, (xmlChar*)"texture");
@@ -172,39 +172,39 @@ BSPTree::TriangleRecord Level::loadWall(xmlNodePtr wallNode) {
if(++vertexNum > 2)
break;
- wall.triangle.setVertex(vertexNum, loadVector(node));
+ wall.getTriangle().setVertex(vertexNum, loadVector(node));
}
else if(!xmlStrcmp(node->name, (xmlChar*)"normal")) {
if(vertexNum < 0)
continue;
- wall.triangle.setNormal(vertexNum, loadVector(node));
+ wall.getTriangle().setNormal(vertexNum, loadVector(node));
}
else if(!xmlStrcmp(node->name, (xmlChar*)"texcoords")) {
if(vertexNum < 0) continue;
data = xmlGetProp(node, (xmlChar*)"s");
if(data) {
- wall.triangle.getTexCoords(vertexNum)[0] = atof((char*)data);
+ wall.getTriangle().getTexCoords(vertexNum)[0] = atof((char*)data);
xmlFree(data);
}
data = xmlGetProp(node, (xmlChar*)"t");
if(data) {
- wall.triangle.getTexCoords(vertexNum)[1] = atof((char*)data);
+ wall.getTriangle().getTexCoords(vertexNum)[1] = atof((char*)data);
xmlFree(data);
}
}
}
- vmml::vec3f normal = wall.triangle.computeNormal();
+ vmml::vec3f normal = wall.getTriangle().computeNormal();
if(normal.squared_length() > 0) {
normal.normalize();
for(int i = 0; i < 3; ++i) {
- if(wall.triangle.getNormal(i).squared_length() == 0)
- wall.triangle.setNormal(i, normal);
+ if(wall.getTriangle().getNormal(i).squared_length() == 0)
+ wall.getTriangle().setNormal(i, normal);
}
}
diff --git a/src/Level.h b/src/Level.h
index 277b6e7..5e32aa1 100644
--- a/src/Level.h
+++ b/src/Level.h
@@ -20,7 +20,7 @@
#ifndef ZOOM_LEVEL_H_
#define ZOOM_LEVEL_H_
-#include "BSPTree.h"
+#include "TriangleRecord.h"
#include <string>
#include <map>
@@ -36,13 +36,13 @@ class Level {
public:
static boost::shared_ptr<Level> loadLevel(const std::string &filename);
- struct WallData : public BSPTree::TriangleData {
+ struct WallData : public TriangleRecord::TriangleData {
std::string texture;
};
struct Room {
std::string id;
- std::list<BSPTree::TriangleRecord> walls;
+ std::list<TriangleRecord> walls;
};
const std::list<Room> &getRooms() const {
@@ -63,7 +63,7 @@ class Level {
void loadRoom(xmlNodePtr roomNode);
void loadTextures(xmlNodePtr texturesNode);
- static BSPTree::TriangleRecord loadWall(xmlNodePtr wallNode);
+ static TriangleRecord loadWall(xmlNodePtr wallNode);
static vmml::vec3f loadVector(xmlNodePtr node);
static bool validateLevel(xmlDocPtr doc);
diff --git a/src/Renderer.h b/src/Renderer.h
index 8072158..a66504a 100644
--- a/src/Renderer.h
+++ b/src/Renderer.h
@@ -21,9 +21,11 @@
#define ZOOM_RENDERER_H_
#include "gl.h"
-#include "BSPTree.h"
+
#include "Shader.h"
#include "ShadowVolume.h"
+#include "TriangleRecord.h"
+
#include <limits>
@@ -43,7 +45,7 @@ class Renderer {
// Create shadow volumes
std::vector<ShadowVolume> shadowVolumes;
for(typename T::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
- shadowVolumes.push_back(ShadowVolume(t->triangle, lightPos));
+ shadowVolumes.push_back(ShadowVolume(t->getTriangle(), lightPos));
}
@@ -56,11 +58,11 @@ class Renderer {
glStencilFunc(GL_ALWAYS, 0, std::numeric_limits<GLuint>::max());
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
- useTexture(triangles.front().triangle.getTexture());
+ useTexture(triangles.front().getTriangle().getTexture());
glBegin(GL_TRIANGLES);
for(typename T::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
- renderTriangle(t->triangle);
+ renderTriangle(t->getTriangle());
}
glEnd();
@@ -71,7 +73,8 @@ class Renderer {
glDepthMask(GL_FALSE);
glCullFace(GL_FRONT);
- glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
+ glDepthFunc(GL_GEQUAL);
+ glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
glBegin(GL_TRIANGLES);
for(std::vector<ShadowVolume>::iterator v = shadowVolumes.begin(); v != shadowVolumes.end(); ++v) {
@@ -80,7 +83,7 @@ class Renderer {
glEnd();
glCullFace(GL_BACK);
- glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
+ glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
glBegin(GL_TRIANGLES);
for(std::vector<ShadowVolume>::iterator v = shadowVolumes.begin(); v != shadowVolumes.end(); ++v) {
@@ -98,18 +101,18 @@ class Renderer {
glStencilFunc(GL_EQUAL, 0, std::numeric_limits<GLuint>::max());
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
- useTexture(triangles.front().triangle.getTexture());
+ useTexture(triangles.front().getTriangle().getTexture());
glBegin(GL_TRIANGLES);
for(typename T::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
- renderTriangle(t->triangle);
+ renderTriangle(t->getTriangle());
}
glEnd();
}
struct TextureSorter {
- bool operator() (const BSPTree::TriangleRecord &t1, const BSPTree::TriangleRecord &t2) {
- return (t1.triangle.getTexture() < t2.triangle.getTexture());
+ bool operator() (const TriangleRecord &t1, const TriangleRecord &t2) {
+ return (t1.getTriangle().getTexture() < t2.getTriangle().getTexture());
}
};
@@ -122,8 +125,8 @@ class Renderer {
public:
RenderVisitor(Renderer *renderer0) : renderer(renderer0) {}
- void operator() (const BSPTree::TriangleRecord &t) const {
- renderer->renderTriangle(t.triangle);
+ void operator() (const TriangleRecord &t) const {
+ renderer->renderTriangle(t.getTriangle());
}
private:
diff --git a/src/TriangleRecord.h b/src/TriangleRecord.h
new file mode 100644
index 0000000..13b3248
--- /dev/null
+++ b/src/TriangleRecord.h
@@ -0,0 +1,58 @@
+/*
+ * TriangleRecord.h
+ *
+ * Copyright (C) 2009 Matthias Schiffer <matthias@gamezock.de>
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef ZOOM_TRIANGLERECORD_H_
+#define ZOOM_TRIANGLERECORD_H_
+
+#include "Triangle.h"
+
+#include <boost/shared_ptr.hpp>
+
+namespace Zoom {
+
+class TriangleRecord {
+ public:
+ class TriangleData {
+ protected:
+ TriangleData() {}
+
+ public:
+ virtual ~TriangleData() {}
+ };
+
+ TriangleRecord(const Triangle& triangle0, boost::shared_ptr<TriangleData> data0)
+ : triangle(triangle0), data(data0) {}
+ TriangleRecord(const Triangle& triangle0) : triangle(triangle0) {}
+ TriangleRecord() {}
+
+ const Triangle& getTriangle() const {return triangle;}
+ Triangle& getTriangle() {return triangle;}
+ void setTriangle(const Triangle &t) {triangle = t;}
+
+ boost::shared_ptr<TriangleData> getData() const {return data;}
+ void setData(boost::shared_ptr<TriangleData> d) {data = d;}
+
+ private:
+ Triangle triangle;
+ boost::shared_ptr<TriangleData> data;
+};
+
+}
+
+#endif /* ZOOM_TRIANGLERECORD_H_ */