From 59b00645ccfbade509b1d1694c9fcfc68e013a10 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 10 Dec 2009 00:32:11 +0100 Subject: Zu vmmlib migriert --- BSPTree.cpp | 20 ++--- BSPTree.h | 21 ++--- Color.h | 19 ----- Cubehole.cpp | 230 +++++++++++++++++++++++++++---------------------------- Cubehole.h | 3 +- Cuboid.cpp | 86 ++++++++++----------- Cuboid.h | 4 +- DisplayClass.cpp | 4 +- Matrix.h | 45 ----------- Trapezocube.cpp | 89 +++++++++++---------- Trapezocube.h | 5 +- Triangle.h | 42 ++++------ Vector.h | 61 --------------- Vertex.h | 25 ------ main.cpp | 2 +- 15 files changed, 244 insertions(+), 412 deletions(-) delete mode 100644 Color.h delete mode 100644 Matrix.h delete mode 100644 Vector.h delete mode 100644 Vertex.h 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 &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 front, back; @@ -43,8 +43,8 @@ BSPTree::BSPTree(const std::list &triangles) : frontTree(0), backTree( } -Vertex BSPTree::findCenter(const std::list &triangles) { - Vertex v; +vmml::vec3f BSPTree::findCenter(const std::list &triangles) { + vmml::vec3f v; for(std::list::const_iterator t = triangles.begin(); t != triangles.end(); ++t) { v += t->getCenter(); @@ -53,17 +53,17 @@ Vertex BSPTree::findCenter(const std::list &triangles) { return v/triangles.size(); } -const Triangle* BSPTree::findNearestTriangle(const std::list &triangles, const Vertex &v) { +const Triangle* BSPTree::findNearestTriangle(const std::list &triangles, const vmml::vec3f &v) { if(triangles.empty()) return 0; std::list::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 } } - 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; diff --git a/BSPTree.h b/BSPTree.h index 78ec81f..4965318 100644 --- a/BSPTree.h +++ b/BSPTree.h @@ -4,24 +4,25 @@ #include "Triangle.h" #include #include +#include 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 - 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 - 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 triangles; BSPTree *frontTree, *backTree; - static Vertex findCenter(const std::list &triangles); - static const Triangle* findNearestTriangle(const std::list &triangles, const Vertex &v); + static vmml::vec3f findCenter(const std::list &triangles); + static const Triangle* findNearestTriangle(const std::list &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 Cubehole::getTriangles(const Matrix &modelview) +std::list Cubehole::getTriangles(/*const Matrix &modelview*/) { std::list 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::iterator t = triangles.begin(); t != triangles.end(); ++t) { + /*for(std::list::iterator t = triangles.begin(); t != triangles.end(); ++t) { t->transform(modelview); - } + }*/ return triangles; } diff --git a/Cubehole.h b/Cubehole.h index 3f8f689..9da2026 100644 --- a/Cubehole.h +++ b/Cubehole.h @@ -2,7 +2,6 @@ #define _CUBEHOLE_H_ #include "Triangle.h" -#include "Matrix.h" #include @@ -43,7 +42,7 @@ class Cubehole } void setSize(float w, float h, float d); void setPos(float x, float y, float z); - std::list getTriangles(const Matrix &modelview); + std::list getTriangles(/*const Matrix &modelview*/); private: float x, y, z, width, height, depth, innerwidth, innerdepth; diff --git a/Cuboid.cpp b/Cuboid.cpp index b53dada..ed0de5d 100644 --- a/Cuboid.cpp +++ b/Cuboid.cpp @@ -60,77 +60,77 @@ void Cuboid::setPos(float x, float y, float z) this->z = z; } -std::list Cuboid::getTriangles(const Matrix &modelview) +std::list Cuboid::getTriangles(/*const Matrix &modelview*/) { std::list 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::iterator t = triangles.begin(); t != triangles.end(); ++t) { + /*for(std::list::iterator t = triangles.begin(); t != triangles.end(); ++t) { t->transform(modelview); - } + }*/ return triangles; } diff --git a/Cuboid.h b/Cuboid.h index ae9f2a1..7c4b4c5 100644 --- a/Cuboid.h +++ b/Cuboid.h @@ -2,9 +2,7 @@ #define _CUBOID_H_ #include "Triangle.h" -#include "Matrix.h" -#include #include 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 getTriangles(const Matrix &modelview); + std::list 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 #include "Trapezocube.h" #include "BSPTree.h" @@ -89,7 +87,7 @@ void DisplayClass::renderScene(unsigned long delta) /*for(std::list::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 Trapezocube::getTriangles(/*const Matrix &modelview*/) { std::list 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::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 @@ -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 getTriangles(/*const Matrix &modelview*/); 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 1231ece..c053a8d 100644 --- a/Triangle.h +++ b/Triangle.h @@ -2,64 +2,52 @@ #define _TRIANGLE_H_ #include "gl.h" -#include "Color.h" -#include "Vector.h" -#include "Vertex.h" -#include "Matrix.h" +#include +#include 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 - -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_*/ - diff --git a/main.cpp b/main.cpp index 5f24b9a..b92c2d6 100644 --- a/main.cpp +++ b/main.cpp @@ -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); -- cgit v1.2.3