PDA

View Full Version : lighting



Fred F
08-13-2008, 09:39 PM
I am trying to write the following code so which is part of a robot that has a flashlight pointing forward. When I rotate the robot I want the flashlight to point in the direction the robot is facing. When I turn right it works great but when I turn left the light does not point forward.
Anyone have any suggestion on how I can fix this.


glPushMatrix();
glTranslatef(0.3f,1.0,-0.05f);
GLUquadricObj* Rarm=gluNewQuadric();
gluQuadricNormals(Rarm,GLU_SMOOTH);
gluCylinder(Rarm,0.1,0.1,0.8,20,20);
glTranslatef(0.0,-0.2,0.8);
float pos[4]={0.0,0.0,0.0,1.0};
glLightfv(GL_LIGHT1, GL_POSITION, pos);
glLightfv(GL_LIGHT1, GL_SPECULAR, lightcolor);
glLightfv(GL_LIGHT1, GL_AMBIENT, lightcolor);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightcolor);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.0);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.6);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.4);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
if (flashlight)
{
float emlight[4];
for (int i=0;i<4;i++)
emlight[i]=lightcolor[i]/3.0f;
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,emlight );
}
GLUquadricObj* light=gluNewQuadric();
gluSphere(light,0.1,20,20);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
if (flashlight)
{
float blackcolor[4]={0.0,0.0,0.0,1.0};
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,blackco lor);
}
gluDeleteQuadric(Rarm);
gluDeleteQuadric(light);
glPopMatrix();
}