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 ). 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!