I am trying to get glsl shadow maps working… but I am getting a discrepancy between the z depth calculated by the opengl fixed pipeline and my homemade glsl version. And I don’t know why.
So I render to a framebuffer object with a depth buffer attached (code in Java)…
IntBuffer dbo = BufferUtil.newIntBuffer(1);
scene.gl.glGenTextures(1, dbo);
DBO = dbo.get(0);
scene.gl.glBindTexture(GL.GL_TEXTURE_2D, DBO);
scene.gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_DEPTH_COMPONENT32, mapsize[0], mapsize[1],
0, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, null);
scene.gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
scene.gl.glFramebufferTexture2DEXT(GL.GL_FRAMEBUFFER_EXT, GL.GL_DEPTH_ATTACHMENT_EXT,
GL.GL_TEXTURE_2D, DBO, 0);
My projection/view for rendering to the fbo is bog standard…
glu.gluPerspective(spotangle * 2.0f, (float) mapsize[0] / (float) mapsize[1], 1000.0f, 6000.0f);
and
glu.gluLookAt(from[0], from[1], from[2], to[0], to[1], to[2], 0.0f, 1.0f, 0.0f);
I grab each matrix with…
scene.gl.glGetFloatv(GL.GL_PROJECTION_MATRIX, projMat, 0);
scene.gl.glGetFloatv(GL.GL_MODELVIEW_MATRIX, viewMat, 0);
and assign to a texture matrix with scale and translate to remap to [0,1]…
scene.gl.glActiveTexture(GL.GL_TEXTURE1);
scene.gl.glMatrixMode(GL.GL_TEXTURE);
scene.gl.glLoadIdentity();
scene.gl.glScalef(0.5f, 0.5f, 1.0f);
scene.gl.glTranslatef(1.0f, 1.0f, 0.0f);
scene.gl.glMultMatrixf(projMat, 0);
scene.gl.glMultMatrixf(viewMat, 0);
And now in my vertex shader (as per orange book)…
varying vec4 shadowCoord[MAXLIGHTS];
…
vec4 texCoord = gl_TextureMatrix[i+1]*gl_Vertex;
shadowCoord[i] = texCoord/texCoord.w;
…
and frag shader (nearly as per orange book)…
float zmap = texture2DProj(shadowmap[i], shadowCoord[i]).x;
float zobj = shadowCoord[i].z/shadowCoord[i].w;
I get two different answers for zmap and zobj (when a point is not occluded from the light pov). I would assume they should be equal (save for a bit of rounding etc. and usual shadow bias issues). But they are quite different. They both seem to have perspective depth type values (in the rangle [0,1]) but they seem to have listened to different near and far clip values - maybe. I thought perhaps glDepthRange but no.
I want use texture2D and not shadow2D because I want to do ‘things’ with my returned z depth. As Iunderstand it the Proj versions do the divide by w for you. So why the orange book uses shadow2DProj when it did the divide by w in the vertex shader, probaby means that I have missed something.
So I like to refer to documentation… but this is opengl. Mathematicaly, what should I expect to find in my depth buffer?
Anyone know why my two zs are different?
Any help, much appreciated. I am close to crying!