eVoid Development Weblog Burn The Land And Boil The Sea But You Can't Take The Sky From Me

18Jul/100

Update: Mess with depth textures

I've been stuck at a problem for a week now without making any progress. My goal was 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 simple shader to write the z-data.

The following screenshot shows the result:

Depth Texture Problem #1

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:

Depth Texture Problem #2

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 code for running the application:
http://pastebin.com/yJgvQpnK

Otherwise you could use the library of your choice for creating the GL context, like glut or freeglut or whatever.

I've already posted on several forums without getting any further leading advices.

  • Share/Bookmark
Kommentare (0) Trackbacks (1)

Kommentar schreiben