diff --git a/sources/main.cpp b/sources/main.cpp index 915ad5e..fd7625d 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -40,6 +40,7 @@ #define WINDOW_ZETADEPTH 24 // z-buffer depth (in bits) #define SURFACE_COUNT 6 +#define PI 3.14159265f /////////////////////////////////////////////////////////////////////////// @@ -48,7 +49,6 @@ /////////////////////////////////////////////////////////////////////////// // CLASSES -float scale = 10.0f; class Color { public: @@ -70,35 +70,68 @@ class Vec { public: - float x; - float y; - float z; + float c[3]; Vec(float vx, float vy, float vz) { - x = vx; - y = vy; - z = vz; + c[0] = vx; + c[1] = vy; + c[2] = vz; + } + + float x() { + return c[0]; + } + + float y() { + return c[1]; + } + + float z() { + return c[2]; + } + + float* getCoords() { + return c; + } + + Vec(Vec* v) { + Vec (v->c[0], v->c[1], v->c[2]); } Vec operator+(Vec v) { - Vec r = Vec(x + v.x, y + v.y, z + v.z); + Vec r = Vec(c[0] + v.c[0], c[1] + v.c[1], c[2] + v.c[2]); return r; } + void add(Vec a) { + c[0] += a.c[0]; + c[1] += a.c[1]; + c[2] += a.c[2]; + } + Vec operator-(Vec v) { - Vec r = Vec(x - v.x, y -v.y, z - v.z); + Vec r = Vec(c[0] - v.c[0], c[1] - v.c[1], c[2] - v.c[2]); return r; } Vec operator*(float s) { - Vec r = Vec(x*s, y*s, z*s); + Vec r = Vec(c[0]*s, c[1]*s, c[2]*s); return r; } + Vec operator*(Vec s) { + Vec r = Vec(x()*s.x(), y()*s.y(), z()*s.z()); + } + + Vec operator/(float s) { + Vec r = Vec(c[0]/s, c[1]/s, c[2]/s); + return r; + } + Vec* cross(Vec v) { - Vec* r = new Vec(y * v.z - z * v.y, - z * v.x - x * v.z, - x * v.y - y * v.x); + Vec* r = new Vec(c[1] * v.c[2] - c[2] * v.c[1], + c[2] * v.c[0] - c[0] * v.c[2], + c[0] * v.c[1] - c[1] * v.c[0]); return r; } @@ -107,93 +140,161 @@ } float length() { - return sqrt(x*x+y*y+z*z); + return sqrt(c[0]*c[0]+c[1]*c[1]+c[2]*c[2]); } void print() { - printf (" vektor: %f, %f, %f\n", x, y, z); + printf (" vektor: %f, %f, %f\n", c[0], c[1], c[2]); }; Vec* clone() { - return new Vec(x, y, z); + return new Vec(c[0], c[1], c[2]); } }; +class Poly { + private: + Vec** initpoints; + //Vec* initrotation; + int size; -class Tri { + Vec* position; + Vec* rotation; + public: - Vec *p[3]; - Color *col; - Vec *normal; + Poly(Vec** _points, int _size) { + size = _size; + initpoints = _points; + //points = new Vec* [size]; + //for (int v = 0; v < size; v++) { + //create a copy of every point + //points[v] = _points[v]->clone(); + //} - Tri(Vec* p0, Vec* p1, Vec* p2, Color* c) { - p[0] = p0; - p[1] = p1; - p[2] = p2; - col = c; - Vec v1 = (*p[1] - *p[0]); - Vec v2 = (*p[2] - *p[0]); - normal = (v1).cross(v2); + this->setCenter(this->calcCenter()); + rotation = new Vec(0,0,0); } - ~Tri() { - delete normal; + ~Poly() { + delete position; + delete rotation; + } + + /* + * set the position where the (rotation-)center of + * the polygon is located + */ + void setCenter(Vec* c) { + delete position; + position = c->clone(); + } + + /* + * initial rotation relative to rotation center + */ + /* Vec getInitRotation(Vec* v) { + Vec dir = (*position - *v); + return Vec(atan2(dir.y(), dir.z()), + atan2(dir.x(), dir.z()), + atan2(dir.y(), dir.x())); + }*/ + + Vec getSphereCoords(Vec* v) { + Vec dir = (*position - *v); + float bl = sqrt(dir.x()*dir.x()+dir.y()*dir.y()); + return Vec(dir.length(), + acos(dir.z()/dir.length()), + atan2(dir.y(), dir.x())); + } + + /* + * calculate the center relative to all vertices. + */ + Vec* calcCenter() { + Vec s (0,0,0); + for (int i = 0; i < size; i++) { + s = s+*initpoints[i]; + } + Vec* r = (s / size).clone(); + return r; + } + + /* + * reset the position of all vertices (ie. translate your + * polygon to an absolute position) + */ + void setPosition(Vec* newcenter) { + Vec diff = (*newcenter) - (*position); + delete position; + position = newcenter->clone(); + for (int i = 0; i < size; i++) { + initpoints[i]->add(diff); + } + } + + //additional rotation in arcs + void rotate(Vec rot) { + this->rotation->add(rot); } void draw() { - glBegin (GL_TRIANGLES); - this->setNormal(); - col->setColor(); - for (int v = 0; v < 3; v++) { - glVertex3f(scale * p[v]->x, scale * p[v]->y, scale * p[v]->z); - } + glBegin (GL_POLYGON); + //this->setNormal(); + //col->setColor(); + //printf("polygon: \n"); + for (int v = 0; v < size; v++) { + //initpoints[v]->print(); + //glVertex3fv(initpoints[v]->c); + } glEnd(); - this->drawNormals(); - this->print(); - } - void drawNormals() { - for (int v = 0; v < 3; v++) { - glBegin (GL_LINES); - glVertex3f(scale * p[v]->x, scale * p[v]->y, scale * p[v]->z); - glVertex3f(scale * p[v]->x + this->normal->x, - scale * p[v]->y + this->normal->y, - scale * p[v]->z + this->normal->z); - glEnd(); - } - } + glBegin (GL_POLYGON); + printf("polygonrotated: \n"); + for (int v = 0; v < size; v++) { + Vec dir = (*initpoints[v] - *position); + Vec initrot = getSphereCoords(initpoints[v]); + initrot.print(); + Vec rot = initrot + *rotation; + //initpoints[v]->print(); + //printf("%f", initpoints[v]->z()); + //TODO: WARNING: POLAR COORDINATES + Vec out (rot.x() * sin(rot.y()) * cos(rot.z()), + rot.x() * sin(rot.y()) * sin(rot.z()), + rot.x() * cos(rot.y())); - void setNormal() { - glNormal3f(this->normal->x, this->normal->y, this->normal->z); - } - - void print() { - printf("triangle: \n"); - for (int v = 0; v < 3; v++) { - p[v]->print(); - } - printf("normal: "); - normal->print(); - printf("\n"); - } + glVertex3fv((*position + out).c); + } + glEnd(); + } }; -class Rect { +/* + * lightsource + */ +class Light { public: - Tri* t1; - Tri* t2; - Rect(Vec* p1, Vec* p2, Vec* p3, Vec* p4, Color* c) { - t1 = new Tri (p1, p2, p3, c); - t2 = new Tri (p1, p3, p4, c); + Vec* pos; + Vec* dir; + Light(Vec* _pos, Vec* _dir) { + pos = _pos; + dir = _dir; } - ~Rect() { - delete t1; - delete t2; + + void trace() { + glBegin (GL_LINES); + glVertex3fv(pos->getCoords()); + glVertex3fv(dir->getCoords()); + glEnd(); } + void draw() { - t1->draw(); - t2->draw(); + //printf(GL_LIGHT0); + glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f); + float p [4] = {pos->x(), pos->y(), pos->z(), 1.0f}; + float d [4] = {dir->x(), dir->y(), dir->z(), 1.0f}; + glLightfv(GL_LIGHT0, GL_POSITION, p); + glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, d); } }; @@ -208,12 +309,6 @@ bool MouseButtonRight, MouseButtonLeft; bool done = false; // Quit application when true -//light info -float lightpos[] = {10.0f, 12.0f, 15.0f, 1.0f}; -float lightdir[] = {5.0f, 5.0f, 5.0f, 1.0f}; - -float lightdelta = 0.8f; - /* Tri** importModel(char* fname) { @@ -285,24 +380,49 @@ Color green (0,1,0); Color yellow (1,1,0); - /*Vec p0 (0,0,0); - Vec p1 (1,0,0); - Vec p2 (1,1,0); - Vec p3 (0,1,0); + Vec p0 (0 ,0 ,0 ); + Vec p1 (10,0 ,0 ); + Vec p2 (10,10,0 ); + Vec p3 (0 ,10,0 ); - Vec p4 (0,0,1); - Vec p5 (1,0,1); - Vec p6 (1,1,1); - Vec p7 (0,1,1); + Vec p4 (0 ,0 ,10); + Vec p5 (10,0 ,10); + Vec p6 (10,10,10); + Vec p7 (0 ,10,10); - Rect surf [SURFACE_COUNT] = { + /*Rect surf [SURFACE_COUNT] = { Rect (&p0, &p3, &p2, &p1, &red), Rect (&p4, &p5, &p6, &p7, &blue), - Rect (&p0, &p4, &p5, &p1, &green), + Rect (&p4, &p0, &p1, &p5, &green), Rect (&p2, &p3, &p7, &p6, &yellow), Rect (&p1, &p2, &p6, &p5, &black), Rect (&p3, &p0, &p4, &p7, &white) - };*/ + };*/ + + Vec* pp0[4] = {&p0, &p3, &p2, &p1}; + Vec* pp1[4] = {&p4, &p5, &p6, &p7}; + Vec* pp2[4] = {&p4, &p0, &p1, &p5}; + Vec* pp3[4] = {&p2, &p3, &p7, &p6}; + Vec* pp4[4] = {&p1, &p2, &p6, &p5}; + Vec* pp5[4] = {&p3, &p0, &p4, &p7}; + + Poly* polys [SURFACE_COUNT] = { + new Poly(pp0, 4), + new Poly(pp1, 4), + new Poly(pp2, 4), + new Poly(pp3, 4), + new Poly(pp4, 4), + new Poly(pp5, 4), + }; + + //Vec* pp [4] = {&p0, &p3, &p2, &p1}; + //Poly poly0 (pp,4); + + //light info + Vec pos0(10.0f, 12.0f, 15.0f); + Vec dir0(5.0f, 5.0f, 5.0f); + + Light light0(&pos0, &dir0); // Prepare configuration: int bitsPerColor = 8; @@ -378,16 +498,17 @@ glRotatef(deltay, 0.0f, 1.0f, 0.0f); // Rotation around the Y axis glRotatef(deltaz, 1.0f, 0.0f, 0.0f); - for (int i = 0; i < SURFACE_COUNT; i++) { - surf[i].draw(); + for (int i = 0; i < SURFACE_COUNT; i++) { + polys[i]->draw(); + polys[i]->rotate(Vec(0.0, 0.01, 0.01)); } - /* - glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f); - glLightfv(GL_LIGHT0, GL_POSITION, lightpos); - glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightdir); - */ - + //poly0.draw(); + //poly0.rotate(Vec(0.0, 0.01, 0.01)); + + light0.draw(); + light0.trace(); + //////////////// // Check events: SDL_Event event; @@ -418,13 +539,17 @@ // Update keyboard (used like a joypad): Uint8 *keystate = SDL_GetKeyState(NULL); if (keystate[SDLK_ESCAPE]) - done = true; + done = true; + + //background color if (keystate[SDLK_1]) glClearColor(0.5f, 0.0f, 0.0f, 1.0f); if (keystate[SDLK_2]) glClearColor(0.0f, 0.5f, 0.0f, 1.0f); if (keystate[SDLK_3]) glClearColor(0.0f, 0.0f, 0.5f, 1.0f); + + //scene rotation if (keystate[SDLK_LEFT]) deltay -= 2.0f; if (keystate[SDLK_RIGHT]) @@ -433,6 +558,8 @@ deltaz -= 2.0f; if (keystate[SDLK_DOWN]) deltaz += 2.0f; + + //light rotation /* if (keystate[SDLK_a]) lightpos[0]+=lightdelta; if (keystate[SDLK_d]) @@ -469,3 +596,85 @@ SDL_Quit(); return 0; } +/* +//DEPRECATED +class Tri{ + public: + Vec *p[3]; + Color *col; + Vec *normal; + + Tri(Vec* p0, Vec* p1, Vec* p2, Color* c) { + p[0] = p0; + p[1] = p1; + p[2] = p2; + col = c; + Vec v1 = (*p[1] - *p[0]); + Vec v2 = (*p[2] - *p[0]); + normal = (v1).cross(v2); + } + + ~Tri() { + delete normal; + } + + void draw() { + glBegin (GL_TRIANGLES); + this->setNormal(); + col->setColor(); + for (int v = 0; v < 3; v++) { + glVertex3f(p[v]->c[0], p[v]->c[1], p[v]->c[2]); + } + glEnd(); + this->drawNormals(); + this->print(); + } + + void drawNormals() { + for (int v = 0; v < 3; v++) { + glBegin (GL_LINES); + glVertex3f(p[v]->x(), p[v]->y(), p[v]->z()); + //glVertex3fv(((*p[v])*scale).getCoords()); + glVertex3f(p[v]->x() + this->normal->x(), + p[v]->y() + this->normal->y(), + p[v]->z() + this->normal->z()); + //glVertex3fv(((*p[v])*scale + this->normal).getCoords()); + glEnd(); + } + } + + void setNormal() { + glNormal3fv(this->normal->getCoords()); + } + + void print() { + printf("triangle: \n"); + for (int v = 0; v < 3; v++) { + p[v]->print(); + } + printf("normal: "); + normal->print(); + printf("\n"); + } +}; + + +//DEPRECATED +class Rect { + public: + Tri* t1; + Tri* t2; + Rect(Vec* p1, Vec* p2, Vec* p3, Vec* p4, Color* c) { + t1 = new Tri (p1, p2, p3, c); + t2 = new Tri (p1, p3, p4, c); + } + ~Rect() { + delete t1; + delete t2; + } + void draw() { + t1->draw(); + t2->draw(); + } +}; +*/