diff options
-rw-r--r-- | BSPTree.h | 38 | ||||
-rw-r--r-- | Cubehole.cpp | 175 | ||||
-rw-r--r-- | Cubehole.h | 71 | ||||
-rw-r--r-- | Cuboid.cpp | 6 | ||||
-rw-r--r-- | Cuboid.h | 2 | ||||
-rw-r--r-- | DisplayClass.cpp | 27 | ||||
-rw-r--r-- | DisplayClass.h | 4 | ||||
-rw-r--r-- | Trapezocube.cpp | 3 | ||||
-rw-r--r-- | Trapezocube.h | 15 | ||||
-rw-r--r-- | Triangle.h | 1 |
10 files changed, 88 insertions, 254 deletions
@@ -79,32 +79,21 @@ class BSPTree { template<typename T> void visit(const T& visitor, const vmml::vec3f &p) { - if(plane.isBehind(p)) { - if(frontTree) - frontTree->visit(visitor, p); - - for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { - visitor(*t); - } - - if(backTree) - backTree->visit(visitor, p); - } - else { - if(backTree) - backTree->visit(visitor, p); - - for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { - visitor(*t); - } - - if(frontTree) - frontTree->visit(visitor, 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, p); @@ -129,11 +118,6 @@ class BSPTree { } } - private: - Plane plane; - std::list<Triangle> triangles; - BSPTree *frontTree, *backTree; - static vmml::vec3f findCenter(const std::list<Triangle> &triangles); static const Triangle* findNearestTriangle(const std::list<Triangle> &triangles, const vmml::vec3f &v); }; diff --git a/Cubehole.cpp b/Cubehole.cpp index 428cc7a..0806f79 100644 --- a/Cubehole.cpp +++ b/Cubehole.cpp @@ -1,162 +1,19 @@ #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 - // Front face - vmml::vec4f c(0.0, 0.0, 1.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+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 = vmml::vec4f(1.0, 1.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-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 = vmml::vec4f(0.0, 1.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-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(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(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 = 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(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 = vmml::vec4f(1.0, 1.0, 1.0, 0.5); - - 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(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 = vmml::vec4f(1.0, 0.5, 0.0, 0.5); - - 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(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 = vmml::vec4f(0.0, 1.0, 0.0, 0.5); - - 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(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(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(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) { - t->transform(modelview); - }*/ + std::list<Triangle> tf = front.getTriangles(); + triangles.splice(triangles.end(), tf); + std::list<Triangle> tr = right.getTriangles(); + triangles.splice(triangles.end(), tr); + std::list<Triangle> tb = back.getTriangles(); + triangles.splice(triangles.end(), tb); + std::list<Triangle> tl = left.getTriangles(); + triangles.splice(triangles.end(), tl); return triangles; } @@ -190,17 +47,3 @@ float Cubehole::getPosZ() { return z; } - -void Cubehole::setSize(float w, float h, float d) -{ - width = w; - height = h; - depth = d; -} - -void Cubehole::setPos(float x, float y, float z) -{ - this->x = x; - this->y = y; - this->z = z; -} @@ -1,6 +1,7 @@ #ifndef _CUBEHOLE_H_ #define _CUBEHOLE_H_ +#include "Trapezocube.h" #include "Triangle.h" #include <list> @@ -9,43 +10,65 @@ class Cubehole { public: Cubehole(): width(0), height(0), depth(0), x(0), y(0), z(0), innerwidth(0), innerdepth(0) {} - Cubehole(float width, float height, float depth): x(0), y(0), z(0), innerwidth(0), innerdepth(0){ - setSize(width, height, depth); - } - Cubehole(float width, float height, float depth, float x, float y, float z): innerwidth(0), innerdepth(0) { - setSize(width, height, depth); - setPos(x, y, z); - } - Cubehole(float width, float height, float depth, float x, float y, float z, float innerwidth, float innerdepth) { - setSize(width, height, depth); - setPos(x, y, z); - setInnerSize(innerwidth, innerdepth); + + Cubehole(float width0, float height0, float depth0, float x0, float y0, float z0, + 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 setInnerSize(float iw, float id) - { + + float getInnerWidth() {return innerwidth;} + float getInnerDepth() {return innerdepth;} + + void setSize(float w, float h, float d, float iw, float id){ + width = w; + height = h; + depth = d; innerwidth = iw; innerdepth = id; + front.setSize(width, innerwidth, height, (depth-innerdepth)/2); + right.setSize(width, innerdepth, height, (width-innerwidth)/2); + 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); } - void setSize(float w, float h, float d); - void setPos(float x, float y, float z); - std::list<Triangle> getTriangles(/*const Matrix &modelview*/); + + 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(); private: float x, y, z, width, height, depth, innerwidth, innerdepth; + vmml::vec4f colorfront, colorleft, colorback, colorright; + Trapezocube front, right, back, left; }; @@ -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,7 +56,7 @@ 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 @@ -19,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 cf7a0f9..6d9fdee 100644 --- a/DisplayClass.cpp +++ b/DisplayClass.cpp @@ -5,16 +5,12 @@ DisplayClass::Renderer DisplayClass::renderer; - DisplayClass::DisplayClass() { - cubeing[0] = Trapezocube(11.0, 10.0, 10.0, 0.5, 0.0, 0.0, 1.75, 0); - cubeing[1] = Trapezocube(10.0, 9.0, 9.0, 0.5, 0.0, 0.0, 2.25, 90); - cubeing[2] = Trapezocube(9.0, 8.0, 8.0, 0.5, 0.0, 0.0, 2.75, 180); - cubeing[3] = Trapezocube(8.0, 7.0, 7.0, 0.5, 0.0, 0.0, 3.25, 270); - cubeing[4] = Trapezocube(7.0, 6.0, 6.0, 0.5, 0.0, 0.0, 3.75, 0); - cubeing[5] = Trapezocube(6.0, 5.0, 5.0, 0.5, 0.0, 0.0, 4.25, 90); - cubeing[6] = Trapezocube(5.0, 4.0, 4.0, 0.5, 0.0, 0.0, 4.75, 180); - cubeing[7] = Trapezocube(4.0, 3.0, 3.0, 0.5, 0.0, 0.0, 5.25, 270); + 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) @@ -27,20 +23,15 @@ void DisplayClass::renderScene(unsigned long delta) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // Clean up matrix - glTranslatef(0.0, 0.0, -30.0); // Then set up transformation + glTranslatef(0.0, 0.0, -10.0); // Then set up transformation glRotatef(angle, 0.0, 1.0, 0.0); glRotatef(angle*2, 1.0, 0.0, 0.0); glRotatef(angle*3, 0.0, 0.0, 1.0); glRotatef(-angle*5, 1.0, 1.0, 1.0); - std::list<Triangle> triangles; - - for(int i = 0; i < 8; ++i) { - std::list<Triangle> t = cubeing[i].getTriangles(); - triangles.splice(triangles.end(), t); - } - + std::list<Triangle> triangles = cubehole.getTriangles(); + BSPTree tree(triangles); vmml::mat4f transform, inverseTransform; @@ -48,7 +39,7 @@ void DisplayClass::renderScene(unsigned long delta) transform.inverse(inverseTransform); - vmml::vec3f viewPoint = inverseTransform*vmml::vec3f(0, 0, 0); + vmml::vec3f viewPoint = inverseTransform*vmml::vec3f::ZERO; glBegin(GL_TRIANGLES); tree.visit(renderer, viewPoint); diff --git a/DisplayClass.h b/DisplayClass.h index 0b24dc6..04ff651 100644 --- a/DisplayClass.h +++ b/DisplayClass.h @@ -1,7 +1,7 @@ #ifndef _DISPLAYCLASS_H_ #define _DISPLAYCLASS_H_ -#include "Trapezocube.h" +#include "Cubehole.h" class DisplayClass
{
@@ -19,7 +19,7 @@ class DisplayClass static Renderer renderer; - Trapezocube cubeing[8];
+ Cubehole cubehole;
};
#endif /*_DISPLAYCLASS_H_*/ diff --git a/Trapezocube.cpp b/Trapezocube.cpp index bfb82c3..8205ee6 100644 --- a/Trapezocube.cpp +++ b/Trapezocube.cpp @@ -1,7 +1,7 @@ #include "Trapezocube.h" -std::list<Triangle> Trapezocube::getTriangles(/*const Matrix &modelview*/) +std::list<Triangle> Trapezocube::getTriangles() { std::list<Triangle> triangles; // width, height, depth @@ -75,7 +75,6 @@ std::list<Triangle> Trapezocube::getTriangles(/*const Matrix &modelview*/) 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 694ecde..c291e21 100644 --- a/Trapezocube.h +++ b/Trapezocube.h @@ -9,17 +9,12 @@ class Trapezocube { public: 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): x(0), y(0), z(0), rotate(0){ - setSize(widthfront, widthback, height, depth); - } - Trapezocube(float widthfront, float widthback, float height, float depth, float x, float y, float z): rotate(0) { - setSize(widthfront, widthback, height, depth); - setPos(x, y, z); - } - Trapezocube(float widthfront, float widthback, float height, float depth, float x, float y, float z, float rotate) { + + 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); + setColor(col); } float getHeight() {return height;} float getWidthFront() {return widthfront;} @@ -29,6 +24,8 @@ class Trapezocube float getPosY() {return y;} float getPosZ() {return z;} float getRotate() {return rotate;} + vmml::vec4f getColor() {return color;} + void setSize(float wf, float wb, float h, float d) { widthfront = wf; @@ -44,7 +41,7 @@ class Trapezocube } void setRotate(float r) {rotate = r;} void setColor(const vmml::vec4f &col) {color = col;} - std::list<Triangle> getTriangles(/*const Matrix &modelview*/); + std::list<Triangle> getTriangles(); private: float x, y, z, widthfront, widthback, height, depth, rotate; @@ -24,6 +24,7 @@ class Triangle } void render() const { + glColor4fv(c.array); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c.array); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (c/2).array); |