Hi,
my goal is to draw some cubes into a framebuffer object, draw the result back into the default framebuffer and finally draw something more (a single quad) into the default framebuffer that correctly overlaps with the stuff drawn before. In order to achieve this I need to re-use the z-data from the framebuffer object. I have a depth-texture attached to the framebuffer object and when drawing back to the default framebuffer, I’m using a shader to write the z-data.
The following screenshot shows the result:
As you can see, the overlapping doesn’t work properly.
I have written a minimal application which reproduces the problem, more on that later. Yesterday I’ve been playing around with the code and noticed that if I make the range between the near clipping plane and the far clipping plane smaller, the ‘effect’ shown above becomes smaller. The following screenshot shows the same scene with the near clipping plane moved from 0.1f to 1.f and the the far clipping plane from 100.f to 50.f:
So I believe the depth values are somehow quantized far too much. But why? It cannot be the precision of my depth texture which is 24 bit since if I change it to 32 bit the ‘effect’ stays exactly the same. If it would be caused by a too low precision of the depth texture, I would have expected the ‘effect’ to become less.
I can think of two possible reasons though I’m not able to explain either of them. So either the projection matrix is getting messed up somehow so that the quotient of the clipping planes becomes superdimensional. Or the quantization is caused by the “gl_FragDepth = texture2D( depthtex, gl_TexCoord[0].xy ).a” instruction in the fragment shader.
Vertex shader:
void main()
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}
Fragment shader:
#version 120
uniform sampler2D texture0;
uniform sampler2D depthtex;
void main()
{
gl_FragColor = vec4( texture2D( texture0, gl_TexCoord[0].xy ).xyz, 1.0 );
gl_FragDepth = texture2D( depthtex, gl_TexCoord[0].xy ).a;
}
The minimal application can be found here:
http://pastebin.com/UctqYW33
If you have SFML2, you can use the following wrapper code for running the application:
http://pastebin.com/yJgvQpnK
Otherwise use the library of your choice for creating the GL context, like freeglut or whatever.
I would be very happy if someone could take a look on it.
I have no clue what I am doing wrong or just why it doesn’t work.
Thanks in advance!