diff --git a/include/color.h b/include/color.h index 3a85bca..62e0ba8 100755 --- a/include/color.h +++ b/include/color.h @@ -1,5 +1,6 @@ #ifndef _COLOR_H #define _COLOR_H + class Color { public: float r; diff --git a/include/light.h b/include/light.h index 6d076be..3652d49 100755 --- a/include/light.h +++ b/include/light.h @@ -2,13 +2,33 @@ #define _LIGHT_H #include "vec.h" +#include "color.h" +#include class Light { - public: - Vec* pos; - Vec* dir; - Light(Vec* _pos, Vec* _dir); - void trace(); - void draw(); + protected: + void set(); + bool enabled; + public: + Vec* pos; + Vec* dir; + Color* col; + GLenum lightnum; + + Light(Vec* _pos, Vec* _dir, GLenum _gl_light); + Light(); + void flip(); + void trace(); + void draw(); }; + + +class DiffuseLight: public Light{ + public: + DiffuseLight(Vec* _pos, Color* _color, GLenum _gl_light); + void trace(); + void draw(); +}; + + #endif diff --git a/sources/light.cpp b/sources/light.cpp index 9a99614..6a5a529 100755 --- a/sources/light.cpp +++ b/sources/light.cpp @@ -3,9 +3,18 @@ #include #include -Light::Light(Vec* _pos, Vec* _dir) { - pos = _pos; - dir = _dir; +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"); } void Light::trace() { @@ -16,10 +25,47 @@ } void Light::draw() { - //printf(GL_LIGHT0); - glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f); float p [4] = {pos->x(), pos->y(), pos->z(), 1.0f}; float d [4] = {dir->x(), dir->y(), dir->z(), 1.0f}; - glLightfv(GL_LIGHT0, GL_POSITION, p); - glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, d); + 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::flip() { + if (enabled) + enabled = false; + else + enabled = true; + set(); +} + +DiffuseLight::DiffuseLight(Vec* _pos, Color* _color, GLenum _lightnum) { + pos = _pos; + col = _color; + lightnum = _lightnum; + set(); +} + +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(); } diff --git a/sources/main.cpp b/sources/main.cpp index 0748e85..a7fa1f3 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -178,6 +178,8 @@ // MAIN // int main( int argc, char **argv ) { + char a = '|'; + printf("%d", a); // Initialize SDL: if (SDL_Init(SDL_INIT_VIDEO) == -1) { @@ -261,7 +263,7 @@ cube->addFace(new Poly(pp2, cyan)); cube->addFace(new Poly(pp3, blue)); cube->addFace(new Poly(pp4, yellow)); - cube->addFace(new Poly(pp5, violet)); + cube->addFace(new Poly(pp5, white)); cube->setCenter(cen); @@ -274,12 +276,6 @@ myModel->vardump(""); - //light info - Vec dir0(0.0f, 0.0f, 0.0f); - Vec pos0(5.0f, -5.0f, 5.0f); - - Light light0(&pos0, &dir0); - // Prepare configuration: int bitsPerColor = 8; if (WINDOW_COLORDEPTH == 16) { @@ -322,10 +318,22 @@ //enable light glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); + + //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_dir(0.0f, 0.0f, 0.0f); + Vec l1_pos(15.0f, 15.0f, 15.0f); + + //DiffuseLight light0(&l0_pos, &l0_col, GL_LIGHT0); + Light light0(&l1_pos, &l1_dir, GL_LIGHT1); glEnable(GL_COLOR_MATERIAL); + + /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// @@ -363,14 +371,16 @@ glRotatef(deltay, 0.0f, 1.0f, 0.0f); // Rotation around the Y axis glRotatef(deltaz, 1.0f, 0.0f, 0.0f); - //cube->draw(); - //cube->rotate(Vec(0.0f, 0.01f, 0.01f)); + cube->draw(); + cube->rotate(Vec(0.0f, 0.01f, 0.01f)); - myModel->draw(); + //myModel->draw(); light0.draw(); light0.trace(); + //light1.draw(); + //light1.trace(); //////////////// // Check events: @@ -410,8 +420,12 @@ done = true; } + // lights + if (keystate[SDLK_1]) light0.flip(); + //if (keystate[SDLK_2]) light1.flip(); + //background color - if (keystate[SDLK_1]) { + /* if (keystate[SDLK_1]) { glClearColor(0.5f, 0.0f, 0.0f, 1.0f); } @@ -421,7 +435,7 @@ if (keystate[SDLK_3]) { glClearColor(0.0f, 0.0f, 0.5f, 1.0f); - } + }*/ //scene rotation if (keystate[SDLK_LEFT]) deltay -= 2.0f; @@ -434,7 +448,7 @@ if (keystate[SDLK_s]) viewer_zpos += 0.1f; if (keystate[SDLK_a]) viewer_xpos -= 0.1f; if (keystate[SDLK_d]) viewer_xpos += 0.1f; - + if (keystate[SDLK_SPACE]) { deltacolor += 0.01f; if (deltacolor > 1.0f) { @@ -442,6 +456,7 @@ } } + //////////////// // Swap buffers: mysleep(2000); // Just don't kill computer resources ;)