My idea is to use the shadow map to cast a shadow over the scene after its opaque part has been fully rendered (kind of deferred shadowing). So what I am doing is to render a scene from a light’s view (depth only), render the (opaque) scene from the viewer’s eye position, then render a fullscreen quad over the scene, comparing each depth value of the scene with the corresponding depth value from the shadow map:
enable client states for GL_TEXTURE0
bind scene color buffer to GL_TEXTURE0
enable client states for GL_TEXTURE1
bind scene depth buffer to GL_TEXTURE1
bind shadow map to GL_TEXTURE2
load shadowmap transformation matrix to texture matrix of GL_TEXTURE2
render fullscreen quad, using shader as below
The shadow map’s texture matrix has been setup as follows after having set the camera to the light’s position and view direction:
float lightProjection [16];
float lightModelview [16];
float shadowMapTransformation [16];
glGetFloatv(GL_PROJECTION_MATRIX, lightProjection);
glGetFloatv(GL_MODELVIEW_MATRIX, lightModelview);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.5f, 0.5f, 0.5f);
glScalef(0.5f, 0.5f, 0.5f);
glMultMatrixf(lightProjection);
glMultMatrixf(lightModelview);
glGetFloatv(GL_TEXTURE_MATRIX, shadowMapTransformation);
Vertex shader:
varying vec4 vertex;
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_TextureMatrix[2] * gl_Vertex;
gl_Position = ftransform();
gl_FrontColor = gl_Color;
}
Fragment shader:
uniform sampler2D frameBuffer;
uniform sampler2D depthBuffer;
uniform sampler2D shadowMap;
void main()
{
float sceneDepth = texture2D (depthBuffer, gl_TexCoord [0]).r;
float shadowDepth = texture2DProj (shadowMap, gl_TexCoord [1]).r;
float light = 0.25 + ((sceneDepth < shadowDepth + 0.0005) ? 0.75 : 0.0);
vec3 sceneColor = texture2D (frameBuffer, gl_TexCoord [0]).rgb;
gl_FragColor = vec4 (sceneColor * light, 1.0);
}
Problem is: It doesn’t work.
Is the texture2DProj call with gl_TexCoord [1] wrong (i.e. is gl_TexCoord[1] setup wrong)?
Do I need to multiply the shadow map transformation matrix with the inverse of the viewer’s camera modelview matrix?