Shadows not being projected correctly

Started by
6 comments, last by DeathCarrot 16 years, 2 months ago
So, here's what I'm doing: Set projection matrix to light's gluPerspective Set modelview matrix to light's gluLookAt Render the scene geometry into a depth-only FBO Set projection matrix to players's gluPerspective Set modelview matrix to players's gluLookAt Make texture matrix using: (bias matrix) * (light's projection) * (light's modelview) * (camera's modelview inverse) Render scene properly using the following shadow coordinates specified in the vert shader:

shadowCoord = gl_TextureMatrix[3] * gl_ModelViewMatrix * gl_Vertex + vec4(0.0,0.0,-0.004,0.0);
the offset is there to prevent z-fighting. This is what happens: However, if I place the gluLookAt for both the light and scene camera into the projection matrix, shadows are rendered correctly (but lighting breaks): Am I missing out on a step? The drawing of the geometry includes calling glLightfv with GL_POSITION, but taking it out didn't help. I'm not sure what other details might be requried, but here's the main bits. Thanks.
Advertisement
Strangely, if I remove the inverse modelview from the matrix and the modelview multiplication from the shader, it works as expected - when the vertices aren't affected by the modelview matrix, such as with the floor, the shadow is rendered properly, but it obviously falls down when rendering on geometry affected by the modelview matrix.
(bias matrix) * (light's projection) * (light's modelview) * (camera's modelview inverse)

No, that should be
(bias matrix) * (light's projection) * (light's modelview) * (object matrix)

and use polygon offsetting to avoid z-fighting.

And for rendering the scene
shadowCoord = gl_TextureMatrix[1] * ObjectMatrix * gl_Vertex;

The texture matrix should have
(bias matrix) * (light's projection) * (light's modelview)
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Wouldn't I have to compute an object matrix separately for each object then? The texture matrix contains the camera's modelview inverse so it would cancel out the gluLookAt from the modelview matrix inside the vert shader, leaving just the object transformation.
The complete transformation I'm doing is:
(bias matrix) * (light's projection) * (light's modelview) * (camera's modelview inverse) * (camera's and object's modelview).
The last two (should) cancel out the camera transformation. At least that's how I've understood the implementation.

Obviously something's gone wrong somewhere, though.
1. Yes, you would compute the object matrix separatly.

2. The modelview should contain light_lookAt * objectMatrix for your
gl_Position = ftransform();

3. When you project from the light's pespective, the light is the camera.
No need for "camera's modelview inverse"
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
1. That shouldn't be required for shadow mapping. (I guess that is one way to do it - but I'd like to avoid it if possible)
2. The vertex position should contain the light's view transformation when rendering the scene from the player's perspective?
3. What I mean by "camera's modelview inverse" is the inverse of the gluLookAt used when rendering the scene properly from the player's perspective, the only purpose of that matrix is to cancel out the camera transformation and leave just the object transformation (when multiplied by the modelview matrix in the vertex shader).
For 1 and 3, I guess it depends on what you are doing. I'm answering based on what I'm doing.

Quote:
2. The vertex position should contain the light's view transformation when rendering the scene from the player's perspective?


When rendering from the player's perspective, the light is no longer the "camera".
The answer is simply : no
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
I've solved the issue with the help of the offical OpenGL forums, turns out the library I'm using doesn't invert matricies properly.

This topic is closed to new replies.

Advertisement