summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--BSPTree.cpp116
-rw-r--r--BSPTree.h95
-rw-r--r--Color.h19
-rw-r--r--Cubehole.cpp2
-rw-r--r--Cubehole.h43
-rw-r--r--Cuboid.cpp90
-rw-r--r--Cuboid.h4
-rw-r--r--DisplayClass.cpp51
-rw-r--r--DisplayClass.h11
-rw-r--r--Matrix.h45
-rw-r--r--Trapezocube.cpp95
-rw-r--r--Trapezocube.h15
-rw-r--r--Triangle.h46
-rw-r--r--Vector.h60
-rw-r--r--Vertex.h22
-rw-r--r--main.cpp19
16 files changed, 363 insertions, 370 deletions
diff --git a/BSPTree.cpp b/BSPTree.cpp
index b30c907..00e916b 100644
--- a/BSPTree.cpp
+++ b/BSPTree.cpp
@@ -1,25 +1,123 @@
#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;
+vmml::vec3f BSPTree::Plane::intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const {
+ float r = (d - p.dot(normal))/dir.dot(normal);
+
+ return p + r*dir;
+}
+
+void BSPTree::Plane::partition(const Triangle &t, std::list<Triangle> *front, std::list<Triangle> *back) const {
+ for(int i = 0; i < 3; ++i) {
+ if(contains(t.getVertex(i))) {
+ const vmml::vec3f *v[3] = {&t.getVertex(i), &t.getVertex((i+1)%3), &t.getVertex((i+2)%3)};
+
+ vmml::vec3f is = intersection(*v[1], *v[2]-*v[1]);
+
+ if(isInFront(*v[1])) {
+ front->push_back(Triangle(*v[0], *v[1], is, t.getColor()));
+ back->push_back(Triangle(*v[0], is, *v[2], t.getColor()));
+ }
+ else {
+ back->push_back(Triangle(*v[0], *v[1], is, t.getColor()));
+ front->push_back(Triangle(*v[0], is, *v[2], t.getColor()));
+ }
+
+ return;
+ }
+ }
+ for(int i = 0; i < 3; ++i) {
+ const vmml::vec3f *v[3] = {&t.getVertex(i), &t.getVertex((i+1)%3), &t.getVertex((i+2)%3)};
+ if((isInFront(*v[0]) && isBehind(*v[1]) && isBehind(*v[2]))
+ || (isBehind(*v[0]) && isInFront(*v[1]) && isInFront(*v[2]))) {
+ vmml::vec3f is1 = intersection(*v[0], *v[1]-*v[0]);
+ vmml::vec3f is2 = intersection(*v[0], *v[2]-*v[0]);
+
+ if(isInFront(*v[0])) {
+ front->push_back(Triangle(*v[0], is1, is2, t.getColor()));
+ back->push_back(Triangle(is1, *v[1], is2, t.getColor()));
+ back->push_back(Triangle(*v[1], *v[2], is2, t.getColor()));
+ }
+ else {
+ back->push_back(Triangle(*v[0], is1, is2, t.getColor()));
+ front->push_back(Triangle(is1, *v[1], is2, t.getColor()));
+ front->push_back(Triangle(*v[1], *v[2], is2, t.getColor()));
+ }
+
+ return;
+ }
+ }
}
-Vertex BSPTree::findCenter(const std::list<Triangle> &triangles) {
- Vector v;
+BSPTree::BSPTree(const std::list<Triangle> &triangles) : frontTree(0), backTree(0) {
+ if(triangles.empty())
+ return;
+
+ const Triangle *planeT = findNearestTriangle(triangles, findCenter(triangles));
+
+ plane = Plane(*planeT);
+
+ std::list<Triangle> front, back;
for(std::list<Triangle>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
- v += Vector(t->getCenter());
+ if(plane.contains(*t)) {
+ this->triangles.push_back(*t);
+ continue;
+ }
+ else if(plane.isInFront(*t)) {
+ front.push_back(*t);
+ continue;
+ }
+ else if(plane.isBehind(*t)) {
+ back.push_back(*t);
+ continue;
+ }
+
+ std::list<Triangle> frontPart, backPart;
+ plane.partition(*t, &frontPart, &backPart);
+ front.splice(front.end(), frontPart);
+ back.splice(back.end(), backPart);
+ }
+
+ if(!front.empty())
+ frontTree = new BSPTree(front);
+
+ if(!back.empty())
+ backTree = new BSPTree(back);
+}
+
+
+vmml::vec3f BSPTree::findCenter(const std::list<Triangle> &triangles) {
+ vmml::vec3f v;
+
+ for(std::list<Triangle>::const_iterator t = triangles.begin(); t != triangles.end(); ++t) {
+ v += t->getCenter();
}
return v/triangles.size();
}
-const Triangle* BSPTree::findNearestTriangle(const std::list<Triangle> &triangles, const Vertex &v) {
- Triangle *current = 0;
- float distance;
+const Triangle* BSPTree::findNearestTriangle(const std::list<Triangle> &triangles, const vmml::vec3f &v) {
+ if(triangles.empty())
+ return 0;
+
+ std::list<Triangle>::const_iterator t = triangles.begin();
+
+ const Triangle *current = &*t;
+ float distanceSq = current->getCenter().squared_distance(v);
+
+ for(++t; t != triangles.end(); ++t) {
+ float d = t->getCenter().squared_distance(v);
+
+ if(d < distanceSq) {
+ current = &*t;
+ distanceSq = d;
+ }
+ }
+
+ return current;
}
diff --git a/BSPTree.h b/BSPTree.h
index dc7e2df..50f18df 100644
--- a/BSPTree.h
+++ b/BSPTree.h
@@ -4,67 +4,122 @@
#include "Triangle.h"
#include <list>
#include <cmath>
+#include <vmmlib/vector.hpp>
class BSPTree {
private:
class Plane {
public:
Plane() : d(0) {}
- Plane(const Vector &n, float d0) : normal(n), d(d0) {}
+ Plane(const vmml::vec3f &n, float d0) : normal(n), d(d0) {}
+ Plane(const Triangle &t) : normal(t.getNormal()), d(t.getVertex(0).dot(normal)) {}
- bool contains(Vertex v) {
- return (fabsf(normal.dot(Vector(v)) - d) < 1E-6);
+ bool contains(const vmml::vec3f &v) const {
+ return (fabsf(normal.dot(v) - d) < 1E-6);
}
- bool isBehind(Vertex v) {
- return (normal.dot(Vector(v)) - d) < 0;
+ bool isBehind(const vmml::vec3f &v) const {
+ return (normal.dot(v) - d) < 0;
}
- const Vector& getNormal() {
+ bool isInFront(const vmml::vec3f &v) const {
+ return (normal.dot(v) - d) > 0;
+ }
+
+
+ bool contains(const Triangle &t) const {
+ for(int i = 0; i < 3; ++i) {
+ if(!contains(t.getVertex(i)))
+ return false;
+ }
+
+ return true;
+ }
+
+ bool isBehind(const Triangle &t) const {
+ for(int i = 0; i < 3; ++i) {
+ if(!isBehind(t.getVertex(i)) && !contains(t.getVertex(i)))
+ return false;
+ }
+
+ return true;
+ }
+
+ bool isInFront(const Triangle &t) const {
+ for(int i = 0; i < 3; ++i) {
+ if(!isInFront(t.getVertex(i)) && !contains(t.getVertex(i)))
+ return false;
+ }
+
+ return true;
+ }
+
+
+ const vmml::vec3f& getNormal() const {
return normal;
}
+ vmml::vec3f intersection(const vmml::vec3f &p, const vmml::vec3f &dir) const;
+ void partition(const Triangle &t, std::list<Triangle> *front, std::list<Triangle> *back) const;
+
private:
- Vector normal;
+ vmml::vec3f normal;
float d;
};
public:
BSPTree(const std::list<Triangle> &triangles);
+ virtual ~BSPTree() {
+ if(frontTree)
+ delete frontTree;
+
+ if(backTree)
+ delete backTree;
+ }
template<typename T>
- void visit(T& visitor, const Vector &v) {
- if(plane.getNormal().dot(v) > 0) {
+ void visit(const T& visitor, const vmml::vec3f &p) {
+ doVisit<const T>(visitor, p);
+ }
+
+ template<typename T>
+ void visit(T& visitor, const vmml::vec3f &p) {
+ doVisit(visitor, p);
+ }
+
+ private:
+ Plane plane;
+ std::list<Triangle> triangles;
+ BSPTree *frontTree, *backTree;
+
+ template<typename T>
+ void doVisit(T& visitor, const vmml::vec3f &p) {
+ if(plane.isBehind(p)) {
if(frontTree)
- frontTree->visit(visitor, v);
+ frontTree->visit(visitor, p);
for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
visitor(*t);
}
if(backTree)
- backTree->visit(visitor, v);
+ backTree->visit(visitor, p);
}
else {
if(backTree)
- backTree->visit(visitor, v);
+ backTree->visit(visitor, p);
for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
visitor(*t);
}
if(frontTree)
- frontTree->visit(visitor, v);
+ frontTree->visit(visitor, p);
}
}
- 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);
+ static vmml::vec3f findCenter(const std::list<Triangle> &triangles);
+ static const Triangle* findNearestTriangle(const std::list<Triangle> &triangles, const vmml::vec3f &v);
};
#endif /* _BSPTREE_H_ */
diff --git a/Color.h b/Color.h
deleted file mode 100644
index ad242c7..0000000
--- a/Color.h
+++ /dev/null
@@ -1,19 +0,0 @@
-#ifndef _COLOR_H_
-#define _COLOR_H_
-
-class Color
-{
- public:
- Color(float r0 = 0, float g0 = 0, float b0 = 0, float a0 = 1.0) : r(r0), g(g0), b(b0), a(a0) {}
-
- float getR() const {return r;}
- float getG() const {return g;}
- float getB() const {return b;}
- float getA() const {return a;}
-
- private:
- float r, g, b, a;
-};
-
-#endif /*_COLOR_H_*/
-
diff --git a/Cubehole.cpp b/Cubehole.cpp
index ed54e83..0806f79 100644
--- a/Cubehole.cpp
+++ b/Cubehole.cpp
@@ -1,7 +1,7 @@
#include "Cubehole.h"
#include "gl.h"
-std::list<Triangle> Cubehole::getTriangles(/*const Matrix &modelview*/)
+std::list<Triangle> Cubehole::getTriangles()
{
std::list<Triangle> triangles;
// width, height, depth
diff --git a/Cubehole.h b/Cubehole.h
index 83ef382..c8e8736 100644
--- a/Cubehole.h
+++ b/Cubehole.h
@@ -3,35 +3,37 @@
#include "Trapezocube.h"
#include "Triangle.h"
-#include "Matrix.h"
#include <list>
class Cubehole
{
public:
- Cubehole(): width(0), height(0), depth(0), x(0), y(0), z(0), innerwidth(0), innerdepth(0),
- colorfront(0), colorback(0), colorleft(0), colorright(0) {
- }
+ Cubehole(): width(0), height(0), depth(0), x(0), y(0), z(0), innerwidth(0), innerdepth(0) {}
+
Cubehole(float width0, float height0, float depth0, float x0, float y0, float z0,
- float innerwidth0, float innerdepth0,
- Color colorfront0, Color colorright0, Color colorback0, Color colorleft0) {
- setSize(width0, height0, depth0, innerwidth0, innerdepth0);
- setPos(x0, y0, z0);
- setColor(colorfront0, colorright0, colorback0, colorleft0);
- front.setRotate(0);
- right.setRotate(90);
- back.setRotate(180);
- left.setRotate(270);
- }
+ float innerwidth0, float innerdepth0,
+ const vmml::vec4f &colorfront0, const vmml::vec4f &colorright0,
+ const vmml::vec4f &colorback0, const vmml::vec4f &colorleft0) {
+ setSize(width0, height0, depth0, innerwidth0, innerdepth0);
+ setPos(x0, y0, z0);
+ setColor(colorfront0, colorright0, colorback0, colorleft0);
+ front.setRotate(0);
+ right.setRotate(90);
+ back.setRotate(180);
+ left.setRotate(270);
+ }
+
float getHeight();
float getWidth();
float getDepth();
float getPosX();
float getPosY();
float getPosZ();
+
float getInnerWidth() {return innerwidth;}
float getInnerDepth() {return innerdepth;}
+
void setSize(float w, float h, float d, float iw, float id){
width = w;
height = h;
@@ -43,26 +45,29 @@ class Cubehole
back.setSize(width, innerwidth, height, (depth-innerdepth)/2);
left.setSize(width, innerdepth, height, (width-innerwidth)/2);
}
+
void setPos(float x0, float y0, float z0) {
x = x0;
y = y0;
z = z0;
front.setPos(x, y, depth-(depth-innerdepth)/2);
right.setPos(x, y, width-(width-innerwidth)/2);
- back.setPos(x, y, depth-(depth-innerdepth)/2);
- left.setPos(x, y, width-(width-innerwidth)/2);
+ back.setPos(x, y, depth-(depth-innerdepth)/2);
+ left.setPos(x, y, width-(width-innerwidth)/2);
}
- void setColor(Color cf, Color cr, Color cb, Color cl){
+
+ void setColor(const vmml::vec4f &cf, const vmml::vec4f &cr, const vmml::vec4f &cb, const vmml::vec4f &cl) {
front.setColor(cf);
right.setColor(cr);
back.setColor(cb);
left.setColor(cl);
}
- std::list<Triangle> getTriangles(/*const Matrix &modelview*/);
+
+ std::list<Triangle> getTriangles();
private:
float x, y, z, width, height, depth, innerwidth, innerdepth;
- Color colorfront, colorleft, colorback, colorright;
+ vmml::vec4f colorfront, colorleft, colorback, colorright;
Trapezocube front, right, back, left;
};
diff --git a/Cuboid.cpp b/Cuboid.cpp
index b53dada..5afae93 100644
--- a/Cuboid.cpp
+++ b/Cuboid.cpp
@@ -1,10 +1,6 @@
#include "Cuboid.h"
#include "gl.h"
-#include <iostream>
-//#include <stdlib.h>
-//#include <time.h>
-
Cuboid::Cuboid(float width, float height, float depth) {
setSize(width, height, depth);
setPos(0, 0, 0);
@@ -60,77 +56,73 @@ void Cuboid::setPos(float x, float y, float z)
this->z = z;
}
-std::list<Triangle> Cuboid::getTriangles(const Matrix &modelview)
+std::list<Triangle> Cuboid::getTriangles()
{
std::list<Triangle> triangles;
// width, height, depth
// Front face
- Color c(0.0, 0.0, 1.0, 0.5);
+ vmml::vec4f c(0.0, 0.0, 1.0, 0.5);
- triangles.push_back(Triangle(Vertex(x-width/2, y+height/2, z+depth/2),
- Vertex(x+width/2, y+height/2, z+depth/2),
- Vertex(x-width/2, y-height/2, z+depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y+height/2, z+depth/2),
+ vmml::vec3f(x-width/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z+depth/2), c));
- triangles.push_back(Triangle(Vertex(x-width/2, y-height/2, z+depth/2),
- Vertex(x+width/2, y+height/2, z+depth/2),
- Vertex(x+width/2, y-height/2, z+depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+width/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z+depth/2), c));
// Back face
- c = Color(1.0, 1.0, 0.0, 0.5);
+ c = vmml::vec4f(1.0, 1.0, 0.0, 0.5);
- triangles.push_back(Triangle(Vertex(x-width/2, y+height/2, z-depth/2),
- Vertex(x-width/2, y-height/2, z-depth/2),
- Vertex(x+width/2, y+height/2, z-depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y+height/2, z-depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z-depth/2),
+ vmml::vec3f(x-width/2, y-height/2, z-depth/2), c));
- triangles.push_back(Triangle(Vertex(x-width/2, y-height/2, z-depth/2),
- Vertex(x+width/2, y+height/2, z-depth/2),
- Vertex(x+width/2, y-height/2, z-depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y-height/2, z-depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z-depth/2),
+ vmml::vec3f(x+width/2, y-height/2, z-depth/2), c));
// Left face
- c = Color(0.0, 1.0, 0.0, 0.5);
+ c = vmml::vec4f(0.0, 1.0, 0.0, 0.5);
- triangles.push_back(Triangle(Vertex(x-width/2, y+height/2, z+depth/2),
- Vertex(x-width/2, y-height/2, z+depth/2),
- Vertex(x-width/2, y+height/2, z-depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y+height/2, z+depth/2),
+ vmml::vec3f(x-width/2, y+height/2, z-depth/2),
+ vmml::vec3f(x-width/2, y-height/2, z+depth/2), c));
- triangles.push_back(Triangle(Vertex(x-width/2, y-height/2, z-depth/2),
- Vertex(x-width/2, y+height/2, z-depth/2),
- Vertex(x-width/2, y-height/2, z+depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y-height/2, z-depth/2),
+ vmml::vec3f(x-width/2, y-height/2, z+depth/2),
+ vmml::vec3f(x-width/2, y+height/2, z-depth/2), c));
// Right face
- triangles.push_back(Triangle(Vertex(x+width/2, y+height/2, z+depth/2),
- Vertex(x+width/2, y-height/2, z+depth/2),
- Vertex(x+width/2, y+height/2, z-depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x+width/2, y+height/2, z+depth/2),
+ vmml::vec3f(x+width/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z-depth/2), c));
- triangles.push_back(Triangle(Vertex(x+width/2, y-height/2, z-depth/2),
- Vertex(x+width/2, y+height/2, z-depth/2),
- Vertex(x+width/2, y-height/2, z+depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x+width/2, y-height/2, z-depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z-depth/2),
+ vmml::vec3f(x+width/2, y-height/2, z+depth/2), c));
// Top face
- c = Color(1.0, 0.0, 0.0, 0.5);
+ c = vmml::vec4f(1.0, 0.0, 0.0, 0.5);
- triangles.push_back(Triangle(Vertex(x-width/2, y+height/2, z+depth/2),
- Vertex(x+width/2, y+height/2, z+depth/2),
- Vertex(x-width/2, y+height/2, z-depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y+height/2, z+depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z+depth/2),
+ vmml::vec3f(x-width/2, y+height/2, z-depth/2), c));
- triangles.push_back(Triangle(Vertex(x+width/2, y+height/2, z+depth/2),
- Vertex(x+width/2, y+height/2, z-depth/2),
- Vertex(x-width/2, y+height/2, z-depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x+width/2, y+height/2, z+depth/2),
+ vmml::vec3f(x+width/2, y+height/2, z-depth/2),
+ vmml::vec3f(x-width/2, y+height/2, z-depth/2), c));
// Bottom face
- triangles.push_back(Triangle(Vertex(x-width/2, y-height/2, z+depth/2),
- Vertex(x+width/2, y-height/2, z+depth/2),
- Vertex(x-width/2, y-height/2, z-depth/2), c));
+ triangles.push_back(Triangle(vmml::vec3f(x-width/2, y-height/2, z+depth/2),
+ vmml::vec3f(x-width/2, y-height/2, z-depth/2),
+ vmml::vec3f(x+width/2, y-height/2, z+depth/2), c));
- triangles.push_back(Triangle(Vertex(x+width/2, y-height/2, z+depth/2),
- Vertex(x+width/2, y-height/2, z-depth/2),
- Vertex(x-width/2, y-height/2, z-depth/2), c));
-
- for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
- t->transform(modelview);
- }
+ triangles.push_back(Triangle(vmml::vec3f(x+width/2, y-height/2, z+depth/2),
+ vmml::vec3f(x-width/2, y-height/2, z-depth/2),
+ vmml::vec3f(x+width/2, y-height/2, z-depth/2), c));
return triangles;
}
diff --git a/Cuboid.h b/Cuboid.h
index ae9f2a1..25d9f73 100644
--- a/Cuboid.h
+++ b/Cuboid.h
@@ -2,9 +2,7 @@
#define _CUBOID_H_
#include "Triangle.h"
-#include "Matrix.h"
-#include <iostream>
#include <list>
class Cuboid
@@ -21,7 +19,7 @@ class Cuboid
float getPosZ();
void setSize(float w, float h, float d);
void setPos(float x, float y, float z);
- std::list<Triangle> getTriangles(const Matrix &modelview);
+ std::list<Triangle> getTriangles();
private:
float width, height, depth;
diff --git a/DisplayClass.cpp b/DisplayClass.cpp
index b0a4a8d..6d9fdee 100644
--- a/DisplayClass.cpp
+++ b/DisplayClass.cpp
@@ -1,27 +1,20 @@
#include "DisplayClass.h"
-#include "Matrix.h"
#include "gl.h"
-//#include <algorithm>
-#include "Trapezocube.h"
#include "BSPTree.h"
-DisplayClass::DisplayClass() {
- static const float pos[5] = {-3.0, -2.0, 0.0, 2.0, 3.0};
+DisplayClass::Renderer DisplayClass::renderer;
- 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]);
- }
- }
- }
+DisplayClass::DisplayClass() {
+ cubehole = Cubehole(3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5,
+ vmml::vec4f(1.0, 0.0, 0.0),
+ vmml::vec4f(0.0, 1.0, 0.0),
+ vmml::vec4f(0.0, 0.0, 1.0),
+ vmml::vec4f(1.0, 0.85, 0.06));
}
void DisplayClass::renderScene(unsigned long delta)
{
- Cubehole cubeing(3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5, Color(1.0, 0.0, 0.0), Color(0.0, 1.0, 0.0), Color(0.0, 0.0, 1.0), Color(1.0, 0.85, 0.06));
static float angle = 0.0;
angle += delta*0.025;
if(angle >= 360)
@@ -29,11 +22,6 @@ void DisplayClass::renderScene(unsigned long delta)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //Matrix p(GL_PROJECTION_MATRIX);
- //Matrix m;
-
- std::list<Triangle> triangles;
-
glLoadIdentity(); // Clean up matrix
glTranslatef(0.0, 0.0, -10.0); // Then set up transformation
glRotatef(angle, 0.0, 1.0, 0.0);
@@ -41,25 +29,20 @@ void DisplayClass::renderScene(unsigned long delta)
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 k = 0; k < 5; ++k) {
- std::list<Triangle> ct = cubes[i][j][k].getTriangles(m);
- triangles.splice(triangles.end(), ct);
- }
- }
- }*/
- //glLoadIdentity();
+ std::list<Triangle> triangles = cubehole.getTriangles();
+
+ BSPTree tree(triangles);
+
+ vmml::mat4f transform, inverseTransform;
+ glGetFloatv(GL_MODELVIEW_MATRIX, transform.array);
+
+ transform.inverse(inverseTransform);
- std::list<Triangle> ct = cubeing.getTriangles();
- triangles.splice(triangles.end(), ct);
+ vmml::vec3f viewPoint = inverseTransform*vmml::vec3f::ZERO;
glBegin(GL_TRIANGLES);
- for(std::list<Triangle>::reverse_iterator t = triangles.rbegin(); t != triangles.rend(); ++t) {
- t->render();
- }
+ tree.visit(renderer, viewPoint);
glEnd();
glFlush();
diff --git a/DisplayClass.h b/DisplayClass.h
index 51f8636..04ff651 100644
--- a/DisplayClass.h
+++ b/DisplayClass.h
@@ -1,7 +1,6 @@
#ifndef _DISPLAYCLASS_H_
#define _DISPLAYCLASS_H_
-#include "Cuboid.h"
#include "Cubehole.h"
class DisplayClass
@@ -12,7 +11,15 @@ class DisplayClass
void renderScene(unsigned long delta);
private:
- Cuboid cubes[5][5][5];
+ struct Renderer {
+ void operator() (const Triangle &t) const {
+ t.render();
+ }
+ };
+
+ static Renderer renderer;
+
+ Cubehole cubehole;
};
#endif /*_DISPLAYCLASS_H_*/
diff --git a/Matrix.h b/Matrix.h
deleted file mode 100644
index f8c178f..0000000
--- a/Matrix.h
+++ /dev/null
@@ -1,45 +0,0 @@
-#ifndef _MATRIX_H_
-#define _MATRIX_H_
-
-#include "gl.h"
-#include "Vector.h"
-#include "Vertex.h"
-
-class Matrix
-{
- public:
- Matrix(GLenum pname = GL_MODELVIEW_MATRIX) {
- store(pname);
- }
-
- Matrix(float in[16]) {
- for(int i = 0; i < 16; ++i)
- f[i] = in[i];
- }
-
- Vertex operator*(const Vertex &v) const {
- Vector r(v.getX()*m[0][0] + v.getY()*m[1][0] + v.getZ()*m[2][0] + m[3][0],
- v.getX()*m[0][1] + v.getY()*m[1][1] + v.getZ()*m[2][1] + m[3][1],
- v.getX()*m[0][2] + v.getY()*m[1][2] + v.getZ()*m[2][2] + m[3][2]);
- float w = v.getX()*m[0][3] + v.getY()*m[1][3] + v.getZ()*m[2][3] + m[3][3];
-
- return r/w;
- }
-
- void load() {
- glLoadMatrixf(f);
- }
-
- void store(GLenum pname = GL_MODELVIEW_MATRIX) {
- glGetFloatv(pname, f);
- }
-
- private:
- union {
- float f[16];
- float m[4][4];
- };
-};
-
-#endif /*_MATRIX_H_*/
-
diff --git a/Trapezocube.cpp b/Trapezocube.cpp
index 20aa1c4..8205ee6 100644
--- a/Trapezocube.cpp
+++ b/Trapezocube.cpp
@@ -1,73 +1,80 @@
#include "Trapezocube.h"
-std::list<Triangle> Trapezocube::getTriangles(/*const Matrix &modelview*/)
+
+std::list<Triangle> Trapezocube::getTriangles()
{
std::list<Triangle> triangles;
+ // width, height, depth
- glPushMatrix();
- glLoadIdentity();
- glRotatef(rotate, 0.0, 1.0, 0.0);
- Matrix rotation;
- glPopMatrix();
+ vmml::mat4f rotation(vmml::mat4f::IDENTITY);
+ rotation.rotate_y(M_PI*rotate/180.0f);
// Front face
- triangles.push_back(Triangle(Vertex(x-widthfront/2, y+height/2, z+depth/2),
- Vertex(x-widthfront/2, y-height/2, z+depth/2),
- Vertex(x+widthfront/2, y+height/2, z+depth/2), color));
+ vmml::vec4f c(0.0, 0.0, 1.0, 0.5);
+
+ triangles.push_back(Triangle(vmml::vec3f(x-widthfront/2, y+height/2, z+depth/2),
+ vmml::vec3f(x-widthfront/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+widthfront/2, y+height/2, z+depth/2), c));
- triangles.push_back(Triangle(Vertex(x-widthfront/2, y-height/2, z+depth/2),
- Vertex(x+widthfront/2, y-height/2, z+depth/2),
- Vertex(x+widthfront/2, y+height/2, z+depth/2), color));
+ triangles.push_back(Triangle(vmml::vec3f(x-widthfront/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+widthfront/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+widthfront/2, y+height/2, z+depth/2), c));
// Back face
- triangles.push_back(Triangle(Vertex(x-widthback/2, y+height/2, z-depth/2),
- Vertex(x+widthback/2, y+height/2, z-depth/2),
- Vertex(x-widthback/2, y-height/2, z-depth/2), color));
+ c = vmml::vec4f(1.0, 1.0, 0.0, 0.5);
- triangles.push_back(Triangle(Vertex(x-widthback/2, y-height/2, z-depth/2),
- Vertex(x+widthback/2, y+height/2, z-depth/2),
- Vertex(x+widthback/2, y-height/2, z-depth/2), color));
+ triangles.push_back(Triangle(vmml::vec3f(x-widthback/2, y+height/2, z-depth/2),
+ vmml::vec3f(x+widthback/2, y+height/2, z-depth/2),
+ vmml::vec3f(x-widthback/2, y-height/2, z-depth/2), c));
+
+ triangles.push_back(Triangle(vmml::vec3f(x-widthback/2, y-height/2, z-depth/2),
+ vmml::vec3f(x+widthback/2, y+height/2, z-depth/2),
+ vmml::vec3f(x+widthback/2, y-height/2, z-depth/2), c));
// Left face
- triangles.push_back(Triangle(Vertex(x-widthfront/2, y+height/2, z+depth/2),
- Vertex(x-widthback /2, // width, height, depth
- y+height/2, z-depth/2),
- Vertex(x-widthfront/2, y-height/2, z+depth/2), color));
+ c = vmml::vec4f(0.0, 1.0, 0.0, 0.5);
+
+ triangles.push_back(Triangle(vmml::vec3f(x-widthfront/2, y+height/2, z+depth/2),
+ vmml::vec3f(x-widthback /2, y+height/2, z-depth/2),
+ vmml::vec3f(x-widthfront/2, y-height/2, z+depth/2), c));
- triangles.push_back(Triangle(Vertex(x-widthback /2, y-height/2, z-depth/2),
- Vertex(x-widthfront/2, y-height/2, z+depth/2),
- Vertex(x-widthback /2, y+height/2, z-depth/2), color));
+ triangles.push_back(Triangle(vmml::vec3f(x-widthback /2, y-height/2, z-depth/2),
+ vmml::vec3f(x-widthfront/2, y-height/2, z+depth/2),
+ vmml::vec3f(x-widthback /2, y+height/2, z-depth/2), c));
// Right face
- triangles.push_back(Triangle(Vertex(x+widthfront/2, y+height/2, z+depth/2),
- Vertex(x+widthfront/2, y-height/2, z+depth/2),
- Vertex(x+widthback /2, y+height/2, z-depth/2), color));
- triangles.push_back(Triangle(Vertex(x+widthback /2, y-height/2, z-depth/2),
- Vertex(x+widthback /2, y+height/2, z-depth/2),
- Vertex(x+widthfront/2, y-height/2, z+depth/2), color));
+ triangles.push_back(Triangle(vmml::vec3f(x+widthfront/2, y+height/2, z+depth/2),
+ vmml::vec3f(x+widthfront/2, y-height/2, z+depth/2),
+ vmml::vec3f(x+widthback /2, y+height/2, z-depth/2), c));
+
+ triangles.push_back(Triangle(vmml::vec3f(x+widthback /2, y-height/2, z-depth/2),
+ vmml::vec3f(x+widthback /2, y+height/2, z-depth/2),
+ vmml::vec3f(x+widthfront/2, y-height/2, z+depth/2), c));
// Top face
- triangles.push_back(Triangle(Vertex(x-widthfront/2, y+height/2, z+depth/2),
- Vertex(x+widthfront/2, y+height/2, z+depth/2),
- Vertex(x-widthback /2, y+height/2, z-depth/2), color));
+ c = vmml::vec4f(1.0, 0.0, 0.0, 0.5);
+
+ triangles.push_back(Triangle(vmml::vec3f(x-widthfront/2, y+height/2, z+depth/2),
+ vmml::vec3f(x+widthfront/2, y+height/2, z+depth/2),
+ vmml::vec3f(x-widthback /2, y+height/2, z-depth/2), c));
- triangles.push_back(Triangle(Vertex(x+widthfront/2, y+height/2, z+depth/2),
- Vertex(x+widthback /2, y+height/2, z-depth/2),
- Vertex(x-widthback /2, y+height/2, z-depth/2), color));
+ triangles.push_back(Triangle(vmml::vec3f(x+widthfront/2, y+height/2, z+depth/2),
+ vmml::vec3f(x+widthback /2, y+height/2, z-depth/2),
+ vmml::vec3f(x-widthback /2, y+height/2, z-depth/2), c));
// Bottom face
- triangles.push_back(Triangle(Vertex(x-widthfront/2, y-height/2, z+depth/2),
- Vertex(x-widthback /2, y-height/2, z-depth/2),
- Vertex(x+widthfront/2, y-height/2, z+depth/2), color));
- triangles.push_back(Triangle(Vertex(x+widthfront/2, y-height/2, z+depth/2),
- Vertex(x-widthback /2, y-height/2, z-depth/2),
- Vertex(x+widthback /2, y-height/2, z-depth/2), color));
+ triangles.push_back(Triangle(vmml::vec3f(x-widthfront/2, y-height/2, z+depth/2),
+ vmml::vec3f(x-widthback /2, y-height/2, z-depth/2),
+ vmml::vec3f(x+widthfront/2, y-height/2, z+depth/2), c));
+
+ triangles.push_back(Triangle(vmml::vec3f(x+widthfront/2, y-height/2, z+depth/2),
+ vmml::vec3f(x-widthback /2, y-height/2, z-depth/2),
+ vmml::vec3f(x+widthback /2, y-height/2, z-depth/2), c));
for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) {
t->transform(rotation);
- //t->transform(modelview);
}
return triangles;
diff --git a/Trapezocube.h b/Trapezocube.h
index 533cbf8..c291e21 100644
--- a/Trapezocube.h
+++ b/Trapezocube.h
@@ -2,15 +2,15 @@
#define _TRAPEZOCUBE_H_
#include "gl.h"
-#include "Color.h"
#include "Triangle.h"
#include <list>
class Trapezocube
{
public:
- Trapezocube(): widthfront(0), widthback(0), height(0), depth(0), x(0), y(0), z(0), rotate(0), color(0) {}
- Trapezocube(float widthfront, float widthback, float height, float depth, float x, float y, float z, float rotate, Color col) {
+ Trapezocube(): widthfront(0), widthback(0), height(0), depth(0), x(0), y(0), z(0), rotate(0) {}
+
+ Trapezocube(float widthfront, float widthback, float height, float depth, float x, float y, float z, float rotate, const vmml::vec4f &col) {
setSize(widthfront, widthback, height, depth);
setPos(x, y, z);
setRotate(rotate);
@@ -24,7 +24,8 @@ class Trapezocube
float getPosY() {return y;}
float getPosZ() {return z;}
float getRotate() {return rotate;}
- Color getColor() {return color;}
+ vmml::vec4f getColor() {return color;}
+
void setSize(float wf, float wb, float h, float d)
{
widthfront = wf;
@@ -39,12 +40,12 @@ class Trapezocube
this->z = z;
}
void setRotate(float r) {rotate = r;}
- void setColor(Color col) {color = col;}
- std::list<Triangle> getTriangles(/*const Matrix &modelview*/);
+ void setColor(const vmml::vec4f &col) {color = col;}
+ std::list<Triangle> getTriangles();
private:
float x, y, z, widthfront, widthback, height, depth, rotate;
- Color color;
+ vmml::vec4f color;
};
#endif /*_TRAPEZOCUBE_H_ */
diff --git a/Triangle.h b/Triangle.h
index ddb9d97..febcb3d 100644
--- a/Triangle.h
+++ b/Triangle.h
@@ -2,64 +2,54 @@
#define _TRIANGLE_H_
#include "gl.h"
-#include "Color.h"
-#include "Vector.h"
-#include "Vertex.h"
-#include "Matrix.h"
+#include <vmmlib/vector.hpp>
+#include <vmmlib/matrix.hpp>
class Triangle
{
public:
- Triangle(const Vertex &v1, const Vertex &v2, const Vertex &v3, const Color &c0) : c(c0)
+ Triangle() {}
+ Triangle(const vmml::vec3f &v1, const vmml::vec3f &v2, const vmml::vec3f &v3, const vmml::vec4f &c0) : c(c0)
{
v[0] = v1;
v[1] = v2;
v[2] = v3;
}
- const Vertex& getVertex(int i) const {return v[i];}
- const Color& getColor() const {return c;}
+ const vmml::vec3f& getVertex(int i) const {return v[i];}
+ const vmml::vec4f& getColor() const {return c;}
- Vertex getNormal() const {
- Vector v1 = v[0]-v[2];
- Vector v2 = v[0]-v[1];
-
- return v1.cross(v2).normalize();
+ vmml::vec3f getNormal() const {
+ return v[0].compute_normal(v[1], v[2]);
}
void render() const {
- //glColor4f(c.getR(), c.getG(), c.getB(), c.getA());
- float color[] = {c.getR(), c.getG(), c.getB(), c.getA()};
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
-
- for(int i = 0; i < 3; ++i) {
- color[i] /= 2;
- }
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
+ glColor4fv(c.array);
+ glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c.array);
+ glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (c/2).array);
- Vector normal = getNormal();
- glNormal3f(normal.getX(), normal.getY(), normal.getZ());
+ glNormal3fv(getNormal().array);
for(int i = 0; i < 3; ++i)
{
- glVertex3f(v[i].getX(), v[i].getY(), v[i].getZ());
+ glVertex3fv(v[i].array);
}
}
- void transform(const Matrix &m) {
+ void transform(const vmml::mat4f &m) {
for(int i = 0; i < 3; ++i)
{
v[i] = m*v[i];
}
}
- Vertex getCenter() const {
- return (Vector(v[0])+Vector(v[1])+Vector(v[2]))/3;
+ vmml::vec3f getCenter() const {
+ return (v[0]+v[1]+v[2])/3;
}
private:
- Vertex v[3];
- Color c;
+ vmml::vec3f v[3];
+ vmml::vec4f c;
};
#endif /*_TRIANGLE_H_*/
diff --git a/Vector.h b/Vector.h
deleted file mode 100644
index f03c4b4..0000000
--- a/Vector.h
+++ /dev/null
@@ -1,60 +0,0 @@
-#ifndef _VECTOR_H_
-#define _VECTOR_H_
-
-#include "Vertex.h"
-#include <math.h>
-
-class Vector : public Vertex {
- public:
- Vector(float x0 = 0, float y0 = 0, float z0 = 0) : Vertex(x0, y0, z0) {}
- Vector(const Vertex &v) : Vertex(v) {}
-
- Vector operator+(const Vector &v) const {
- 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);
- }
-
- Vector operator/(float f) const {
- return (*this)*(1/f);
- }
-
- Vector operator-() const {
- return Vector(-x, -y, -z);
- }
-
- Vector cross(const Vector &v) const {
- return Vector(y*v.z - z*v.y,
- z*v.x - x*v.z,
- x*v.y - y*v.x);
- }
-
- float dot(const Vector &v) const {
- return x*v.x + y*v.y + z*v.z;
- }
-
- float length() const {
- return sqrtf(x*x+y*y+z*z);
- }
-
- Vector normalize() const {
- return *this/length();
- }
-};
-
-static inline Vector operator-(const Vertex &v1, const Vertex &v2) {
- return Vector(v1.getX()-v2.getX(), v1.getY()-v2.getY(), v1.getZ()-v2.getZ());
-}
-
-#endif /*_VECTOR_H_*/
-
diff --git a/Vertex.h b/Vertex.h
deleted file mode 100644
index 665430d..0000000
--- a/Vertex.h
+++ /dev/null
@@ -1,22 +0,0 @@
-#ifndef _VERTEX_H_
-#define _VERTEX_H_
-
-class Vertex
-{
- public:
- Vertex(float x0 = 0, float y0 = 0, float z0 = 0) : x(x0), y(y0), z(z0) {}
-
- float getX() const {return x;}
- 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;
-};
-
-#endif /*_VERTEX_H_*/
-
diff --git a/main.cpp b/main.cpp
index 5f24b9a..fd20eb9 100644
--- a/main.cpp
+++ b/main.cpp
@@ -20,11 +20,11 @@ void resize(int width, int height);
void initGL(bool multisample) {
glClearColor(1.0, 0.85, 0.06, 1.0);
glClearDepth(1.0);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
+ //glEnable(GL_DEPTH_TEST);
+ //glDepthFunc(GL_LEQUAL);
- //glEnable(GL_BLEND);
- //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
#ifndef _WIN32
if(multisample)
@@ -34,14 +34,14 @@ void initGL(bool multisample) {
resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
glEnable(GL_LIGHTING);
- static const float light[] = {-1, -1, -1, 0};
+ static const float light[] = {1, 1, 1, 0};
static const float lightColor[] = {1, 1, 1, 1};
glLightfv(GL_LIGHT0, GL_POSITION, light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glEnable(GL_LIGHT0);
- glEnable(GL_CULL_FACE);
- glFrontFace(GL_CCW);
+ //glEnable(GL_CULL_FACE);
+ //glFrontFace(GL_CCW);
}
void resize(int width, int height)
@@ -382,11 +382,13 @@ int main() {
glXSwapBuffers(disp, wnd);
XSync(disp, 0);
+ long slept = 0;
gettimeofday(&tv, NULL);
delta = ((tv.tv_usec + 1000000 - ticks)%1000000)/1000;
if(delta < MIN_FRAME_DELTA) {
usleep((MIN_FRAME_DELTA-delta)*1000);
+ slept += (MIN_FRAME_DELTA-delta);
gettimeofday(&tv, NULL);
delta = ((tv.tv_usec + 1000000 - ticks)%1000000)/1000;
@@ -397,9 +399,10 @@ int main() {
frames++;
tocks += delta*1000;
if(tocks > 1000000) {
- std::cerr << frames << std::endl;
+ std::cerr << frames << " fps; slept a total of " << slept << " ms" << std::endl;
frames = 0;
tocks -= 1000000;
+ slept = 0;
}
}