# OpenGL Lighting Problem

This topic is 4822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just got Beginning OpenGl game programming and I just can't seem to get lighting to work. I have been working on creating a what I thought would be a simple spot light for over 2 hours now and can't seem to figure out why its not working. I have a green quad at 0,0,0 and want to shine a spot light on it. So I place my camera 20 units out of the screen and expect it to shine. But the thing is if my cut-off value is less than 20 then you just can't see the shape and even if it is higher it still ends up being really dark black. I think I am setting it up right but I don't know why it won't work.
bool SetupGL(int width,int height) {
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background				// Enable Depth Testing
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);    // Allow depth testing
glDepthFunc(GL_LEQUAL);	    // Type of depth testing

glViewport(0,0,(GLsizei)(width),(GLsizei)(height));	//Set up viewport
glMatrixMode(GL_PROJECTION);	// Switch to projection to set up view
gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,0.1f,400.0f);   // Apply the perspective

glMatrixMode(GL_MODELVIEW);	//switch to modelview

GLfloat pos [] = { 0.0f,0.0f,20.0f,1.0f };   // Set the position
GLfloat dir [] = { 0.0f,0.0f,-1.0f };        // Direction pointing into screen
GLfloat color [] = { 0.2f,0.2f,0.2f,1.0f }; // Set the color

// Set up light
glEnable(GL_LIGHTING);  // Enable Lighting
glEnable(GL_LIGHT0);    // Enable light0
glLightfv(GL_LIGHT0,GL_POSITION,pos);    // Set the position
glLightfv(GL_LIGHT0,GL_AMBIENT,color);  // Set the type of light
glLightfv(GL_LIGHT0,GL_DIFFUSE,color);
glLightfv(GL_LIGHT0,GL_SPECULAR,color);
glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,dir);  // Set the direction
glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,20.0f);   // Set the cutoff

return true;
}


That is my init code
void Render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear the buffers

gluLookAt(0.0f,0.0f,20.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);    // Set up camera

glColor3f(0.0f,1.0f,0.0f);      // Green
glNormal3f(0.0f,0.0f,1.0f);     // Point out
glVertex3f( 5.0f, 5.0f,0.0f);   // Top Right
glVertex3f(-5.0f, 5.0f,0.0f);   // Top Left
glVertex3f(-5.0f,-5.0f,0.0f);   // Bottom Left
glVertex3f( 5.0f,-5.0f,0.0f);   // Bottom Right
glEnd();

SDL_GL_SwapBuffers(); // Swap the buffers
}


and That is my rendering code. Can anyone help me try to figure this out. Thanks, vbuser

##### Share on other sites
GL lighting is vertex-based. That is, it only computes the color for each vertex, and then interpolates it across the primitive. Since all you have is a single large quad, it only computes the lighting in the 4 vertices, not "inside" it. If you want more detailed lighting, you must tesselate the polygon, that is, divide it into NxN smaller quads. The more you tesselate it, the better it will look. There are techniques which override GL lighting and perform lighting per-pixel, but since you're a beginner you'd better off experimenting with traditional GL lighting for now.

Also, one other thing. The light position specified with glLightf(...) is transformed by the current modelview matrix, just like any vertex, and stored in eye space. For that reason, you must update the light position in each frame after the camera setup code- in your case, right after gluLookAt(...)

##### Share on other sites
Thanks I was going crazy trying to figure out why it was not working. I will have to check out per pixel lighting later so I get a better effect.
Thanks again,
vbuser

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 9
• 9
• 13
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007284

×