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?

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.

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.

Have you some how disabled depth tests? Or cleared the depth buffer before you read?

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?

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.

Actually, even what I read is not totally right. It seems only very few good values. So it is not usable.

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).

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.