Content of a depth texture
I'm copying the depth buffer to a floating point texture like this:
glBindTexture( id )
But then in a fragment shader, I can't tell exactly what I get when I sample the texture. What I get does seem to make sense (if I do gl_FragColor = vec4( depth, depth, depth, 1 ), what is closer to the camera is darker)I know it is in the range [0,1]. I tried quantizing the values and on a 3D plane, I get banding where each band is of equal size, which seems to suggest the data is linear (which was a surprise to me because gl_FragCoord.z doesn't have a linear relationship with world-space distance of fragments to the camera).
My question is: what is the format of that data, and how can I transform it to absolute distance to eye?
Well, the depth value that gets written by the fragment shader to the depth buffer by default is a 0..1 window-space value (assuming you've left your glDepthRange the default 0..1). If your depth buffer is fixed-point (e.g. in the pseudo-standard DEPTH24_STENCIL8 form), then that 0..1 range is just stretched across 0..2^24-1.
Originally Posted by sdrouin
For perspective projections, this window-space depth value is non-linear relative to eye-space depth. For orthographic projections, it's linear.
In olden days, you would (and still can if you want) use gluUnProject() to back out from window-space to eye-space or object-space. Basically this just involves undoing the viewport transform, the projection transform and the modelview transform (use viewing for modelview if you want to stop in eye-space not object-space). You can find the code on the net for this or just do it yourself. Just google "position from depth". There are several ways to go about it. Here's one of many (for perspective projection specifically): link
Presumably when you copy your depth buffer to a float depth texture, you'll end up with nice 0..1 window-space depth values, ready for you to back-project if desired.
Last edited by Dark Photon; 06-15-2012 at 08:21 PM.
Tags for this Thread