# SpotLight

## Recommended Posts

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);

glEnable(GL_LIGHT0);

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


##### Share on other sites
That code is in order? As in, you draw the rectangle first and then enable lighting? If yes, you need to have lighting enabled before you draw something you want lighting on.

You'll also most likely need more than 4 vertices to get a decent looking spotlight effect (unless you're doing per-pixel lighting, which you don't appear to be doing) because lighting is done per-vertex, and then the color values calculated at each vertex are interpolated between them for each pixel. So if no light is reaching any of the vertices (maybe the spotlight is too close to the rectangle) then it will interpolate black over the whole rectangle and it will appear to not be working.

##### Share on other sites

How do i do per-pixel lightning?

Lizard

##### Share on other sites
Could you give me any links to any source codes too? :)

##### Share on other sites
Same site, in the menu on the left there is a download. There you can downlaod the source of the tutorials, with a simple opengl framework too. Here if your lazy.

Lizard

##### Share on other sites
Thnak You very much:)

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628305
• Total Posts
2981967

• 9
• 13
• 11
• 12
• 11