True Depth Values โ€‹โ€‹Using OpenGL readPixels

I would like to get the depth buffer from my camera view for a 3D filtering application. I am currently using glReadPixels to get the depth component. Instead of the values โ€‹โ€‹[0,1], I need the true values โ€‹โ€‹of the depth buffer or the true distance to the camera in world coordinates.

I tried converting depth values โ€‹โ€‹using GL_DEPTH_BIAS and GL_DEPTH_SCALE, but that did not work.

glReadPixels(0, 0, width_, height_, GL_DEPTH_COMPONENT, GL_FLOAT, depth_buffer); glGetDoublev(GL_DEPTH_BIAS, &depth_bias); // Returns 0.0 glGetDoublev(GL_DEPTH_SCALE, &depth_scale); // Returns 1.0 

I understand that this is similar to Getting the true z value from the depth buffer , but I would like to get the depth values โ€‹โ€‹in the main memory, not in the shader.

+4
source share
2 answers

Try using gluUnproject() after retrieving the normalized depth value from the Z buffer, as before. You will need to provide the model matrix, projection matrix, and values โ€‹โ€‹in the view, which you can get with glGetDoublev() and glGetIntegerv() .

+2
source

Instead of the values โ€‹โ€‹[0,1] I need the true values โ€‹โ€‹for the depth buffer or the true distance to the camera in world coordinates.

The depth buffer does not contain distance values โ€‹โ€‹from the camera. Depths are the perpendicular distance to the plane of the camera. Therefore, if you really need radial distances to the camera, you need to calculate them in the shader and write them to the buffer; depth buffer will not help.

but I would like to get the depth values โ€‹โ€‹in the main memory, not in the shader.

Then do what these shaders do, except for the C / C ++ code (or something else), and not in the shader. The math is the same anyway. Simply loop over each value in the depth buffer and convert it.

+2
source

All Articles