How to get the real depth value

I was now working on an IBR altorithm. I need to read out depth value of all pixels of the display window. I have tried glReadPixels function. But the depth values are limited to 0.0-1.0. What I need is the real z value in the 3D coordinate space. Is anyone that who have use something like this? I appreciate your help very much.

You could write vertex and fragment shaders which calculate any value you like and write that into a separate floating point buffer by using multiple render targets or a multipass algorithm.

This is what you need:

dist = (NEAR_PLANE*FAR_PLANE/(NEAR_PLANE-FAR_PLANE))/(zbuffer[ii][iii]-FAR_PLANE/(FAR_PLANE-NEAR_PLANE));

For more discussions on how the z is calculated I recomend this document: http://sjbaker.org/steve/omniv/love_your_z_buffer.html

Thor Henning Amdahl

Relic, you’re a funny guy.

Thank you for all of your helps.

Now I have another question for the equation posted by Thor Henning Amdahl. What does zbuffer[ii][iii] represent? Can I simply treat it as z-buffer value directly read from depth buffer whose value is from 0.0-1.0?

I am looking forward to your reply! :slight_smile:

You are right. You can simply insert the values 0-1 read from the depth buffer.

From the code:

GLfloat zbuffer[XRES][YRES];

glReadPixels(0,0,XRES,YRES,GL_DEPTH_COMPONENT,GL_FLOAT,zbuffer);

I tried it in your approach, and it works very well. I appreciate your help very much.

Thank you so much, Thor Henning Amdahl :slight_smile:

Hawaii