diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-12-10 00:32:11 +0100 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-12-10 00:32:11 +0100 |
commit | 59b00645ccfbade509b1d1694c9fcfc68e013a10 (patch) | |
tree | 5df4d3b47810d2d95589f9e7c58653349578f195 | |
parent | 4731d3f4cf576d791db21ac1932fd91f9b43ff3a (diff) | |
download | c3d-59b00645ccfbade509b1d1694c9fcfc68e013a10.tar c3d-59b00645ccfbade509b1d1694c9fcfc68e013a10.zip |
Zu vmmlib migriert
-rw-r--r-- | BSPTree.cpp | 20 | ||||
-rw-r--r-- | BSPTree.h | 21 | ||||
-rw-r--r-- | Color.h | 19 | ||||
-rw-r--r-- | Cubehole.cpp | 230 | ||||
-rw-r--r-- | Cubehole.h | 3 | ||||
-rw-r--r-- | Cuboid.cpp | 86 | ||||
-rw-r--r-- | Cuboid.h | 4 | ||||
-rw-r--r-- | DisplayClass.cpp | 4 | ||||
-rw-r--r-- | Matrix.h | 45 | ||||
-rw-r--r-- | Trapezocube.cpp | 89 | ||||
-rw-r--r-- | Trapezocube.h | 5 | ||||
-rw-r--r-- | Triangle.h | 42 | ||||
-rw-r--r-- | Vector.h | 61 | ||||
-rw-r--r-- | Vertex.h | 25 | ||||
-rw-r--r-- | main.cpp | 2 |
15 files changed, 244 insertions, 412 deletions
diff --git a/BSPTree.cpp b/BSPTree.cpp index cb182a9..1dbb6a9 100644 --- a/BSPTree.cpp +++ b/BSPTree.cpp @@ -5,13 +5,13 @@ BSPTree::BSPTree(const std::list<Triangle> &triangles) : frontTree(0), backTree( if(triangles.empty()) return; - Vertex center = findCenter(triangles); - std::cout << "Center at (" << center.getX() << ", " << center.getY() << ", " << center.getZ() << ")" << std::endl; + vmml::vec3f center = findCenter(triangles); + std::cout << "Center at " << center << std::endl; const Triangle *planeT = findNearestTriangle(triangles, center); plane = Plane(*planeT); - std::cout << "The plane normal is (" << plane.getNormal().getX() << ", " << plane.getNormal().getY() << ", " << plane.getNormal().getZ() << ")" << std::endl; + std::cout << "The plane normal is " << plane.getNormal() << std::endl; std::list<Triangle> front, back; @@ -43,8 +43,8 @@ BSPTree::BSPTree(const std::list<Triangle> &triangles) : frontTree(0), backTree( } -Vertex BSPTree::findCenter(const std::list<Triangle> &triangles) { - Vertex v; +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(); @@ -53,17 +53,17 @@ Vertex BSPTree::findCenter(const std::list<Triangle> &triangles) { return v/triangles.size(); } -const Triangle* BSPTree::findNearestTriangle(const std::list<Triangle> &triangles, const Vertex &v) { +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().distanceSq(v); + float distanceSq = current->getCenter().squared_distance(v); for(++t; t != triangles.end(); ++t) { - float d = t->getCenter().distanceSq(v); + float d = t->getCenter().squared_distance(v); if(d < distanceSq) { current = &*t; @@ -71,8 +71,8 @@ const Triangle* BSPTree::findNearestTriangle(const std::list<Triangle> &triangle } } - Vertex tCenter = current->getCenter(); - std::cout << "Nearest triangle center at (" << tCenter.getX() << ", " << tCenter.getY() << ", " << tCenter.getZ() << ")" << std::endl; + vmml::vec3f tCenter = current->getCenter(); + std::cout << "Nearest triangle center at " << tCenter << std::endl; std::cout << "DistanceSq is " << distanceSq << std::endl; return current; @@ -4,24 +4,25 @@ #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(const Vertex &v) { + bool contains(const vmml::vec3f &v) { return (fabsf(normal.dot(v) - d) < 1E-6); } - bool isBehind(const Vertex &v) { + bool isBehind(const vmml::vec3f &v) { return (normal.dot(v) - d) < 0; } - bool isInFront(const Vertex &v) { + bool isInFront(const vmml::vec3f &v) { return (normal.dot(v) - d) > 0; } @@ -54,12 +55,12 @@ class BSPTree { } - const Vector& getNormal() { + const vmml::vec3f& getNormal() { return normal; } private: - Vector normal; + vmml::vec3f normal; float d; }; @@ -74,7 +75,7 @@ class BSPTree { } template<typename T> - void visit(const T& visitor, const Vector &v) { + void visit(const T& visitor, const vmml::vec3f &v) { if(plane.getNormal().dot(v) > 0) { if(frontTree) frontTree->visit(visitor, v); @@ -100,7 +101,7 @@ class BSPTree { } template<typename T> - void visit(T& visitor, const Vector &v) { + void visit(T& visitor, const vmml::vec3f &v) { if(plane.getNormal().dot(v) > 0) { if(frontTree) frontTree->visit(visitor, v); @@ -130,8 +131,8 @@ class BSPTree { 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 e755e45..428cc7a 100644 --- a/Cubehole.cpp +++ b/Cubehole.cpp @@ -1,162 +1,162 @@ #include "Cubehole.h" #include "gl.h" -std::list<Triangle> Cubehole::getTriangles(const Matrix &modelview) +std::list<Triangle> Cubehole::getTriangles(/*const Matrix &modelview*/) { 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); - - 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+innerdepth/2), c)); - - triangles.push_back(Triangle(Vertex(x+width/2, y+height/2, z+depth/2), - Vertex(x+width/2, y+height/2, z+innerdepth/2), - Vertex(x-width/2, y+height/2, z+innerdepth/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-innerdepth/2), c)); - - triangles.push_back(Triangle(Vertex(x+width/2, y+height/2, z-depth/2), - Vertex(x+width/2, y+height/2, z-innerdepth/2), - Vertex(x-width/2, y+height/2, z-innerdepth/2), c)); - - triangles.push_back(Triangle(Vertex(x- width/2, y+height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y+height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y+height/2, z-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x- width/2, y+height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y+height/2, z-innerdepth/2), - Vertex(x- width/2, y+height/2, z-innerdepth/2), c)); - - triangles.push_back(Triangle(Vertex(x+ width/2, y+height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x+ width/2, y+height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z-innerdepth/2), - Vertex(x+ width/2, y+height/2, z-innerdepth/2), c)); + c = vmml::vec4f(1.0, 0.0, 0.0, 0.5); + + 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+innerdepth/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+innerdepth/2), + vmml::vec3f(x-width/2, y+height/2, z+innerdepth/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-innerdepth/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-innerdepth/2), + vmml::vec3f(x-width/2, y+height/2, z-innerdepth/2), c)); + + triangles.push_back(Triangle(vmml::vec3f(x- width/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y+height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x- width/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y+height/2, z-innerdepth/2), + vmml::vec3f(x- width/2, y+height/2, z-innerdepth/2), c)); + + triangles.push_back(Triangle(vmml::vec3f(x+ width/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x+ width/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z-innerdepth/2), + vmml::vec3f(x+ width/2, y+height/2, z-innerdepth/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+innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x+width/2, y-height/2, z+depth/2), - Vertex(x+width/2, y-height/2, z+innerdepth/2), - Vertex(x-width/2, y-height/2, z+innerdepth/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-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x+width/2, y-height/2, z-depth/2), - Vertex(x+width/2, y-height/2, z-innerdepth/2), - Vertex(x-width/2, y-height/2, z-innerdepth/2), c)); - - triangles.push_back(Triangle(Vertex(x- width/2, y-height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y-height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y-height/2, z-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x- width/2, y-height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y-height/2, z-innerdepth/2), - Vertex(x- width/2, y-height/2, z-innerdepth/2), c)); - - triangles.push_back(Triangle(Vertex(x+ width/2, y-height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y-height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y-height/2, z-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x+ width/2, y-height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y-height/2, z-innerdepth/2), - Vertex(x+ width/2, y-height/2, z-innerdepth/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+innerdepth/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+innerdepth/2), + vmml::vec3f(x-width/2, y-height/2, z+innerdepth/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-innerdepth/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-innerdepth/2), + vmml::vec3f(x-width/2, y-height/2, z-innerdepth/2), c)); + + triangles.push_back(Triangle(vmml::vec3f(x- width/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y-height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x- width/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y-height/2, z-innerdepth/2), + vmml::vec3f(x- width/2, y-height/2, z-innerdepth/2), c)); + + triangles.push_back(Triangle(vmml::vec3f(x+ width/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y-height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x+ width/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y-height/2, z-innerdepth/2), + vmml::vec3f(x+ width/2, y-height/2, z-innerdepth/2), c)); // FrontInner face - c = Color(1.0, 1.0, 1.0, 0.5); + c = vmml::vec4f(1.0, 1.0, 1.0, 0.5); - triangles.push_back(Triangle(Vertex(x-innerwidth/2, y+height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y-height/2, z+innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x-innerwidth/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y-height/2, z+innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x-innerwidth/2, y-height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y-height/2, z+innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x-innerwidth/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y-height/2, z+innerdepth/2), c)); // BackInner face - c = Color(1.0, 0.5, 0.0, 0.5); + c = vmml::vec4f(1.0, 0.5, 0.0, 0.5); - triangles.push_back(Triangle(Vertex(x-innerwidth/2, y+height/2, z-innerdepth/2), - Vertex(x-innerwidth/2, y-height/2, z-innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x-innerwidth/2, y+height/2, z-innerdepth/2), + vmml::vec3f(x-innerwidth/2, y-height/2, z-innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x-innerwidth/2, y-height/2, z-innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z-innerdepth/2), - Vertex(x+innerwidth/2, y-height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x-innerwidth/2, y-height/2, z-innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z-innerdepth/2), + vmml::vec3f(x+innerwidth/2, y-height/2, z-innerdepth/2), c)); // LeftInner 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-innerwidth/2, y+height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y-height/2, z+innerdepth/2), - Vertex(x-innerwidth/2, y+height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x-innerwidth/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x-innerwidth/2, y+height/2, z-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x-innerwidth/2, y-height/2, z-innerdepth/2), - Vertex(x-innerwidth/2, y+height/2, z-innerdepth/2), - Vertex(x-innerwidth/2, y-height/2, z+innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x-innerwidth/2, y-height/2, z-innerdepth/2), + vmml::vec3f(x-innerwidth/2, y+height/2, z-innerdepth/2), + vmml::vec3f(x-innerwidth/2, y-height/2, z+innerdepth/2), c)); // RightInner face - triangles.push_back(Triangle(Vertex(x+innerwidth/2, y+height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y-height/2, z+innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z-innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x+innerwidth/2, y+height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y-height/2, z+innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z-innerdepth/2), c)); - triangles.push_back(Triangle(Vertex(x+innerwidth/2, y-height/2, z-innerdepth/2), - Vertex(x+innerwidth/2, y+height/2, z-innerdepth/2), - Vertex(x+innerwidth/2, y-height/2, z+innerdepth/2), c)); + triangles.push_back(Triangle(vmml::vec3f(x+innerwidth/2, y-height/2, z-innerdepth/2), + vmml::vec3f(x+innerwidth/2, y+height/2, z-innerdepth/2), + vmml::vec3f(x+innerwidth/2, y-height/2, z+innerdepth/2), c)); - for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { + /*for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { t->transform(modelview); - } + }*/ return triangles; } @@ -2,7 +2,6 @@ #define _CUBEHOLE_H_ #include "Triangle.h" -#include "Matrix.h" #include <list> @@ -43,7 +42,7 @@ class Cubehole } 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(/*const Matrix &modelview*/); private: float x, y, z, width, height, depth, innerwidth, innerdepth; @@ -60,77 +60,77 @@ 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(/*const Matrix &modelview*/) { 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)); + 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)); - for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { + /*for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { t->transform(modelview); - } + }*/ return triangles; } @@ -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(/*const Matrix &modelview*/);
private: float width, height, depth;
diff --git a/DisplayClass.cpp b/DisplayClass.cpp index f320f3c..b82b18e 100644 --- a/DisplayClass.cpp +++ b/DisplayClass.cpp @@ -1,7 +1,5 @@ #include "DisplayClass.h" -#include "Matrix.h" #include "gl.h" -//#include <algorithm> #include "Trapezocube.h" #include "BSPTree.h" @@ -89,7 +87,7 @@ void DisplayClass::renderScene(unsigned long delta) /*for(std::list<Triangle>::reverse_iterator t = triangles.rbegin(); t != triangles.rend(); ++t) { t->render(); }*/ - tree.visit(RenderVisitor(), Vector(0, 0, -1)); + tree.visit(RenderVisitor(), vmml::vec3f(0, 0, -1)); glEnd(); glFlush(); 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 24bcfc3..271db7d 100644 --- a/Trapezocube.cpp +++ b/Trapezocube.cpp @@ -1,78 +1,77 @@ #include "Trapezocube.h" + std::list<Triangle> Trapezocube::getTriangles(/*const Matrix &modelview*/) { 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(rotate); + // 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-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), c)); + 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), c)); + 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 - 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-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), 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)); - 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), 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 - 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-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), 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-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), c)); + 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), c)); + 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+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), 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 - 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-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), c)); + 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), 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)); // 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), 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+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-widthback /2, y-height/2, z-depth/2), - Vertex(x+widthback /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); diff --git a/Trapezocube.h b/Trapezocube.h index 3d64eaa..694ecde 100644 --- a/Trapezocube.h +++ b/Trapezocube.h @@ -2,7 +2,6 @@ #define _TRAPEZOCUBE_H_ #include "gl.h" -#include "Color.h" #include "Triangle.h" #include <list> @@ -44,12 +43,12 @@ class Trapezocube this->z = z; } void setRotate(float r) {rotate = r;} - void setColor(Color col) {color = col;} + void setColor(const vmml::vec4f &col) {color = col;} std::list<Triangle> getTriangles(/*const Matrix &modelview*/); private: float x, y, z, widthfront, widthback, height, depth, rotate; - Color color; + vmml::vec4f color; }; #endif /*_TRAPEZOCUBE_H_ */ @@ -2,64 +2,52 @@ #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(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;} - Vector 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); + 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 { + 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 49a5597..0000000 --- a/Vector.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef _VECTOR_H_ -#define _VECTOR_H_ - -#include <cmath> - -class Vector { - public: - Vector(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;} - - 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(); - } - - protected: - float x, y, z; -}; - -#endif /*_VECTOR_H_*/ - diff --git a/Vertex.h b/Vertex.h deleted file mode 100644 index 801d029..0000000 --- a/Vertex.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef _VERTEX_H_ -#define _VERTEX_H_ - -#include "Vector.h" - -class Vertex : public Vector -{ - public: - Vertex(float x0 = 0, float y0 = 0, float z0 = 0) : Vector(x0, y0, z0) {} - Vertex(const Vector &v) : Vector(v) {} - - float distanceSq(const Vertex &v) const { - Vector delta = *this - v; - - return delta.dot(delta); - } - - Vector operator-(const Vertex &v) const { - return Vector(x-v.x, y-v.y, z-v.z); - } - -}; - -#endif /*_VERTEX_H_*/ - @@ -34,7 +34,7 @@ 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); |