diff --git a/include/poly.h b/include/poly.h index 28377a6..39e9d81 100755 --- a/include/poly.h +++ b/include/poly.h @@ -28,6 +28,7 @@ Vec rotate_x(Vec& c); Vec rotate_y(Vec& c); Vec rotate_z(Vec& c); + Vec rotate_xyz(Vec out); void draw(); int numPoints(); void vardump(std::string whitespace); diff --git a/include/vec.h b/include/vec.h index d59c4f9..33a6232 100755 --- a/include/vec.h +++ b/include/vec.h @@ -20,7 +20,7 @@ Vec operator*(float s); Vec operator*(Vec& s); Vec operator/(float s); - Vec cross(Vec* v); + Vec cross(Vec& v); Vec normalize(); float length(); void print(); diff --git a/sources/poly.cpp b/sources/poly.cpp index 00ed918..ffef192 100755 --- a/sources/poly.cpp +++ b/sources/poly.cpp @@ -111,26 +111,46 @@ return Vec (r*cos(rot.z()), r*sin(rot.z()), c.z()); } +Vec Poly::rotate_xyz(Vec out) { + Vec ret = rotate_x(out); + ret = rotate_y(ret); + ret = rotate_z(ret); + return ret; +} + void Poly::draw() { - glBegin (GL_POLYGON); + unsigned int vcount = initpoints.size(); - //Vec v1 = *((Vec*)initpoints->get(2)) - *((Vec*)initpoints->get(0)); - //Vec v2 = *((Vec*)initpoints->get(1)) - *((Vec*)initpoints->get(0)); - //glNormal3fv( v1.cross(v2)+ *(Vec*)((*initpoints)[0]).c ); - //glNormal3fv( v1.cross(v2) + ((Vec*)initpoints->get(0))->c ); + //draw Poly + glBegin (GL_POLYGON); + for ( std::vector::size_type i = 0; i < vcount; i++ ) { + Vec cur = rotate_xyz(*initpoints.at(i) - *position); + //normal + Vec prev = rotate_xyz(*initpoints.at((i-1+vcount)%vcount) - *position); + Vec next = rotate_xyz(*initpoints.at((i+1) % vcount) - *position); + prev = prev - cur; + next = next - cur; + glNormal3fv(prev.cross(next).normalize().c); - Vec mv; - - for ( std::vector::size_type i = 0; i < initpoints.size(); i++ ) { - color->drawColor(); - Vec out = *(initpoints.at(i)) - *position; - out = rotate_x(out); - out = rotate_y(out); - out = rotate_z(out); - mv = *position + out; - glVertex3fv(mv.c); + color->drawColor(); + glVertex3fv((*position + cur).c); } + glEnd(); + + //draw Normals + for ( std::vector::size_type i = 0; i < vcount; i++ ) { + Vec cur = rotate_xyz(*initpoints.at(i) - *position); + //previous and next Vector of current Vector to calculate normal + Vec prev = rotate_xyz(*initpoints.at((i-1+vcount)%vcount) - *position); + Vec next = rotate_xyz(*initpoints.at((i+1) % vcount) - *position); + prev = prev - cur; + next = next - cur; + + glBegin (GL_LINES); + glVertex3fv((cur + *position).c); + glVertex3fv((cur + prev.cross(next).normalize() + *position).c); glEnd(); + } } int Poly::numPoints() { @@ -138,13 +158,15 @@ } void Poly::vardump(std::string whitespace) { - printf("%sPoly(%d points, center: [%f, %f, %f], rotation: [%f, %f, %f], color: [%f, %f, %f])\n", - whitespace.c_str(), initpoints.size(), position->x(), position->y(), position->z(), - rotation->x(), rotation->y(), rotation->z(), color->r, color->g, color->b); + printf("%sPoly(%d points, center: [%f, %f, %f], rotation: [%f, %f, %f], color: [%f, %f, %f])\n", + whitespace.c_str(), initpoints.size(), + position->x(), position->y(), position->z(), + rotation->x(), rotation->y(), rotation->z(), + color->r, color->g, color->b); - for (std::vector::size_type i = 0 ; i < initpoints.size(); i++ ) { - initpoints.at(i)->vardump(whitespace+" "); - } + for (std::vector::size_type i = 0 ; i < initpoints.size(); i++ ) { + initpoints.at(i)->vardump(whitespace+" "); + } } void Poly::addPoint(Vec* p) { diff --git a/sources/vec.cpp b/sources/vec.cpp index de2c40f..c56c745 100755 --- a/sources/vec.cpp +++ b/sources/vec.cpp @@ -66,8 +66,8 @@ return Vec(x()/s, y()/s, z()/s); } -Vec 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 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 Vec::normalize() {