PDA

View Full Version : SpotLight



MACnus
02-14-2005, 05:38 AM
I'm having trouble with spotlights

I use this code to make a spotlight point in the middle of a rectangle, the problem is that the light doesn't work.
Can anybody help me?
Tnx

GLfloat diffuseLight[] = {0.8, 0.8, 0.8, 1.0};
GLfloat specularLight[] = {1.0, 1.0, 1.0, 1.0};
GLfloat ambientLight[] = {0.2, 0.2, 0.2, 1.0};
GLfloat lightPos[] = {0.0, 0.0, 5.0, 1.0};
GLfloat specularReflection[] = {1.0, 1.0, 1.0, 1.0};
GLfloat spotDir[] = {0.0, 0.0, -30.0};


glPushMatrix();
glTranslatef(-20, -20, -30);
glColor4f(1, 1, 1, 1);
glBegin( GL_TRIANGLE_STRIP );
glTexCoord2i(0, 1); glVertex3i(40, 0, 0); // Top right
glTexCoord2i(1, 1); glVertex3i(0, 0, 0); // Top left
glTexCoord2i(0, 0); glVertex3i(40, 40, 0); // Bottom right
glTexCoord2i(1, 0); glVertex3f(0, 40, 0); // Bottom left
glEnd();
glPopMatrix();


glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);

glLightfv(GL_LIGHT0, GL_POSITION, lightPos);

glMaterialfv(GL_FRONT, GL_SPECULAR, specularReflection);
glMateriali(GL_FRONT, GL_SHININESS, 128);

glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDir);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2.0f);

glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0f);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0f);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0);

glEnable(GL_LIGHT0);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_NORMALIZE);

02-14-2005, 08:50 AM
You are facing the common OpenGL pitfall #2

http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/