diff options
-rw-r--r-- | DisplayClass.cpp | 43 | ||||
-rw-r--r-- | Trapezocube.cpp | 42 | ||||
-rw-r--r-- | Trapezocube.h | 6 | ||||
-rw-r--r-- | Triangle.h | 19 | ||||
-rw-r--r-- | Vector.h | 9 | ||||
-rw-r--r-- | main.cpp | 12 |
6 files changed, 97 insertions, 34 deletions
diff --git a/DisplayClass.cpp b/DisplayClass.cpp index 2741fce..35083d9 100644 --- a/DisplayClass.cpp +++ b/DisplayClass.cpp @@ -1,7 +1,7 @@ #include "DisplayClass.h" #include "Matrix.h"
#include "gl.h" -#include <algorithm> +//#include <algorithm> #include "Trapezocube.h"
@@ -20,28 +20,35 @@ DisplayClass::DisplayClass() { void DisplayClass::renderScene(unsigned long delta)
{ -// Cubehole cubeing(3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5); - Trapezocube cubeing(2.0, 1.5, 3.0, 0.5, 0.0, 0.0, 0.0, 90); + //Cubehole cubeing(3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5); + Trapezocube cubeing1(11.0, 10.0, 10.0, 0.5, 0.0, 0.0, 1.75, 0); + Trapezocube cubeing2(10.0, 9.0, 9.0, 0.5, 0.0, 0.0, 2.25, 90); + Trapezocube cubeing3(9.0, 8.0, 8.0, 0.5, 0.0, 0.0, 2.75, 180); + Trapezocube cubeing4(8.0, 7.0, 7.0, 0.5, 0.0, 0.0, 3.25, 270); + Trapezocube cubeing5(7.0, 6.0, 6.0, 0.5, 0.0, 0.0, 3.75, 0); + Trapezocube cubeing6(6.0, 5.0, 5.0, 0.5, 0.0, 0.0, 4.25, 90); + Trapezocube cubeing7(5.0, 4.0, 4.0, 0.5, 0.0, 0.0, 4.75, 180); + Trapezocube cubeing8(4.0, 3.0, 3.0, 0.5, 0.0, 0.0, 5.25, 270); static float angle = 0.0;
- angle += delta*0.05;
+ angle += delta*0.025;
if(angle >= 360)
angle -= 360;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- Matrix p(GL_PROJECTION_MATRIX);
- Matrix m;
+ //atrix 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
+ glTranslatef(0.0, 0.0, -30.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); - m.store(); // Save current transformation + //m.store(); // Save current transformation /*for(int i = 0; i < 5; ++i) { for(int j = 0; j < 5; ++j) {
@@ -51,10 +58,24 @@ void DisplayClass::renderScene(unsigned long delta) } } }*/ - std::list<Triangle> ct = cubeing.getTriangles(m); - triangles.splice(triangles.end(), ct); + std::list<Triangle> ct1 = cubeing1.getTriangles(); + triangles.splice(triangles.end(), ct1); + std::list<Triangle> ct2 = cubeing2.getTriangles(); + triangles.splice(triangles.end(), ct2); + std::list<Triangle> ct3 = cubeing3.getTriangles(); + triangles.splice(triangles.end(), ct3); + std::list<Triangle> ct4 = cubeing4.getTriangles(); + triangles.splice(triangles.end(), ct4); + std::list<Triangle> ct5 = cubeing5.getTriangles(); + triangles.splice(triangles.end(), ct5); + std::list<Triangle> ct6 = cubeing6.getTriangles(); + triangles.splice(triangles.end(), ct6); + std::list<Triangle> ct7 = cubeing7.getTriangles(); + triangles.splice(triangles.end(), ct7); + std::list<Triangle> ct8 = cubeing8.getTriangles(); + triangles.splice(triangles.end(), ct8); - glLoadIdentity();
+ //glLoadIdentity();
glBegin(GL_TRIANGLES); for(std::list<Triangle>::reverse_iterator t = triangles.rbegin(); t != triangles.rend(); ++t) { diff --git a/Trapezocube.cpp b/Trapezocube.cpp index e9877de..24bcfc3 100644 --- a/Trapezocube.cpp +++ b/Trapezocube.cpp @@ -1,27 +1,32 @@ #include "Trapezocube.h" -std::list<Triangle> Trapezocube::getTriangles(const Matrix &modelview) +std::list<Triangle> Trapezocube::getTriangles(/*const Matrix &modelview*/) { std::list<Triangle> triangles; // width, height, depth - //glRotatef(rotate, 0.0, 1.0, 0.0); + + glPushMatrix(); + glLoadIdentity(); + glRotatef(rotate, 0.0, 1.0, 0.0); + Matrix rotation; + glPopMatrix(); // Front face Color 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)); + 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(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)); + Vertex(x+widthfront/2, y-height/2, z+depth/2), + Vertex(x+widthfront/2, y+height/2, z+depth/2), c)); // Back face c = Color(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)); + 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(Vertex(x-widthback/2, y-height/2, z-depth/2), Vertex(x+widthback/2, y+height/2, z-depth/2), @@ -30,13 +35,13 @@ std::list<Triangle> Trapezocube::getTriangles(const Matrix &modelview) // Left face c = Color(0.0, 1.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(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(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)); + Vertex(x-widthfront/2, y-height/2, z+depth/2), + Vertex(x-widthback /2, y+height/2, z-depth/2), c)); // Right face @@ -62,15 +67,16 @@ std::list<Triangle> Trapezocube::getTriangles(const Matrix &modelview) // Bottom 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)); + 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(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)); + Vertex(x-widthback /2, y-height/2, z-depth/2), + Vertex(x+widthback /2, y-height/2, z-depth/2), c)); for(std::list<Triangle>::iterator t = triangles.begin(); t != triangles.end(); ++t) { - t->transform(modelview); + t->transform(rotation); + //t->transform(modelview); } return triangles; diff --git a/Trapezocube.h b/Trapezocube.h index b2cdd9a..3d64eaa 100644 --- a/Trapezocube.h +++ b/Trapezocube.h @@ -9,8 +9,8 @@ class Trapezocube { public: - Trapezocube(): widthfront(0), widthback(0), height(0), depth(0), x(0), y(0), z(0) {} - Trapezocube(float widthfront, float widthback, float height, float depth): x(0), y(0), z(0){ + 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) { @@ -45,7 +45,7 @@ class Trapezocube } void setRotate(float r) {rotate = r;} void setColor(Color col) {color = col;} - std::list<Triangle> getTriangles(const Matrix &modelview); + std::list<Triangle> getTriangles(/*const Matrix &modelview*/); private: float x, y, z, widthfront, widthback, height, depth, rotate; @@ -20,9 +20,26 @@ class Triangle const Vertex& getVertex(int i) const {return v[i];} const Color& getColor() const {return c;} + Vertex getNormal() const { + Vector v1 = v[0]-v[2]; + Vector v2 = v[0]-v[1]; + + return v1.cross(v2).normalize(); + } + void render() const { - glColor4f(c.getR(), c.getG(), c.getB(), c.getA()); + //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); + Vector normal = getNormal(); + glNormal3f(normal.getX(), normal.getY(), normal.getZ()); + for(int i = 0; i < 3; ++i) { glVertex3f(v[i].getX(), v[i].getY(), v[i].getZ()); @@ -2,6 +2,7 @@ #define _VECTOR_H_ #include "Vertex.h" +#include <math.h> class Vector : public Vertex { public: @@ -33,6 +34,14 @@ class Vector : public Vertex { 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) { @@ -18,7 +18,7 @@ void initGL(bool multisample); void resize(int width, int height); void initGL(bool multisample) { - glClearColor(0.0, 0.0, 0.0, 1.0); + glClearColor(1.0, 0.85, 0.06, 1.0); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); @@ -32,6 +32,16 @@ void initGL(bool multisample) { #endif resize(DEFAULT_WIDTH, DEFAULT_HEIGHT); + + glEnable(GL_LIGHTING); + 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); } void resize(int width, int height) |