Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Content of a depth texture

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2009
    Posts
    9

    Content of a depth texture

    I'm copying the depth buffer to a floating point texture like this:

    glBindTexture( id )
    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,0,GL_DEPTH_COMPONENT32F,width,height,0,GL_DEPTH_COMPONENT,GL_FLOAT,0);
    glReadBuffer(GL_BACK);
    glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,0,0,0,0,0,width,height);

    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?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,220
    Quote Originally Posted by sdrouin View Post
    I'm copying the depth buffer to a floating point texture like this ... 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.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •