diff --git a/include/light.h b/include/light.h index ee6605c..4222647 100755 --- a/include/light.h +++ b/include/light.h @@ -38,4 +38,15 @@ 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); + void draw(); +}; + #endif diff --git a/sources/color.cpp b/sources/color.cpp index 007c099..ab878a9 100755 --- a/sources/color.cpp +++ b/sources/color.cpp @@ -5,9 +5,6 @@ c[1] = g; c[2] = b; c[3] = 1.0f; - //r = _r; - //g = _g; - //b = _b; } void Color::drawColor() { diff --git a/sources/light.cpp b/sources/light.cpp index eaf9e7b..1ac77d6 100755 --- a/sources/light.cpp +++ b/sources/light.cpp @@ -31,9 +31,9 @@ col = _color.clone(); num = _lightnum; - glLightfv(num, GL_DIFFUSE, col->v()); + glLightfv(num, GL_DIFFUSE, col->v()); GLfloat LightPosition[]= { pos->x(), pos->y(), pos->z(), 1.0f }; - glLightfv(num, GL_POSITION,LightPosition); + glLightfv(num, GL_POSITION, LightPosition); enable(); } @@ -52,6 +52,71 @@ +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; +} + +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}; + + glTranslatef(pos->x(), pos->y(), pos->z()); + + glLightf(num, GL_SPOT_CUTOFF, angle); + glLightfv(num, GL_POSITION, p); + 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); + glPopMatrix(); + //gluCylinder (repr, 0, 20, 20, 10, 20); + + glPopMatrix(); +} + + + AmbientLight::AmbientLight(Color& _color, GLenum _lightnum) { col = _color.clone(); num = _lightnum; diff --git a/sources/main.cpp b/sources/main.cpp index dcb28fd..a0e32a0 100755 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -201,25 +201,9 @@ return 1; } - // current viewport position - Vec viewer_pos = Vec(0.0f, 0.0f, -50.0f); - // viewport movement delta - Vec viewer_zmove_pos = Vec(0.0f, 0.0f, -0.3f); - Vec viewer_zmove_neg = Vec(0.0f, 0.0f, 0.5f); - Vec viewer_xmove_pos = Vec(0.3f, 0.0f, 0.0f); - Vec viewer_xmove_neg = Vec(-0.3f, 0.0f, 0.0f); - - // current viewport rotation - Vec viewer_rot = Vec(0.0f, 0.0f, 0.0f); - - // 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); Color blue (0,0,1); + Color black (0,0,0); Color red (1,0,0); Color violet (1,0,1); Color yellow (1,1,0); @@ -228,7 +212,7 @@ Color cyan (0,1,1); Color grey (0.5f,0.5f,0.5f); - Vec initPos = Vec(30.0f, -30.0f, -30.0f); + Vec initPos = Vec(0.0f, 0.0f, 0.0f); Vec initRot = Vec(0.0f, 0.0f, 0.0f); View* myView = new View(initPos, initRot); UserAnimation* userAnim = new UserAnimation(); @@ -395,19 +379,21 @@ //enable light glEnable(GL_LIGHTING); - GLfloat global_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f }; + glFrontFace(GL_CCW); + GLfloat global_ambient[] = { 0.3f, 0.3f, 0.3f, 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(0.3f, 0.3f, 0.3f); - Vec l0_pos(0.0f, -10.0f, -10.0f); - SpecularLight light0(l0_pos, l0_col, GL_LIGHT0); - Vec l1_pos(-10.0f, -10.0f, -10.0f); - Color l1_col(0.0f, 0.5f, 1.0f); - DiffuseLight light1(l1_pos, l1_col, GL_LIGHT1); + Color l0_col(1, 1, 1); + Vec l0_pos(3, 10, 3); + SpotLight light0(l0_pos, l0_col, GL_LIGHT0, 30); + + //Vec l1_pos(-10.0f, -10.0f, -10.0f); + //Color l1_col(0.0f, 0.5f, 1.0f); + //DiffuseLight light1(l1_pos, l1_col, GL_LIGHT1); //glEnable(GL_COLOR_MATERIAL); @@ -445,10 +431,11 @@ t->setColor(tc); models.push_back(t); } - - cube->showNormals(); + cube->showNormals(); + float delta = 0; while (!done) { + delta += 1; ///////////////////// // Do some rendering: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -459,6 +446,7 @@ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } + // Setup a perspective view: glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -466,30 +454,54 @@ WINDOW_X/WINDOW_Y, // width/height ratio 1.0f, // near clipping plane 1000.0f); // far clipping plane - - + myView->draw(); glMatrixMode(GL_MODELVIEW); - //initial viewer position glLoadIdentity(); // move viewer - myView->draw(); + light0.draw(); + + + + //glTranslatef(0,0,-50); /* do animations */ for ( std::vector::size_type anim_ct = 0; anim_ct < anims.size(); anim_ct++ ) { - ((UserAnimation*)anims.at(anim_ct))->tick(); + ((UserAnimation*)anims.at(anim_ct))->tick(); } /* 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)); } - cube->draw(); - myModel->draw(); + //xy plane + glBegin(GL_TRIANGLES); + glNormal3f(0,0,1); + glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); + glVertex3f(0,0,0); + //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); + glVertex3f(10,0,0); + //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red.v()); + glVertex3f(0,10,0); + glEnd(); + + //xz plane + glBegin(GL_TRIANGLES); + glNormal3f(0,1,0); + glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); + glVertex3f(0,0,0); + //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue.v()); + glVertex3f(30,0,0); + //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green.v()); + glVertex3f(0,0,30); + glEnd(); + + //cube->draw(); + //myModel->draw(); //draw cube //cube->draw(); @@ -524,7 +536,7 @@ light0.flip(); } else if (keystate[SDLK_2]) { - light1.flip(); + //light1.flip(); } else { react_keyevent = 0; @@ -555,18 +567,6 @@ viewerMovingSpeed = 1.0f; } - // rotate viewer - if (keystate[SDLK_LEFT]) viewer_rot.add(viewer_yrot*viewerMovingSpeed); - if (keystate[SDLK_RIGHT]) viewer_rot.sub(viewer_yrot*viewerMovingSpeed); - if (keystate[SDLK_UP]) viewer_rot.add(viewer_xrot*viewerMovingSpeed); - if (keystate[SDLK_DOWN]) viewer_rot.sub(viewer_xrot*viewerMovingSpeed); - - // move viewer - if (keystate[SDLK_w]) viewer_pos.add(viewer_zmove_neg*viewerMovingSpeed); - if (keystate[SDLK_s]) viewer_pos.add(viewer_zmove_pos*viewerMovingSpeed); - if (keystate[SDLK_a]) viewer_pos.add(viewer_xmove_pos*viewerMovingSpeed); - if (keystate[SDLK_d]) viewer_pos.add(viewer_xmove_neg*viewerMovingSpeed); - if (keystate[SDLK_SPACE]) { deltacolor += 0.01f; if (deltacolor > 1.0f) { diff --git a/sources/material.cpp b/sources/material.cpp index f089e6d..37653a1 100644 --- a/sources/material.cpp +++ b/sources/material.cpp @@ -6,19 +6,16 @@ Material::Material() { ambient = new Color(0,0,0); specular = new Color(0.1,0.1,0.1); - ptdump("m0"); } Material::Material(Color _ambient) { ambient = _ambient.clone(); specular = new Color(0,0,0); - ptdump("m1"); } Material::Material(Color _ambient, Color _specular) { ambient = _ambient.clone(); specular = _specular.clone(); - ptdump("m2"); } void Material::drawMaterial() { diff --git a/sources/poly.cpp b/sources/poly.cpp index 0fdf310..8503834 100755 --- a/sources/poly.cpp +++ b/sources/poly.cpp @@ -222,7 +222,7 @@ if ( delta <= 0.0f ) { /* face normal */ - glNormal3fv(normal->c); + glNormal3fv((*normal).c); } else { /* vertex normal (delta) */ n.c[0] = 0.0f; diff --git a/sources/thing.cpp b/sources/thing.cpp index ebc0aed..13de17c 100644 --- a/sources/thing.cpp +++ b/sources/thing.cpp @@ -50,12 +50,12 @@ bgcolor = c; } void View::draw() { - //glClearColor(bgcolor->r, bgcolor->g, bgcolor->b, 1.0f); + //glClearColor(bgcolor->r(), bgcolor->g(), bgcolor->b(), 1.0f); - glRotatef(rotation->x(), 1.0f, 0.0f, 0.0f); - glRotatef(rotation->y(), 0.0f, 1.0f, 0.0f); - glRotatef(rotation->z(), 0.0f, 0.0f, 1.0f); + glTranslatef(position->x(), position->y(), position->z()); - glTranslatef(position->x(), position->y(), position->z()); + glRotatef(rotation->x(), 1.0f, 0.0f, 0.0f); + glRotatef(rotation->y(), 0.0f, 1.0f, 0.0f); + glRotatef(rotation->z(), 0.0f, 0.0f, 1.0f); }