diff --git a/sources/main.cpp b/sources/main.cpp index e9e600c..9ea5b45 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -61,9 +61,12 @@ b = _b; } - void setColor() { - printf("color: %f, %f, %f\n", r, g, b); - glColor4f (r, g, b, 1.0f); + void drawColor() { + glColor4f(r, g, b, 1.0f); + } + + Color* clone() { + return new Color(r,g,b); } }; @@ -78,6 +81,10 @@ c[2] = vz; } + Vec(Vec* v) { + Vec(v->x(), v->y(), v->z()); + } + float x() { return c[0]; } @@ -94,140 +101,136 @@ return c; } - Vec(Vec* v) { - Vec (v->c[0], v->c[1], v->c[2]); + Vec operator+(Vec& v) { + return Vec(x() + v.x(), y() + v.y(), z() + v.z()); } - Vec operator+(Vec v) { - 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.x(); + c[1] += a.y(); + c[2] += a.z(); } - 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(c[0] - v.c[0], c[1] - v.c[1], c[2] - v.c[2]); - return r; + Vec operator-(Vec& v) { + return Vec(x() - v.x(), y() - v.y(), z() - v.z()); } Vec operator*(float s) { - Vec r = Vec(c[0]*s, c[1]*s, c[2]*s); + Vec r = Vec(x()*s, y()*s, z()*s); return r; } - Vec operator*(Vec s) { + 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); + Vec r = Vec(x()/s, y()/s, z()/s); return r; } - Vec* cross(Vec v) { - 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; + Vec cross(Vec& v) { + return Vec(y() * v.z() - z() * v.y(), + z() * v.x() - x() * v.z(), + x() * v.y() - y() * v.x()); } Vec normalize() { - return *this * (1 / this->length()); + return *this * (1 / length()); } float length() { - return sqrt(c[0]*c[0]+c[1]*c[1]+c[2]*c[2]); + return sqrt(x()*x()+y()*y()+z()*z()); } void print() { - printf (" vektor: %f, %f, %f\n", c[0], c[1], c[2]); - }; + printf (" vektor: %f, %f, %f\n", x(), y(), z()); + } Vec* clone() { - return new Vec(c[0], c[1], c[2]); + return new Vec(x(), y(), z()); } }; - class Poly { private: Vec** initpoints; - //Vec* initrotation; int size; Vec* position; Vec* rotation; + Color* color; public: Poly(Vec** _points, int _size) { + //TODO constructor almost same as below 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(); - //} - this->setCenter(this->calcCenter()); - rotation = new Vec(0,0,0); + color = new Color (1,1,1); + + Vec c = calcCenter(); + setCenter(c); + + rotation = new Vec(0,0,0); + } + + Poly(Vec** _points, int _size, Color _color) { + size = _size; + initpoints = _points; + + color = _color.clone(); + + Vec c = calcCenter(); + setCenter(c); + + rotation = new Vec(0,0,0); } ~Poly() { delete position; delete rotation; + delete color; } /* * set the position where the (rotation-)center of * the polygon is located */ - void setCenter(Vec* c) { + void setCenter(Vec& c) { delete position; - position = c->clone(); + position = c.clone(); } /* * initial rotation relative to rotation center */ - Vec getInitRotation(Vec* v) { - return Vec(atan2(v->y(), v->z()), - atan2(v->x(), v->z()), - atan2(v->y(), v->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())); + Vec getInitRotation(Vec& v) { + return Vec(atan2(v.y(), v.z()), + atan2(v.x(), v.z()), + atan2(v.y(), v.x())); } /* * calculate the center relative to all vertices. */ - Vec* calcCenter() { + Vec calcCenter() { Vec s (0,0,0); for (int i = 0; i < size; i++) { s = s+*initpoints[i]; } - Vec* r = (s / ((float)size)).clone(); - return r; + return s / ((float)size); } /* * reset the position of all vertices (ie. translate your * polygon to an absolute position) */ - void setPosition(Vec* newcenter) { - Vec diff = (*newcenter) - (*position); + void setPosition(Vec newcenter) { + Vec diff = newcenter - (*position); delete position; - position = newcenter->clone(); + position = newcenter.clone(); for (int i = 0; i < size; i++) { initpoints[i]->add(diff); } @@ -235,68 +238,45 @@ //additional rotation in arcs void rotate(Vec rot) { - this->rotation->add(rot); + rotation->add(rot); } - Vec rotate_x(Vec c) { - Vec initrot = getInitRotation(&c); + Vec rotate_x(Vec& c) { + Vec initrot = getInitRotation(c); Vec rot = initrot + *rotation; float r = Vec(0, c.y(), c.z()).length(); return Vec (c.x(), r*cos(rot.x()), r*sin(rot.x())); } - Vec rotate_y(Vec c) { - Vec initrot = getInitRotation(&c); + Vec rotate_y(Vec& c) { + Vec initrot = getInitRotation(c); Vec rot = initrot + *rotation; float r = Vec(c.x(), 0, c.z()).length(); return Vec (r*sin(rot.y()), c.y(), r*cos(rot.y())); } - Vec rotate_z(Vec c) { - Vec initrot = getInitRotation(&c); + Vec rotate_z(Vec& c) { + Vec initrot = getInitRotation(c); Vec rot = initrot + *rotation; float r = Vec(c.x(), c.y(), 0).length(); return Vec (r*cos(rot.z()), r*sin(rot.z()), c.z()); } - void draw() { + void draw() { 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(); + + Vec v1 = *initpoints[2] - *initpoints[0]; + Vec v2 = *initpoints[1] - *initpoints[0]; + glNormal3fv( (v1.cross(v2)+ *initpoints[0]).c ); + + for (int v = 0; v < size; v++) { - glBegin (GL_POLYGON); - printf("polygonrotated: \n"); - for (int v = 0; v < size; v++) { - //Vec initrot = getSphereCoords(initpoints[v]); - //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())); - - //r = Vec(dir.x(), 0, dir.z()).length(); - //Vec out (r * sin(rot.y()), dir.y(), r*cos(rot.y())); - //Vec out (r * sin(rot.y())+dir(x), r*cos(rot(x))+dir.y(), r*cos(rot.y())+r*sin(rot.x())); - + color->drawColor(); Vec out = (*initpoints[v] - *position); - out = this->rotate_x(out); - out = this->rotate_y(out); - out = this->rotate_z(out); - + out = rotate_x(out); + out = rotate_y(out); + out = rotate_z(out); glVertex3fv((*position + out).c); - - //r = Vec(dir.x(), dir.y(), 0).length(); - //Vec out (r * cos (rot.z()), r * sin (rot.z()), dir.z()); - - //Vec out (dir.x(), dir.y(), dir.z()); - //glVertex3fv((*position + out).c); } glEnd(); } @@ -344,7 +324,8 @@ Model(Poly** _faces, int _size) { size = _size; faces = _faces; - this->setCenter(this->calcCenter()); + Vec c = calcCenter(); + setCenter(c); rotation = new Vec(0,0,0); } @@ -353,24 +334,22 @@ delete rotation; } - void setCenter(Vec* c) { + void setCenter(Vec& c) { delete position; - position = c->clone(); + position = c.clone(); } - Vec* calcCenter() { + Vec calcCenter() { Vec s(0,0,0); - for ( int i = 0; i < size; i++ ) { - s = s+*faces[i]->calcCenter(); - } - - Vec* r = (s/((float)size)).clone(); - return r; + Vec c = faces[i]->calcCenter(); + s = s + c; + } + return s/((float)size); } void rotate(Vec rot) { - this->rotation->add(rot); + rotation->add(rot); for ( int i = 0; i < size; i++ ) { faces[i]->rotate(rot); } @@ -391,6 +370,8 @@ faces[size-1] = face; } }; + + /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// @@ -401,7 +382,6 @@ bool MouseButtonRight, MouseButtonLeft; bool done = false; // Quit application when true -/* Model* importModel(char* fname) { FILE *fp; @@ -450,7 +430,7 @@ fclose(fp); return mm; } -*/ + /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// @@ -503,18 +483,18 @@ 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(pp0, 4, red), + new Poly(pp1, 4, green), + new Poly(pp2, 4, black), + new Poly(pp3, 4, blue), + new Poly(pp4, 4, yellow), new Poly(pp5, 4), }; - //Model* myModel = importModel("teapot.obj"); + Model* myModel = importModel("teapot.obj"); for (int x = 0; x < SURFACE_COUNT; x++) { - polys[x]->setCenter(&cen); + polys[x]->setCenter(cen); } //Vec* pp [4] = {&p0, &p3, &p2, &p1}; @@ -612,6 +592,7 @@ light0.trace(); //myModel->draw(); + //////////////// // Check events: SDL_Event event;