diff --git a/include/model.h b/include/model.h index 0903d45..064e288 100755 --- a/include/model.h +++ b/include/model.h @@ -1,5 +1,5 @@ #ifndef _MODEL_H -#define _MODDEL_H +#define _MODEL_H #include "vec.h" #include "poly.h" @@ -27,5 +27,7 @@ void vardump(std::string whitespace); void setDelta(float d); float getDelta(); + void showNormals(); + void hideNormals(); }; #endif diff --git a/include/poly.h b/include/poly.h index 2dbe5de..600a530 100755 --- a/include/poly.h +++ b/include/poly.h @@ -18,6 +18,7 @@ Vec* normal; Color* color; float delta; + bool drawNormals; public: Poly(); Poly(std::vector & _points); @@ -39,5 +40,8 @@ void setColor(Color c); Color* getColor(); void setDelta(float d); + void showNormals(); + void hideNormals(); + void calcNormal(); }; #endif diff --git a/sources/main.cpp b/sources/main.cpp index 1873d5d..a9a5cfa 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -213,80 +213,68 @@ Color green (0,1,0); Color cyan (0,1,1); - Vec* p0 = new Vec(0 ,0 ,0 ); - Vec* p1 = new Vec(10,0 ,0 ); - Vec* p2 = new Vec(10,10,0 ); - Vec* p3 = new Vec(0 ,10,0 ); + Vec* ulh = new Vec(0 ,0 ,0 ); + Vec* ulv = new Vec(0 ,0 ,10); + Vec* urv = new Vec(10,0 ,10); + Vec* urh = new Vec(10,0 ,0 ); - Vec* p4 = new Vec(0 ,0 ,10); - Vec* p5 = new Vec(10,0 ,10); - Vec* p6 = new Vec(10,10,10); - Vec* p7 = new Vec(0 ,10,10); + Vec* olh = new Vec(0 ,10,0 ); + Vec* olv = new Vec(0 ,10,10); + Vec* orv = new Vec(10,10,10); + Vec* orh = new Vec(10,10,0 ); Vec cen (5,5,5); Model* cube = new Model(); - /* - * - * 4---7 - * |pp5| - * 4---0---3---7---4 - * |pp2|pp0|pp3|pp1| - * 5---1---2---6---5 - * |pp4| - * 5---6 - */ - - std::vector pp0; - std::vector pp1; - std::vector pp2; - std::vector pp3; - std::vector pp4; - std::vector pp5; + std::vector bottom; + std::vector left; + std::vector right; + std::vector top; + std::vector rear; + std::vector front; - pp0.push_back(p0); - pp0.push_back(p3); - pp0.push_back(p2); - pp0.push_back(p1); + bottom.push_back(ulh); + bottom.push_back(urh); + bottom.push_back(urv); + bottom.push_back(ulv); - pp1.push_back(p4); - pp1.push_back(p5); - pp1.push_back(p6); - pp1.push_back(p7); + left.push_back(ulh); + left.push_back(ulv); + left.push_back(olv); + left.push_back(olh); - pp2.push_back(p4); - pp2.push_back(p0); - pp2.push_back(p1); - pp2.push_back(p5); + right.push_back(urh); + right.push_back(orh); + right.push_back(orv); + right.push_back(urv); - pp3.push_back(p2); - pp3.push_back(p3); - pp3.push_back(p7); - pp3.push_back(p6); + top.push_back(olh); + top.push_back(olv); + top.push_back(orv); + top.push_back(orh); - pp4.push_back(p1); - pp4.push_back(p2); - pp4.push_back(p6); - pp4.push_back(p5); + rear.push_back(ulh); + rear.push_back(olh); + rear.push_back(orh); + rear.push_back(urh); - pp5.push_back(p3); - pp5.push_back(p0); - pp5.push_back(p4); - pp5.push_back(p7); + front.push_back(ulv); + front.push_back(urv); + front.push_back(orv); + front.push_back(olv); - cube->addFace(new Poly(pp0, red)); - cube->addFace(new Poly(pp1, green)); - cube->addFace(new Poly(pp2, cyan)); - cube->addFace(new Poly(pp3, blue)); - cube->addFace(new Poly(pp4, yellow)); - cube->addFace(new Poly(pp5, white)); + cube->addFace(new Poly(bottom, red)); + cube->addFace(new Poly(left, green)); + cube->addFace(new Poly(right, cyan)); + cube->addFace(new Poly(top, blue)); + cube->addFace(new Poly(rear, yellow)); + cube->addFace(new Poly(front, white)); cube->setCenter(cen); - cube->setDelta(PI); + cube->showNormals(); - //char* modelfile = "obj/sample.obj"; char* modelfile = "obj/untitled.obj"; //char* modelfile = "obj/teapot.obj"; @@ -294,6 +282,7 @@ printf("imported %s : %d faces\n", modelfile, myModel->numFaces()); Vec myCen = Vec(0,0,0); myModel->setCenter(myCen); + myModel->setDelta(PI); //myModel->vardump(""); @@ -399,7 +388,6 @@ myModel->rotate(Vec(0.2f, 0.3f, 1.2f)); - //////////////// // Check events: SDL_Event event; @@ -491,7 +479,7 @@ //////////////// // Swap buffers: - mysleep(2000); // Just don't kill computer resources ;) + //mysleep(2000); // Just don't kill computer resources ;) SDL_GL_SwapBuffers(); } diff --git a/sources/model.cpp b/sources/model.cpp index 89448eb..1156db4 100755 --- a/sources/model.cpp +++ b/sources/model.cpp @@ -87,3 +87,15 @@ float Model::getDelta() { return delta; } + +void Model::showNormals() { + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->showNormals(); + } +} + +void Model::hideNormals() { + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->hideNormals(); + } +} diff --git a/sources/poly.cpp b/sources/poly.cpp index ab5367a..32576a9 100755 --- a/sources/poly.cpp +++ b/sources/poly.cpp @@ -11,6 +11,7 @@ using namespace std; Poly::Poly() { + drawNormals = false; delta = 0.0f; position = new Vec(0,0,0); rotation = new Vec(0,0,0); @@ -25,6 +26,7 @@ Poly::Poly(std::vector & _points, Color _color) { delta = 0.0f; + drawNormals = false; position = new Vec(0,0,0); rotation = new Vec(0,0,0); color = _color.clone(); @@ -35,13 +37,14 @@ setCenter(calcCenter()); - //normal - Vec cur = rotate_xyz(*initpoints.at(1) - *position); - Vec prev = rotate_xyz(*initpoints.at(0) - *position); - Vec next = rotate_xyz(*initpoints.at(2) - *position); - prev = prev - cur; - next = next - cur; - normal = prev.cross(next).normalize().clone(); +} + +void Poly::calcNormal() { + delete normal; + /* calculate face normal */ + Vec b = *initpoints.at(0)-*initpoints.at(1); + Vec a = *initpoints.at(2)-*initpoints.at(1); + normal = a.cross(b).normalize().clone(); } Poly::~Poly() { @@ -132,6 +135,7 @@ void Poly::draw() { Vec n = Vec(0.0f, 0.0f, 0.0f); + Color nc = Color(1.0f, 0.0f, 1.0f); std::vector::size_type vcount = initpoints.size(); @@ -146,27 +150,69 @@ glRotatef(rotation->y(), 0.0f, 1.0f, 0.0f); glRotatef(rotation->z(), 0.0f, 0.0f, 1.0f); + if ( drawNormals ) { + /* draw face normal */ + glBegin(GL_LINES); + nc.drawColor(); + glVertex3fv((calcCenter()-*position).c); + glVertex3fv((calcCenter()+(*normal * 3.0f)-*position).c); + glEnd(); + + /* draw vertex normals */ + for ( std::vector::size_type i = 0; i < vcount; i++ ) { + if ( delta <= 0.0f ) { + /* face normal */ + glBegin(GL_LINES); + nc.drawColor(); + glVertex3fv((*initpoints.at(i)-*position).c); + glVertex3fv((*initpoints.at(i)+(*normal * 1.5f)-*position).c); + glEnd(); + } else { + /* vertex normal (delta) */ + n.c[0] = 0.0f; + n.c[1] = 0.0f; + n.c[2] = 0.0f; + + n = n+*normal; + + for ( std::vector::size_type j = 0; j < initpoints.at(i)->faces.size(); j++ ) { + if ( ((Poly*)initpoints.at(i)->faces.at(j))->normal->angle(*normal) < delta ) { + n = n+*(((Poly*)initpoints.at(i)->faces.at(j))->normal); + } + } + + glBegin(GL_LINES); + nc.drawColor(); + glVertex3fv((*initpoints.at(i)-*position).c); + glVertex3fv((*initpoints.at(i)+(*normal * 1.5f)-*position).c); + glEnd(); + } + } + } + glBegin (GL_POLYGON); for ( std::vector::size_type i = 0; i < vcount; i++ ) { - /* face normal */ - //glNormal3fv(normal->c); + if ( delta <= 0.0f ) { + /* face normal */ + glNormal3fv(normal->c); + } else { + /* vertex normal (delta) */ + n.c[0] = 0.0f; + n.c[1] = 0.0f; + n.c[2] = 0.0f; - /* TODO: vertex normal */ - n.c[0] = 0.0f; - n.c[1] = 0.0f; - n.c[2] = 0.0f; + n = n+*normal; - n = n+*normal; - - for ( std::vector::size_type j = 0; j < initpoints.at(i)->faces.size(); j++ ) { - if ( ((Poly*)initpoints.at(i)->faces.at(j))->normal->angle(*normal) < delta ) { - n = n+*(((Poly*)initpoints.at(i)->faces.at(j))->normal); + for ( std::vector::size_type j = 0; j < initpoints.at(i)->faces.size(); j++ ) { + if ( ((Poly*)initpoints.at(i)->faces.at(j))->normal->angle(*normal) < delta ) { + n = n+*(((Poly*)initpoints.at(i)->faces.at(j))->normal); + } } - } - glNormal3fv(n.c); + glNormal3fv(n.c); + } color->drawColor(); glVertex3fv((*initpoints.at(i) - *position).c); @@ -198,6 +244,10 @@ void Poly::addPoint(Vec* p) { p->registerFace(this); initpoints.push_back(p); + + if ( initpoints.size() > 2 ) { + this->calcNormal(); + } } void Poly::setColor(Color c) { @@ -211,3 +261,11 @@ void Poly::setDelta(float d) { delta = d; } + +void Poly::showNormals() { + drawNormals = true; +} + +void Poly::hideNormals() { + drawNormals = false; +}