diff --git a/include/light.h b/include/light.h index 3652d49..3e4ccfb 100755 --- a/include/light.h +++ b/include/light.h @@ -6,29 +6,29 @@ #include class Light { - protected: + protected: void set(); bool enabled; + public: - Vec* pos; - Vec* dir; Color* col; - GLenum lightnum; - - Light(Vec* _pos, Vec* _dir, GLenum _gl_light); + GLenum num; Light(); void flip(); - void trace(); - void draw(); + void enable(); + void disable(); }; class DiffuseLight: public Light{ public: - DiffuseLight(Vec* _pos, Color* _color, GLenum _gl_light); - void trace(); - void draw(); + Vec* pos; + DiffuseLight(Vec& _pos, Color& _color, GLenum _lightnum); }; +class AmbientLight: public Light{ + public: + AmbientLight(Color& _color, GLenum _lightnum); +}; #endif diff --git a/include/model.h b/include/model.h index b430152..0903d45 100755 --- a/include/model.h +++ b/include/model.h @@ -12,6 +12,8 @@ Vec* position; Vec* rotation; + float delta; + public: Model(); Model(std::vector & _faces); @@ -23,5 +25,7 @@ void addFace(Poly* face); int numFaces(); void vardump(std::string whitespace); + void setDelta(float d); + float getDelta(); }; #endif diff --git a/include/poly.h b/include/poly.h index 59802b1..2dbe5de 100755 --- a/include/poly.h +++ b/include/poly.h @@ -17,6 +17,7 @@ Vec* rotation; Vec* normal; Color* color; + float delta; public: Poly(); Poly(std::vector & _points); @@ -37,5 +38,6 @@ void addPoint(Vec* p); void setColor(Color c); Color* getColor(); + void setDelta(float d); }; #endif diff --git a/include/vec.h b/include/vec.h index 04e768a..2dadadc 100755 --- a/include/vec.h +++ b/include/vec.h @@ -7,8 +7,8 @@ #include class Vec { private: - std::vector faces; public: + std::vector faces; float c[3]; Vec(); @@ -26,6 +26,8 @@ Vec operator*(float s); Vec operator*(Vec& s); Vec operator/(float s); + float angle(Vec& v); + float dotP(Vec& v); Vec cross(Vec& v); Vec normalize(); float length(); diff --git a/sources/light.cpp b/sources/light.cpp index 6a5a529..2cdb4cc 100755 --- a/sources/light.cpp +++ b/sources/light.cpp @@ -3,69 +3,48 @@ #include #include -Light::Light(Vec* _pos, Vec* _dir, GLenum _lightnum) { - pos = _pos; - dir = _dir; - lightnum = _lightnum; - enabled=true; - set(); -} - Light::Light() { - Vec _dir = new Vec(0.0f, 0.0f, 0.0f); - Vec _pos = new Vec(20.0f, 20.0f, 20.0f); - printf("fuckoff"); + //TODO: this class should be abstract + printf("light not initialized"); } -void Light::trace() { - glBegin (GL_LINES); - glVertex3fv(pos->getCoords()); - glVertex3fv(dir->getCoords()); - glEnd(); +void Light::enable() { + enabled = true; + glEnable(num); } -void Light::draw() { - float p [4] = {pos->x(), pos->y(), pos->z(), 1.0f}; - float d [4] = {dir->x(), dir->y(), dir->z(), 1.0f}; - glLightf(lightnum, GL_SPOT_CUTOFF, 180.0f); - glLightfv(lightnum, GL_POSITION, p); - glLightfv(lightnum, GL_SPOT_DIRECTION, d); -} - -void Light::set() { - if (enabled) { - glEnable(lightnum); - printf("%d on\n", lightnum); - } else { - glDisable(lightnum); - printf("%d off\n", lightnum); - } +void Light::disable() { + enabled = false; + glDisable(num); } void Light::flip() { if (enabled) - enabled = false; + disable(); else - enabled = true; - set(); + enable(); } -DiffuseLight::DiffuseLight(Vec* _pos, Color* _color, GLenum _lightnum) { - pos = _pos; - col = _color; - lightnum = _lightnum; - set(); + + +DiffuseLight::DiffuseLight(Vec& _pos, Color& _color, GLenum _lightnum) { + pos = _pos.clone(); + col = _color.clone(); + num = _lightnum; + + GLfloat LightDiffuse[]= { col->r, col->g, col->b, 1.0f }; + glLightfv(num, GL_DIFFUSE, LightDiffuse); + GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; + glLightfv(num, GL_POSITION,LightPosition); + enable(); } -void DiffuseLight::draw() { - float p [4] = {pos->x(), pos->y(), pos->z(), 1.0f}; - float c [4] = {col->r, col->g, col->b, 1.0f}; - glLightfv(lightnum, GL_DIFFUSE, c); - glLightfv(lightnum, GL_POSITION, p); -} -void DiffuseLight::trace() { - glBegin(GL_POINTS); - glVertex3fv(pos->getCoords()); - glEnd(); + +AmbientLight::AmbientLight(Color& _color, GLenum _lightnum) { + col = _color.clone(); + num = _lightnum; + GLfloat LightAmbient[]= { 0.1f, 0.1f, 0.1f, 1.0f }; + glLightfv(num, GL_AMBIENT, LightAmbient); + enable(); } diff --git a/sources/main.cpp b/sources/main.cpp index c18caf1..60555f8 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -201,6 +201,7 @@ // viewport rotation delta Vec viewer_zrot = Vec (0.0f, 0.0f, 2.0f); + Vec viewer_yrot = Vec (0.0f, 2.0f, 0.0f); Vec viewer_xrot = Vec (2.0f, 0.0f, 0.0f); Color black (0,0,0); @@ -283,15 +284,18 @@ cube->setCenter(cen); + cube->setDelta(PI); + //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); + Vec myCen = Vec(0,0,0); + myModel->setCenter(myCen); - myModel->vardump(""); + //myModel->vardump(""); // Prepare configuration: int bitsPerColor = 8; @@ -334,22 +338,20 @@ glEnable (GL_DEPTH_TEST); //enable light - //glEnable(GL_LIGHTING); + glEnable(GL_LIGHTING); //light info - Vec l0_dir(0.0f, 0.0f, 0.0f); - Vec l0_pos(10.0f, -10.0f, 10.0f); - Color l0_col(1.0f, 0.0f, 0.0f); + Vec l1_pos(0.0f, -10.0f, -10.0f); + Color l1_col(1.0f, 1.0f, 1.0f); - Vec l1_dir(0.0f, 0.0f, 0.0f); - Vec l1_pos(15.0f, 15.0f, 15.0f); + Color l0_col(0.5f, 0.5f, 0.5f); - //DiffuseLight light0(&l0_pos, &l0_col, GL_LIGHT0); - Light light0(&l1_pos, &l1_dir, GL_LIGHT1); + AmbientLight light0(l0_col, GL_LIGHT0); + DiffuseLight light1(l1_pos, l1_col, GL_LIGHT1); glEnable(GL_COLOR_MATERIAL); - + glShadeModel(GL_SMOOTH); /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// @@ -358,7 +360,7 @@ // Mainloop: float deltacolor = 0.0f; - + int react_keyevent = 0; while (!done) { @@ -380,29 +382,23 @@ //initial viewer position glLoadIdentity(); - - //rotate viewer - glRotatef(viewer_rot.z(), 0.0f, 0.0f, 1.0f); - glRotatef(viewer_rot.x(), 1.0f, 0.0f, 0.0f); - + //move viewer glTranslatef(viewer_pos.x(), viewer_pos.y(), viewer_pos.z()); + //rotate viewer + glRotatef(viewer_rot.z(), 0.0f, 0.0f, 1.0f); + glRotatef(viewer_rot.y(), 0.0f, 1.0f, 0.0f); + glRotatef(viewer_rot.x(), 1.0f, 0.0f, 0.0f); + //draw cube cube->draw(); - cube->rotate(Vec(1.0f, 1.0f, 1.0f)); - + cube->rotate(Vec(0.1f, 0.5f, 1.0f)); //myModel->draw(); + //myModel->rotate(Vec(0.2f, 0.3f, 1.2f)); - //light0.draw(); - //light0.trace(); - //light1.draw(); - //light1.trace(); - - // camera position - //////////////// // Check events: SDL_Event event; @@ -442,8 +438,22 @@ } // lights - if (keystate[SDLK_1]) light0.flip(); - //if (keystate[SDLK_2]) light1.flip(); + if (react_keyevent == 0) { + react_keyevent = 10; + + if (keystate[SDLK_1]) { + light0.flip(); + } + else if (keystate[SDLK_2]) { + light1.flip(); + } + else { + react_keyevent = 0; + } + + } else { + react_keyevent -= 1; + } //background color /* if (keystate[SDLK_1]) { @@ -459,8 +469,8 @@ }*/ // rotate viewer - if (keystate[SDLK_LEFT]) viewer_rot.add(viewer_zrot); - if (keystate[SDLK_RIGHT]) viewer_rot.sub(viewer_zrot); + if (keystate[SDLK_LEFT]) viewer_rot.add(viewer_yrot); + if (keystate[SDLK_RIGHT]) viewer_rot.sub(viewer_yrot); if (keystate[SDLK_UP]) viewer_rot.add(viewer_xrot); if (keystate[SDLK_DOWN]) viewer_rot.sub(viewer_xrot); diff --git a/sources/model.cpp b/sources/model.cpp index 799a335..89448eb 100755 --- a/sources/model.cpp +++ b/sources/model.cpp @@ -75,3 +75,15 @@ faces.at(i)->vardump(whitespace+" "); } } + +void Model::setDelta(float d) { + delta = d; + + for (std::vector::size_type i = 0 ; i < faces.size(); i++ ) { + faces.at(i)->setDelta(d); + } +} + +float Model::getDelta() { + return delta; +} diff --git a/sources/poly.cpp b/sources/poly.cpp index 18da677..ab5367a 100755 --- a/sources/poly.cpp +++ b/sources/poly.cpp @@ -11,6 +11,7 @@ using namespace std; Poly::Poly() { + delta = 0.0f; position = new Vec(0,0,0); rotation = new Vec(0,0,0); color = new Color(1,0,0); @@ -23,6 +24,7 @@ } Poly::Poly(std::vector & _points, Color _color) { + delta = 0.0f; position = new Vec(0,0,0); rotation = new Vec(0,0,0); color = _color.clone(); @@ -129,7 +131,9 @@ } void Poly::draw() { - unsigned int vcount = initpoints.size(); + Vec n = Vec(0.0f, 0.0f, 0.0f); + + std::vector::size_type vcount = initpoints.size(); //save camera position glPushMatrix(); @@ -145,20 +149,27 @@ glBegin (GL_POLYGON); for ( std::vector::size_type i = 0; i < vcount; i++ ) { - /* old version - 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; - */ + /* face normal */ + //glNormal3fv(normal->c); - glNormal3fv(normal->c); - color->drawColor(); - //glVertex3fv((*position + cur).c); - glVertex3fv((*initpoints.at(i) - *position).c); + /* TODO: vertex normal */ + 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); + } + } + + glNormal3fv(n.c); + + color->drawColor(); + glVertex3fv((*initpoints.at(i) - *position).c); } glEnd(); @@ -196,3 +207,7 @@ Color* Poly::getColor() { return color; } + +void Poly::setDelta(float d) { + delta = d; +} diff --git a/sources/vec.cpp b/sources/vec.cpp index b54865c..182f9c1 100755 --- a/sources/vec.cpp +++ b/sources/vec.cpp @@ -16,6 +16,7 @@ Vec::Vec(Vec* v) { Vec(v->x(), v->y(), v->z()); + /* TODO: copy registered faces ?? */ } float Vec::x() { @@ -72,6 +73,14 @@ return Vec(x()/s, y()/s, z()/s); } +float Vec::angle(Vec& v) { + return acos((*this).dotP(v)/((*this).length()*v.length())); +} + +float Vec::dotP(Vec &v) { + return x()*v.x() + y()*v.y() + z()*v.z(); +} + 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()); }