PDA

View Full Version : Spotlight doesn't light up my scene



profoX
05-31-2011, 04:27 AM
Hello everyone!

I'm having some trouble with getting a spotlight to correctly light up my scene.

The center of my scene is at 0, 0, 0.

I translate and rotate my scene so that it seems like a player has moved,
I then reposition the light at 0, 0, 0 and draw the world around 0, 0, 0 as center point.

I am not seeing my spotlight however... Who knows what might be wrong?

// INIT:

glShadeModel(GL_SMOOTH); // Enable smooth shading
qglClearColor(Qt::black); // Set the clear color to a black background
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really nice perspective calculations

// Set up lighting
glEnable(GL_LIGHTING);
GLfloat fv[] = {1.0f, 1.0f, 1.0f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, fv);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, fv);

// Spot in center of room shining down
GLfloat ambient = {0.5f, 0.2f, 0.2f, 1.0f};
glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
GLfloat diffuse = {0.5f, 0.2f, 0.2f, 1.0f};
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
GLfloat specular = {0.5f, 0.2f, 0.2f, 1.0f};
glLightfv(GL_LIGHT1, GL_SPECULAR, specular);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 90.0f);
GLfloat direction = {0.0f, 0.0f, -1.0f};
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction);
glEnable(GL_LIGHT1);

// Set up textures
glEnable(GL_TEXTURE_2D);

// Set up blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

// Set up depth testing
glClearDepth(1.0f);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);

// RESIZE:

// Prevent divide by zero (in the gluPerspective call)
if (height == 0)
height = 1;

glViewport(0, 0, width, height); // Reset current viewport

glMatrixMode(GL_PROJECTION); // Select projection matrix
glLoadIdentity(); // Reset projection matrix

gluPerspective(45.0f, static_cast<GLfloat>(width)/height, 0.1f, 100.0f); // Calculate aspect ratio

glMatrixMode(GL_MODELVIEW); // Select modelview matrix
glLoadIdentity(); // Reset modelview matrix

// PAINT:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen and depth buffer

glMatrixMode(GL_MODELVIEW); // Select modelview matrix
glLoadIdentity(); // Reset modelview matrix

// Move scene to make it seem like player moves around
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f); // Rotate to direction player is watching
glTranslatef(xtrans, ytrans, ztrans); // Translate scene based on player position

// The center of the world is at (0.0f, 0.0f, 0.0f)

// Reset light position
GLfloat fv[] = {0.0f, 0.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT1, GL_POSITION, fv);

// Draw the world
for (int i = 0; i < displayLists.size(); ++i) {
glCallList(displayLists.at(i));
}

BionicBytes
05-31-2011, 06:35 AM
glMatrixMode(GL_MODELVIEW); // Select modelview matrix
glLoadIdentity(); // Reset modelview matrix

// Move scene to make it seem like player moves around
glRotatef(lookupdown, 1.0f, 0.0f, 0.0f);
glRotatef(sceneroty, 0.0f, 1.0f, 0.0f); // Rotate to direction player is watching
glTranslatef(xtrans, ytrans, ztrans); // Translate scene based on player position

// The center of the world is at (0.0f, 0.0f, 0.0f)

// Reset light position
GLfloat fv[] = {0.0f, 0.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT1, GL_POSITION, fv);


That's your problem...you have moved the current camera with glRotatef etc then immediately tried to reset the light position with glLightfv.
Are you sure this is what you want, since the call to glLightfv is affected by the value of the current ModelView matrix which has just been rotated and translated?