diff --git a/Makefile b/Makefile index c0c3d5a..f88ed22 100755 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ endif ifeq "$(DEBUG)" "yes" - CXXFLAGS := $(CXXFLAGS) -DDEBUG + CXXFLAGS := $(CXXFLAGS) -g -DDEBUG endif OBJECTS = main.o array.o poly.o model.o light.o vec.o color.o diff --git a/include/model.h b/include/model.h index 33b49c6..fef4ce3 100755 --- a/include/model.h +++ b/include/model.h @@ -4,22 +4,25 @@ #include "array.h" #include "vec.h" #include "poly.h" +#include class Model { private: - Array* faces; + std::vector faces; Vec* position; Vec* rotation; public: - Model(Array* _faces); + Model(); + Model(std::vector & _faces); ~Model(); void setCenter(Vec& c); Vec calcCenter(); void rotate(Vec rot); void draw(); void addFace(Poly* face); - int numberOfFaces(); + int numFaces(); + void vardump(std::string whitespace); }; #endif diff --git a/include/poly.h b/include/poly.h index 70c2935..30ead36 100755 --- a/include/poly.h +++ b/include/poly.h @@ -1,20 +1,24 @@ #ifndef _POLY_H #define _POLY_H -#include "array.h" #include "vec.h" #include "color.h" +#include +#include + +using namespace std; class Poly { private: - Array* initpoints; + std::vector initpoints; int size; Vec* position; Vec* rotation; Color* color; public: - Poly(Array* _points); - Poly(Array* _points, Color _color); + Poly(); + Poly(std::vector & _points); + Poly(std::vector & _points, Color _color); ~Poly(); void setCenter(Vec c); Vec getInitRotation(Vec& v); @@ -25,5 +29,8 @@ Vec rotate_y(Vec& c); Vec rotate_z(Vec& c); void draw(); + int numPoints(); + void vardump(std::string whitespace); + void addPoint(Vec* p); }; #endif diff --git a/include/vec.h b/include/vec.h index 929c1fe..d59c4f9 100755 --- a/include/vec.h +++ b/include/vec.h @@ -1,9 +1,12 @@ #ifndef _VEC_H #define _VEC_H + +#include class Vec { public: float c[3]; + Vec(); Vec(float vx, float vy, float vz); Vec(Vec* v); float x(); @@ -22,5 +25,6 @@ float length(); void print(); Vec* clone(); + void vardump(std::string whitespace); }; #endif diff --git a/sources/main.cpp b/sources/main.cpp index 6054c89..c7a1bd8 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -63,10 +63,8 @@ #define PI 3.14159265f int debugct = 0; -int firstprint = 1; #define DD() do { printf("DD:%d\n", debugct); debugct++;} while(0); -#define DD1(x) while(firstprint) { printf("DD1: %d\n", x); firstprint = 0;}; /////////////////////////////////////////////////////////////////////////// // GLOBAL VARS @@ -125,9 +123,10 @@ string line; ifstream fp; - Array* points = new Array(); - Array* tpoints = new Array(); - Model* mm = new Model(new Array()); + std::vector points; + Model* mm = new Model(); + + Poly* tpoly; int tp = -1; @@ -144,25 +143,22 @@ if ( line[0] == 'v' ) { line.erase(0,1); - points->push(new Vec(getNextCoord(line), getNextCoord(line), getNextCoord(line))); + points.push_back(new Vec(getNextCoord(line), getNextCoord(line), getNextCoord(line))); } if ( line[0] == 'f' ) { line.erase(0,1); - tpoints->clear(); + tpoly = new Poly(); while ( (tp = getNextPoint(line)) > -1 ) { - tpoints->push(points->get(tp-1)); + tpoly->addPoint(points.at(tp-1)); } - mm->addFace(new Poly(tpoints)); + mm->addFace(tpoly); } } fp.close(); - delete points; - delete tpoints; - return mm; } @@ -173,278 +169,299 @@ /////////////////////////////////////////////////////////////////////////// // MAIN // -int main( int argc, char **argv ) -{ - // Initialize SDL: - if (SDL_Init(SDL_INIT_VIDEO) == -1) - { - printf("ERROR: unable to init SDL!\n"); - return 1; - } +int main( int argc, char **argv ) { - Color black (0,0,0); - Color blue (0,0,1); - Color red (1,0,0); - Color violet (1,0,1); - Color yellow (1,1,0); - Color white (1,1,1); - Color green (0,1,0); - Color cyan (0,1,1); + // Initialize SDL: + if (SDL_Init(SDL_INIT_VIDEO) == -1) { + printf("ERROR: unable to init SDL!\n"); + return 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 ); + Color black (0,0,0); + Color blue (0,0,1); + Color red (1,0,0); + Color violet (1,0,1); + Color yellow (1,1,0); + Color white (1,1,1); + Color green (0,1,0); + Color cyan (0,1,1); - 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* 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 cen (5,5,5); + 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 cen (5,5,5); - 4---7 - |pp5| - 4---0---3---7---4 - |pp2|pp0|pp3|pp1| - 5---1---2---6---5 - |pp4| - 5---6 + Model* cube = new Model(); - */ + /* + * + * 4---7 + * |pp5| + * 4---0---3---7---4 + * |pp2|pp0|pp3|pp1| + * 5---1---2---6---5 + * |pp4| + * 5---6 + */ - Array* pp0 = new Array(); - pp0->push(p0); - pp0->push(p3); - pp0->push(p2); - pp0->push(p1); + std::vector pp0; + std::vector pp1; + std::vector pp2; + std::vector pp3; + std::vector pp4; + std::vector pp5; - Array* pp1 = new Array(); - pp1->push(p4); - pp1->push(p5); - pp1->push(p6); - pp1->push(p7); + pp0.push_back(p0); + pp0.push_back(p3); + pp0.push_back(p2); + pp0.push_back(p1); - Array* pp2 = new Array(); - pp2->push(p4); - pp2->push(p0); - pp2->push(p1); - pp2->push(p5); + pp1.push_back(p4); + pp1.push_back(p5); + pp1.push_back(p6); + pp1.push_back(p7); - Array* pp3 = new Array(); - pp3->push(p2); - pp3->push(p3); - pp3->push(p7); - pp3->push(p6); + pp2.push_back(p4); + pp2.push_back(p0); + pp2.push_back(p1); + pp2.push_back(p5); - Array* pp4 = new Array(); - pp4->push(p1); - pp4->push(p2); - pp4->push(p6); - pp4->push(p5); + pp3.push_back(p2); + pp3.push_back(p3); + pp3.push_back(p7); + pp3.push_back(p6); - Array* pp5 = new Array(); - pp5->push(p3); - pp5->push(p0); - pp5->push(p4); - pp5->push(p7); - - Array* polys = new Array(); - polys->push(new Poly(pp0, red)); - polys->push(new Poly(pp1, green)); - polys->push(new Poly(pp2, cyan)); - polys->push(new Poly(pp3, blue)); - polys->push(new Poly(pp4, yellow)); - polys->push(new Poly(pp5, violet)); + pp4.push_back(p1); + pp4.push_back(p2); + pp4.push_back(p6); + pp4.push_back(p5); - //Model* myModel = importModel("obj/untitled.obj"); -//printf("imported obj/untitled.obj : %d faces\n", myModel->numberOfFaces()); + pp5.push_back(p3); + pp5.push_back(p0); + pp5.push_back(p4); + pp5.push_back(p7); - Poly* t; - polys->reset(); - while ( t = (Poly*)polys->next() ) { - //printf("polys %d setCenter\n", polys->pos()-1); - t->setCenter(cen); - } + 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, violet)); - //light info - Vec pos0(-10.0f, 12.0f, 15.0f); - Vec dir0(5.0f, -5.0f, 5.0f); + cube->setCenter(cen); - Light light0(&pos0, &dir0); + //char* modelfile = "obj/sample.obj"; + char* modelfile = "obj/untitled.obj"; + //char* modelfile = "obj/teapot.obj"; + Model* myModel = importModel(modelfile); + printf("imported %s : %d faces\n", modelfile, myModel->numFaces()); + //myModel->setCenter(cen); - // Prepare configuration: - int bitsPerColor = 8; - if (WINDOW_COLORDEPTH == 16) - bitsPerColor = 5; - SDL_GL_SetAttribute(SDL_GL_RED_SIZE, bitsPerColor); - SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, bitsPerColor); - SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, bitsPerColor); - SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0); - SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0); - SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0); - SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0); - if (WINDOW_COLORDEPTH == 32) - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); - else - SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, WINDOW_ZETADEPTH); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + //myModel->vardump(""); - // Create surface: - unsigned flags = SDL_OPENGL; - SDL_WM_SetCaption(WINDOW_TITLE, NULL); - if (SDL_SetVideoMode(WINDOW_X, WINDOW_Y, WINDOW_COLORDEPTH, flags) == NULL) - { - printf("ERROR: unsupported video configuration!\n"); - return 1; - } + //light info + Vec dir0(0.0f, 0.0f, 0.0f); + Vec pos0(5.0f, -5.0f, 5.0f); - // Ok, SDL up with a valid OpenGL context! - // Now setup some OpenGL parameter: - // Clear background with a darkblue color - glClearColor(0.0f, 0.0f, 0.5f, 1.0f); + Light light0(&pos0, &dir0); - // correct clipping - glEnable (GL_DEPTH_TEST); + // Prepare configuration: + int bitsPerColor = 8; + if (WINDOW_COLORDEPTH == 16) { + bitsPerColor = 5; + } - //enable light - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, bitsPerColor); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, bitsPerColor); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, bitsPerColor); + SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0); + SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0); + SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0); + SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0); - glEnable(GL_COLOR_MATERIAL); + if (WINDOW_COLORDEPTH == 32) { + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + } else { + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); + } - /////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// - // Mainloop: + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, WINDOW_ZETADEPTH); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - float deltay = 0.0f; - float deltaz = 0.0f; - float deltacolor = 0.0f; + // Create surface: + unsigned flags = SDL_OPENGL; + SDL_WM_SetCaption(WINDOW_TITLE, NULL); + + if (SDL_SetVideoMode(WINDOW_X, WINDOW_Y, WINDOW_COLORDEPTH, flags) == NULL ) { + printf("ERROR: unsupported video configuration!\n"); + return 1; + } + + // Ok, SDL up with a valid OpenGL context! + // Now setup some OpenGL parameter: + // Clear background with a darkblue color + glClearColor(0.0f, 0.0f, 0.5f, 1.0f); + + // correct clipping + glEnable (GL_DEPTH_TEST); + + //enable light + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + glEnable(GL_COLOR_MATERIAL); + + /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// + /////////////////////////////////////////////////////////////////////////// + // Mainloop: + + float deltay = 0.0f; + float deltaz = 0.0f; + float deltacolor = 0.0f; - while (!done) -{ - ///////////////////// - // Do some rendering: - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + while (!done) { + ///////////////////// + // Do some rendering: + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // Setup a perspective view: - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(45.0f, // Field of view - WINDOW_X/WINDOW_Y, // width/height ratio - 1.0f, // near clipping plane - 1000.0f); // far clipping plane + // Setup a perspective view: + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(45.0f, // Field of view + WINDOW_X/WINDOW_Y, // width/height ratio + 1.0f, // near clipping plane + 1000.0f); // far clipping plane - // Place the viewer 50 units backward: - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0.0f, 0.0f, -50.0f); + // Place the viewer 50 units backward: + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, -50.0f); - // Draw a rotating triangle: - glRotatef(deltay, 0.0f, 1.0f, 0.0f); // Rotation around the Y axis - glRotatef(deltaz, 1.0f, 0.0f, 0.0f); + // Draw a rotating triangle: + glRotatef(deltay, 0.0f, 1.0f, 0.0f); // Rotation around the Y axis + glRotatef(deltaz, 1.0f, 0.0f, 0.0f); - polys->reset(); - while ( t = (Poly*)polys->next() ) { - t->draw(); - t->rotate(Vec(0.0f, 0.01f, 0.01f)); - } + //cube->draw(); + //cube->rotate(Vec(0.0f, 0.01f, 0.01f)); + + + myModel->draw(); light0.draw(); light0.trace(); - //myModel->draw(); + //////////////// + // Check events: + SDL_Event event; - //////////////// - // Check events: - SDL_Event event; - while (SDL_PollEvent(&event)) - { - // Quit by click on X: - if (event.type == SDL_QUIT) - done = true; - } + while (SDL_PollEvent(&event)) { + // Quit by click on X: + if (event.type == SDL_QUIT) { + done = true; + } + } - // Update mouse: - OldMouseX = MouseX; - OldMouseY = MouseY; - unsigned buttons = SDL_GetMouseState(&MouseX, &MouseY); - if (buttons&SDL_BUTTON(1)) - MouseButtonLeft = true; - else - MouseButtonLeft = false; - if (buttons&SDL_BUTTON(3)) // <-- Beware: may be 2 on mouses - // without the middle button! - MouseButtonRight = true; - else - MouseButtonRight = false; + // Update mouse: + OldMouseX = MouseX; + OldMouseY = MouseY; + unsigned buttons = SDL_GetMouseState(&MouseX, &MouseY); - //if (MouseButtonLeft) - // delta += 1.0f; + if (buttons&SDL_BUTTON(1)) { + MouseButtonLeft = true; + } else { + MouseButtonLeft = false; + } - // Update keyboard (used like a joypad): - Uint8 *keystate = SDL_GetKeyState(NULL); - if (keystate[SDLK_ESCAPE]) - done = true; + if (buttons&SDL_BUTTON(3)) {// <-- Beware: may be 2 on mouses + // without the middle button! + MouseButtonRight = true; + } else { + MouseButtonRight = false; + } - //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); + //if (MouseButtonLeft) + // delta += 1.0f; - //scene rotation - if (keystate[SDLK_LEFT]) + // Update keyboard (used like a joypad): + Uint8 *keystate = SDL_GetKeyState(NULL); + if (keystate[SDLK_ESCAPE]) { + 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]) - deltay += 2.0f; - if (keystate[SDLK_UP]) - deltaz -= 2.0f; - if (keystate[SDLK_DOWN]) - deltaz += 2.0f; + } - //light rotation - /* if (keystate[SDLK_a]) - lightpos[0]+=lightdelta; - if (keystate[SDLK_d]) - lightpos[0]-=lightdelta; - if (keystate[SDLK_w]) - lightpos[1]+=lightdelta; - if (keystate[SDLK_s]) - lightpos[1]-=lightdelta; - if (keystate[SDLK_q]) - lightpos[2]+=lightdelta; - if (keystate[SDLK_e]) - lightpos[2]-=lightdelta; - */ - if (keystate[SDLK_SPACE]) { - deltacolor += 0.01f; + if (keystate[SDLK_RIGHT]) { + deltay += 2.0f; + } + + if (keystate[SDLK_UP]) { + deltaz -= 2.0f; + } + + if (keystate[SDLK_DOWN]) { + deltaz += 2.0f; + } + + //light rotation + /* if (keystate[SDLK_a]) + lightpos[0]+=lightdelta; + if (keystate[SDLK_d]) + lightpos[0]-=lightdelta; + if (keystate[SDLK_w]) + lightpos[1]+=lightdelta; + if (keystate[SDLK_s]) + lightpos[1]-=lightdelta; + if (keystate[SDLK_q]) + lightpos[2]+=lightdelta; + if (keystate[SDLK_e]) + lightpos[2]-=lightdelta; + */ + if (keystate[SDLK_SPACE]) { + deltacolor += 0.01f; if (deltacolor > 1.0f) { deltacolor = 0.0f; } - } + } - //////////////// - // Swap buffers: - usleep(2000); // Just don't kill computer resources ;) - SDL_GL_SwapBuffers(); + //////////////// + // Swap buffers: + usleep(2000); // Just don't kill computer resources ;) + SDL_GL_SwapBuffers(); - // Plain stupid log, just to show values: - //printf("X: %d, Y: %d, but1: %d, but2: %d\n", - // MouseX, MouseY, MouseButtonLeft, MouseButtonRight); - } + // Plain stupid log, just to show values: + //printf("X: %d, Y: %d, but1: %d, but2: %d\n", + // MouseX, MouseY, MouseButtonLeft, MouseButtonRight); + } - // Ok, release everything and byebye: - SDL_Quit(); - return 0; + // Ok, release everything and byebye: + SDL_Quit(); + return 0; } diff --git a/sources/model.cpp b/sources/model.cpp index 0deef4c..5345a35 100755 --- a/sources/model.cpp +++ b/sources/model.cpp @@ -1,7 +1,17 @@ #include "model.h" -Model::Model(Array* _faces) { - faces = new Array(*_faces); +#include + +Model::Model() { + rotation = new Vec(0,0,0); + position = new Vec(0,0,0); +} + +Model::Model(vector & _faces) { + for (std::vector::size_type i = 0 ; i < _faces.size(); i++ ) { + faces.push_back(_faces.at(i)); + } + Vec c = calcCenter(); setCenter(c); rotation = new Vec(0,0,0); @@ -10,45 +20,58 @@ Model::~Model() { delete position; delete rotation; - delete faces; } void Model::setCenter(Vec& c) { - delete position; position = c.clone(); + + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->setCenter(c); + } } Vec Model::calcCenter() { Vec s(0,0,0); - Poly* t; - faces->reset(); - while ( t = (Poly*)faces->next() ) { - s = s + t->calcCenter(); + + if ( faces.size() == 0 ) { + return s; } - return s/((float)faces->length()); + + for ( std::vector::size_type i = 0; i < faces.size(); i++ ) { + s = s + faces.at(i)->calcCenter(); + } + + return s / (float)faces.size(); } void Model::rotate(Vec rot) { rotation->add(rot); - faces->reset(); - Poly* t; - while ( t = (Poly*)faces->next() ) { - t->rotate(rot); + + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->rotate(rot); } } void Model::draw() { - faces->reset(); - Poly* t; - while ( t = (Poly*)faces->next() ) { - t->draw(); + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->draw(); } } void Model::addFace(Poly* face) { - faces->push(face); + faces.push_back(face); } -int Model::numberOfFaces() { - return faces->length(); +int Model::numFaces() { + return faces.size(); +} + +void Model::vardump(std::string whitespace) { + printf("%sModel(%d faces, center: [%f, %f, %f], rotation: [%f, %f, %f])\n", + whitespace.c_str(), faces.size(), position->x(), position->y(), position->z(), + rotation->x(), rotation->y(), rotation->z()); + + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->vardump(whitespace+" "); + } } diff --git a/sources/poly.cpp b/sources/poly.cpp index e835543..2c84328 100755 --- a/sources/poly.cpp +++ b/sources/poly.cpp @@ -5,21 +5,30 @@ #include #include "poly.h" +#include +#include -Poly::Poly(Array* _points) { - Poly(_points, Color(1,1,1)); +using namespace std; + +Poly::Poly() { + position = new Vec(0,0,0); + rotation = new Vec(0,0,0); + color = new Color(1,0,0); + setCenter(Vec(0,0,0)); } -Poly::Poly(Array* _points, Color _color) { - size = _points->length(); +Poly::Poly(std::vector & _points) { + Poly(_points, Color(1,0,0)); +} + +Poly::Poly(std::vector & _points, Color _color) { position = new Vec(0,0,0); rotation = new Vec(0,0,0); color = _color.clone(); -#ifdef DEBUG - printf("new Poly(%d)\n", size); -#endif - initpoints = new Array(_points); + for (std::vector::size_type i = 0 ; i < _points.size(); i++ ) { + initpoints.push_back(_points.at(i)); + } setCenter(calcCenter()); } @@ -28,7 +37,6 @@ delete position; delete rotation; delete color; - delete initpoints; } /* @@ -36,7 +44,6 @@ * the polygon is located */ void Poly::setCenter(Vec c) { - delete position; position = c.clone(); } @@ -52,12 +59,16 @@ */ Vec Poly::calcCenter() { Vec s (0,0,0); - Vec* t; - initpoints->reset(); - while ( t = (Vec*)initpoints->next() ) { - s = s+*t; + + if ( initpoints.size() == 0 ) { + return s; } - return s / ((float)size); + + for ( std::vector::size_type i = 0; i < initpoints.size(); i++ ) { + s = s + *(initpoints.at(i)); + } + + return s / (float)initpoints.size(); } /* @@ -66,12 +77,10 @@ */ void Poly::setPosition(Vec newcenter) { Vec diff = newcenter - (*position); - delete position; position = newcenter.clone(); - Vec* t; - initpoints->reset(); - while ( t = (Vec*)initpoints->next() ) { - t->add(diff); + + for ( std::vector::size_type i = 0; i < initpoints.size(); i++ ) { + initpoints.at(i)->add(diff); } } @@ -109,12 +118,11 @@ //glNormal3fv( v1.cross(v2)+ *(Vec*)((*initpoints)[0]).c ); //glNormal3fv( v1.cross(v2) + ((Vec*)initpoints->get(0))->c ); - Vec* t; Vec* mv; - initpoints->reset(); - while ( (t = (Vec*)initpoints->next()) ) { + + for ( std::vector::size_type i = 0; i < initpoints.size(); i++ ) { color->drawColor(); - Vec out = (*t - *position); + Vec out = *(initpoints.at(i)) - *position; out = rotate_x(out); out = rotate_y(out); out = rotate_z(out); @@ -123,3 +131,21 @@ } glEnd(); } + +int Poly::numPoints() { + return (int)initpoints.size(); +} + +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); + + for (std::vector::size_type i = 0 ; i < initpoints.size(); i++ ) { + initpoints.at(i)->vardump(whitespace+" "); + } +} + +void Poly::addPoint(Vec* p) { + initpoints.push_back(p); +} diff --git a/sources/vec.cpp b/sources/vec.cpp index 0acb620..de2c40f 100755 --- a/sources/vec.cpp +++ b/sources/vec.cpp @@ -2,6 +2,11 @@ #include #include +#include + +Vec::Vec() { + Vec(0.0f, 0.0f, 0.0f); +} Vec::Vec(float vx, float vy, float vz) { c[0] = vx; @@ -80,3 +85,7 @@ Vec* Vec::clone() { return new Vec(x(), y(), z()); } + +void Vec::vardump(std::string whitespace) { + printf("%sVec(%f, %f, %f)\n", whitespace.c_str(), c[0], c[1], c[2]); +}