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 9 of 9

Thread: Why I can't get the depth value with glReadPixel()?

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    10

    Why I can't get the depth value with glReadPixel()?

    I am using an opengl rendering program to do some simulation. Unfortunately, the model redering code is not availabe. What I'm doing is first loading and rendering a model with this program, and the model can be selected and translated, then trying to get the fragment information including z-buffer for further simulation. But when I use glReadPixel() to get z-buffer, all the values are 1.0, even the fragment at the model displaying area. I know it should be between 0.0 and 1.0.

    Because I don't know how the model is rendered in the program, so anyone could help what the problem is and in which rendering case the z-buffer can not be read correctly. Is there any way to get the depth value of each pixel?

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,213
    Are you sure they're all exactly 1.0, or is it possible they're all near 1.0?

    Readback the full depth buffer, and for each value, print out anything that isn't exactly == 1.0. Also try printf( "%.16f", value ) to see all the digits of precision.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    10
    I checked all the digits with a cylinder from front view. The axes of the cylinder is in the screen.

    When I first run the code, z-buffers not equal to 1.0 are like this in the range of the model (I omitted some digits here):
    0.492642 0.476300 0.467576 0.460798 0.455382 0.450774 0.445838 0.442736 0.439400 0.436064 0.433211 0.430743 0.428274 0.425806 ...
    0.500000
    0.500000
    .
    .
    .
    0.492642 0.476300 0.467576 0.460798 0.455382 0.450774 0.445838 0.442736 0.439400 0.436064 0.433211 0.430743 0.428274 0.425806 ...

    They looks like the outline of the cylinder with all the inner depth being 1.0. But when I run again, they would be all 1.0.
    Last edited by li.j.z; 12-21-2013 at 09:40 PM.

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Have you some how disabled depth tests? Or cleared the depth buffer before you read?

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    10
    I'm not sure about this because the program I'm working with was not written by myself. Do you know how to check if the depth buffer is cleared? And do you know if the model is rendered with FBO or texture or some other method, is it possible that we cannot read z-buffer?

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    Since your read good values once it is unlikely the z-buffer has been removed. When thez-buffer is cleared it is (by default) set to 1.0 which is what you are seeing. If you do not access to the code, the best you can do is to try to move the pixel read to somewhere else in the code.

  7. #7
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    10
    Actually, even what I read is not totally right. It seems only very few good values. So it is not usable.

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,117
    The data you showed in the earlier post looked ok. Really if you do not have access to the source you will struggle to solve your problem. The only buffer that you can guarantee to have access to is the actual window buffer but it does not have to have a depth buffer although it usually does. You can get at it by doing a glbindbuffer(0).

  9. #9
    Junior Member Newbie
    Join Date
    Dec 2013
    Posts
    10
    The data I showed are not right. It just seems the boundary of the model. All the values inside are all 1.0. And when I called glReadPixel again, all the data will be 1.0 even at the boundary.

Posting Permissions

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