Shadow Mapping with GLSL

Hi again, i’ve recently been trying to work my way through some of the examples in the openGL Shading Language book and have been having trouble with the implementation of the shadow mapping example.
The shaders below are cut down versions of the book examples and are mainly for reference(though if anyone sees any huge mistakes do let me know :smiley: ). I understand this forum is for JOGL help so i’ll try and keep this brief.
Vertex shader:


varying vec4 ShadowCoord;
varying vec2 TexCoord; 

void main()
{
    vec4 texCoord = gl_TextureMatrix[0] * gl_Vertex;
    ShadowCoord   = texCoord / texCoord.w;

    gl_FrontColor  = vec4(gl_Color.rgb, gl_Color.a);
    TexCoord = gl_MultiTexCoord0.st; 

    gl_Position    = ftransform();
}

Fragment Shader :



uniform sampler2DShadow ShadowMap;

varying vec4 ShadowCoord;
varying vec2 TexCoord; 

float lookup(float x, float y)
{
    
    float depth = shadow2DProj(ShadowMap, ShadowCoord  ).r;
    return depth != 1.0 ? 0.75 : 1.0;
}
 
void main()
{
	float shadeFactor = lookup(0.0, 0.0);

        gl_FragColor = vec4( gl_Color.rgb shadeFactor ,  0);
}


Where i think the problem lies is in my construction of the matrix used to go from the camera’s view space to the light’s clip space.

The book says to achieve this by constructing the matrix :

Texture Matrix = Bias Matrix * Modelling matrix * light view matrix * light projection matrix

Whereas other sources seem to indicate that the inverse of the modelling matrix must be used (to get back to world space).

The code i am currently using to construct the matrix is :



        myGL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_COMPARE_MODE_ARB, GL.GL_COMPARE_R_TO_TEXTURE_ARB);
	myGL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_COMPARE_FUNC_ARB, GL.GL_LEQUAL);	
	myGL.glMatrixMode(GL.GL_TEXTURE); 
	myGL.glActiveTexture(GL.GL_TEXTURE0);

	myGL.glLoadMatrixf(cameraViewMatrix );
	myGL.glMultMatrixf(lightProjectionMatrix);
	myGL.glMultMatrixf(lightViewMatrix);
	myGL.glMultMatrixf(biasMatrix);


I’ve tried changing the ordering of multiplication (i can’t remember whether matrix multiplication is commutative :-[ ) and inverting the cameraViewMatrix but i seem to always end up with a horrible skewed mess of flicking shadows.
The depth map is definitely functioning properly and i’m totally perplexed.
Apologies for a very long, very individual question and thanks in advance if anyone can offer any help!

Hi hayfest,

First of all, iirc the shadow2DProj is doing the division by texCoord.w automatically, so you don’t have to do it in the vertex shader.

The standard texture matrix for shadow mapping goes like this:

[quote](Scale_Bias Matrix * Light Projection Matrix * Light View Matrix) * Inverse View Matrix =>
Shadow Matrix * Inverse View Matrix
[/quote]
Then in the vertex shader you do this:

varying vec4 ShadowCoord;

void main()
{
    gl_Position = ftransform();

    vec4 eyePos = gl_ModelViewMatrix * gl_Vertex; // vertex in eye coordinates
    ShadowCoord = gl_TextureMatrix[0] * eyePos;
}

The net result (per vertex) is this:

[quote]Shadow Matrix * Inverse View Matrix * ModelView Matrix * Vertex => // Inverse view and view cancel each other
Shadow Matrix * Model Matrix * Vertex =>
Shadow Matrix * World-space Vertex
[/quote]
Which is what you want. The crucial component is the inverse view matrix of course, which allows you to render your scene without doing anything extra per-object. You just set the view matrix and the shadow texture matrix at the beginning of the frame and then go through your scene, set the current model transformation for each object and render normally.

That’s cracking, i’ll give it a go and see how it turns out!