diff --git a/include/poly.h b/include/poly.h index 39e9d81..7fa226c 100755 --- a/include/poly.h +++ b/include/poly.h @@ -2,13 +2,14 @@ #define _POLY_H #include "vec.h" +#include "spoly.h" #include "color.h" #include #include using namespace std; -class Poly { +class Poly : SPoly { private: std::vector initpoints; int size; diff --git a/include/vec.h b/include/vec.h index 33a6232..fe8fbe3 100755 --- a/include/vec.h +++ b/include/vec.h @@ -1,8 +1,13 @@ #ifndef _VEC_H #define _VEC_H +#include "spoly.h" + #include +#include class Vec { + private: + std::vector faces; public: float c[3]; @@ -26,5 +31,6 @@ void print(); Vec* clone(); void vardump(std::string whitespace); + void registerFace(SPoly* p); }; #endif diff --git a/sources/main.cpp b/sources/main.cpp index b0f8f3a..0748e85 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -272,7 +272,7 @@ printf("imported %s : %d faces\n", modelfile, myModel->numFaces()); //myModel->setCenter(cen); - //myModel->vardump(""); + myModel->vardump(""); //light info Vec dir0(0.0f, 0.0f, 0.0f); diff --git a/sources/poly.cpp b/sources/poly.cpp index 6f3f102..cad63e3 100755 --- a/sources/poly.cpp +++ b/sources/poly.cpp @@ -27,7 +27,7 @@ color = _color.clone(); for (std::vector::size_type i = 0 ; i < _points.size(); i++ ) { - initpoints.push_back(_points.at(i)); + addPoint(_points.at(i)); } setCenter(calcCenter()); @@ -44,7 +44,7 @@ * the polygon is located */ void Poly::setCenter(Vec c) { - position = c.clone(); + position = c.clone(); } /* @@ -112,45 +112,31 @@ } Vec Poly::rotate_xyz(Vec out) { - Vec ret = rotate_x(out); - ret = rotate_y(ret); - ret = rotate_z(ret); - return ret; + Vec ret = rotate_x(out); + ret = rotate_y(ret); + ret = rotate_z(ret); + return ret; } void Poly::draw() { - unsigned int vcount = initpoints.size(); + unsigned int vcount = initpoints.size(); - //draw Poly - glBegin (GL_POLYGON); + 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 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); - color->drawColor(); - glVertex3fv((*position + cur).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() { @@ -158,18 +144,20 @@ } 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) { + p->registerFace(this); initpoints.push_back(p); } diff --git a/sources/vec.cpp b/sources/vec.cpp index c56c745..2874b30 100755 --- a/sources/vec.cpp +++ b/sources/vec.cpp @@ -88,4 +88,9 @@ void Vec::vardump(std::string whitespace) { printf("%sVec(%f, %f, %f)\n", whitespace.c_str(), c[0], c[1], c[2]); + printf("%s linked to %d faces\n", whitespace.c_str(), faces.size()); +} + +void Vec::registerFace(SPoly* p) { + faces.push_back(p); }