diff --git a/include/light.h b/include/light.h index 4222647..025a5d7 100755 --- a/include/light.h +++ b/include/light.h @@ -3,50 +3,42 @@ #include "vec.h" #include "thing.h" +#include "material.h" #include "color.h" + #include +#include class Light : public Thing { protected: - void set(); + GLUquadricObj* sphere; bool enabled; + GLenum num; + Vec *pos; + Material *mat; + + Vec *rot; public: - Color* col; - GLenum num; Light(); - void flip(); - void enable(); - void disable(); -}; - -class DiffuseLight: public Light{ - public: - Vec* pos; - DiffuseLight(Vec& _pos, Color& _color, GLenum _lightnum); -}; - -class AmbientLight: public Light{ - public: - AmbientLight(Color& _color, GLenum _lightnum); -}; - -class SpecularLight: public Light{ - public: - Vec* pos; - SpecularLight(Vec& _pos, Color& _color, GLenum _lightnum); -}; - - -class SpotLight: public Light{ - public: - Vec* pos; - float delta; - GLUquadricObj* repr; - float angle; - SpotLight(Vec& _pos, Color& _color, GLenum _lightnum, float angle); + Light(GLenum _num, Vec _pos, Material _mat); + void init(GLenum _num, Vec _pos, Material _mat); + void setPosition(Vec newpos); + void rotate(Vec rot); + void show(); + void hide(); void draw(); }; +class SpotLight: public Light{ + private: + GLUquadricObj* cone; + float cutoff_angle; + + public: + void draw(); + SpotLight(GLenum _num, Vec _pos, Material _mat, float _angle); +}; + #endif diff --git a/include/material.h b/include/material.h index a559542..a06f6bb 100644 --- a/include/material.h +++ b/include/material.h @@ -5,11 +5,11 @@ #include class Material { - private: + + public: Color* ambient; //ambient and diffuse Color* specular; - public: Material(); Material(Color _ambient); Material(Color _ambient, Color _specular); diff --git a/sources/light.cpp b/sources/light.cpp index 1ac77d6..941231c 100755 --- a/sources/light.cpp +++ b/sources/light.cpp @@ -1,125 +1,97 @@ #include "light.h" -#include -#include - Light::Light() { //TODO: this class should be abstract - printf("light not initialized"); + printf("light not initialized\n"); } -void Light::enable() { +Light::Light(GLenum _num, Vec _pos, Material _mat) { + init(_num, _pos, _mat); +} + +void Light::init(GLenum _num, Vec _pos, Material _mat) { + num = _num; + pos = _pos.clone(); + mat = _mat.clone(); + rot = new Vec(0,0,0); + sphere = gluNewQuadric(); + + //glLightfv(num, GL_AMBIENT, mat->ambient->v()); + glLightfv(num, GL_DIFFUSE, mat->ambient->v()); + glLightfv(num, GL_SPECULAR, mat->specular->v()); + //TODO + //glLightf(num,GL_QUADRATIC_ATTENUATION,.001f); + + show(); +} + +void Light::show() { enabled = true; glEnable(num); } -void Light::disable() { +void Light::hide() { enabled = false; glDisable(num); } -void Light::flip() { - if (enabled) - disable(); - else - enable(); +void Light::setPosition(Vec newpos) { + delete pos; + pos = newpos.clone(); +} + +void Light::rotate(Vec a) { + rot->add((Vec)a); } -DiffuseLight::DiffuseLight(Vec& _pos, Color& _color, GLenum _lightnum) { - pos = _pos.clone(); - col = _color.clone(); - num = _lightnum; +void Light::draw() { + //save camera position + glPushMatrix(); - glLightfv(num, GL_DIFFUSE, col->v()); - GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; - glLightfv(num, GL_POSITION, LightPosition); - enable(); + glTranslatef(pos->x(), pos->y(), pos->z()); + + float p[] = {0.0f, 0.0f, 0.0f, 1.0f}; + glLightfv(num, GL_POSITION, p); + + //sphere + float c[] = {1,1,1,1}; + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c); + gluSphere(sphere, 1, 10, 10); + + glPopMatrix(); } +SpotLight::SpotLight(GLenum _num, Vec _pos, Material _mat, float _angle) { + Light::init(_num, _pos, _mat); + cutoff_angle = _angle; + cone = gluNewQuadric(); -SpecularLight::SpecularLight(Vec& _pos, Color& _color, GLenum _lightnum) { - pos = _pos.clone(); - col = _color.clone(); - num = _lightnum; - - glLightfv(num, GL_SPECULAR, col->v()); - GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; - glLightfv(num, GL_POSITION,LightPosition); - enable(); -} - - - -SpotLight::SpotLight(Vec& _pos, Color& _color, GLenum _lightnum, float _angle) { - pos = _pos.clone(); - col = _color.clone(); - num = _lightnum; - angle = _angle; - - glLightfv(num, GL_DIFFUSE, col->v()); - //glLightfv(num, GL_AMBIENT, col->v()); - //glTranslate(pos->x(), pos->y(), pos->z(), 1); - //float p[] = {3,3,3, 1.0f}; - //glLightfv(num, GL_POSITION,p); - - //GLfloat dir[] = {2,-1,-2}; - - - - //glLightf(num,GL_QUADRATIC_ATTENUATION,.00001f); - repr = gluNewQuadric(); - - enable(); - delta=0; + show(); } void SpotLight::draw() { //save camera position glPushMatrix(); - delta += 1; - float p[] = {0.0f, 0.0f, 0.0f, 1.0f}; - GLfloat dir[] = {0.0f, -1.0f, 0.0f, 1.0f}; + glRotatef(rot->x(), 1.0f, 0.0f, 0.0f); + glRotatef(rot->y(), 0.0f, 1.0f, 0.0f); + glRotatef(rot->z(), 0.0f, 0.0f, 1.0f); glTranslatef(pos->x(), pos->y(), pos->z()); + glLightf(num, GL_SPOT_CUTOFF, cutoff_angle); - glLightf(num, GL_SPOT_CUTOFF, angle); + float p[] = {0.0f, 0.0f, 0.0f, 1.0f}; glLightfv(num, GL_POSITION, p); + + GLfloat dir[] = {0.0f, -1.0f, 0.0f, 1.0f}; glLightfv(num, GL_SPOT_DIRECTION, dir); - - //sphere - glPushMatrix(); - glTranslatef(0,-120,0); - gluSphere(gluNewQuadric(), 100, 300, 300); - glPopMatrix(); - - //line - glBegin(GL_LINES); - float mat[] = {0,1,1}; - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat); - glVertex3f(p[0], p[1], p[2]); - glVertex3fv(dir); - glEnd(); - //cone - glPushMatrix(); glTranslatef(0,5.5,0); glRotatef(90, 1, 0, 0); - gluCylinder (repr, 0, 1, 5, 10, 20); + gluCylinder (cone, 0, 1, 5, 10, 20); + glPopMatrix(); - //gluCylinder (repr, 0, 20, 20, 10, 20); - - glPopMatrix(); -} - - - -AmbientLight::AmbientLight(Color& _color, GLenum _lightnum) { - col = _color.clone(); - num = _lightnum; - glLightfv(num, GL_AMBIENT, col->v()); - enable(); } diff --git a/sources/main.cpp b/sources/main.cpp index a0e32a0..e75793d 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -305,9 +305,7 @@ //cube->setCenter(cubeCen); userAnim->addThing((Thing*)cube); - //cube->setDelta(PI/2.0); - - //cube->showNormals(); + cube->setDelta(PI/2.0); /* end cube */ @@ -322,8 +320,8 @@ printf("imported %s : %d faces\n", modelfile, myModel->numFaces()); Vec myCen = Vec(5,-10,5); - //myModel->setCenter(myCen); - //myModel->setMaterial(Color(1,0,1), Color(1,0,1)); + myModel->setCenter(myCen); + myModel->setMaterial(Color(1,0,1), Color(1,0,1)); myModel->setPosition(myCen); myModel->showNormals(); myModel->setDelta(PI/2.0); @@ -380,16 +378,24 @@ glEnable(GL_LIGHTING); glFrontFace(GL_CCW); - GLfloat global_ambient[] = { 0.3f, 0.3f, 0.3f, 1.0f }; + GLfloat global_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f }; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); //light info //Color l0_col(0.1f, 0.1f, 0.1f); //AmbientLight light0(l0_col, GL_LIGHT0); - Color l0_col(1, 1, 1); - Vec l0_pos(3, 10, 3); - SpotLight light0(l0_pos, l0_col, GL_LIGHT0, 30); + + SpotLight light0(GL_LIGHT0, + Vec (3, 50, 3), + Material(Color(0.5,0.5,0.5), //Color(0.5,0.5,0.5), + Color(0,0,0)), + 90); + + Light light1(GL_LIGHT1, + Vec (20, 20, 20), + Material(Color(0.5,0.5,0.5), + Color(0,0,0))); //Vec l1_pos(-10.0f, -10.0f, -10.0f); //Color l1_col(0.0f, 0.5f, 1.0f); @@ -454,16 +460,19 @@ WINDOW_X/WINDOW_Y, // width/height ratio 1.0f, // near clipping plane 1000.0f); // far clipping plane + // move viewer myView->draw(); glMatrixMode(GL_MODELVIEW); //initial viewer position glLoadIdentity(); - // move viewer + //draw lights light0.draw(); + light1.draw(); - + //TODO JAGGI t�e diz bitte ind animation + light0.rotate(Vec(1,0,0)); //glTranslatef(0,0,-50); @@ -474,8 +483,8 @@ /* draw some models */ for ( modelCounter = 0; modelCounter < models.size(); modelCounter++ ) { - //models.at(modelCounter)->draw(); - //models.at(modelCounter)->rotate(Vec(1.5f, 0.0f, 0.0f)); + models.at(modelCounter)->draw(); + models.at(modelCounter)->rotate(Vec(1.5f, 0.0f, 0.0f)); } //xy plane @@ -500,8 +509,17 @@ glVertex3f(0,0,30); glEnd(); - //cube->draw(); - //myModel->draw(); + //sphere + glPushMatrix(); + float mat[] = {0.5,0.5,0.5}; + glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat); + glMaterialfv(GL_FRONT, GL_SPECULAR, mat); + glTranslatef(0,-60,0); + gluSphere(gluNewQuadric(), 50, 300, 300); + glPopMatrix(); + + cube->draw(); + myModel->draw(); //draw cube //cube->draw(); @@ -531,21 +549,12 @@ if (react_keyevent == 0) { react_keyevent = 10; - // lights - if (keystate[SDLK_1]) { - light0.flip(); - } - else if (keystate[SDLK_2]) { - //light1.flip(); + if ( keystate[SDLK_g] ) { + wireframe = !wireframe; } else { react_keyevent = 0; - } - - if ( keystate[SDLK_g] ) { - wireframe = !wireframe; - } - + } } else { react_keyevent -= 1; }