开发者

True depth values using OpenGL readPixels

I'd like to retrieve the depth buffer from my camera view for a 3D filtering application. Currently, I'm using glReadPixels to get the depth component. Instead of the [0,1] values, I need the true values for the depth buffer, or true distance to the camera in world coordinates.

I tried to transform the depth values by the GL_DEPTH_BIAS and GL_DEPTH_SCALE开发者_高级运维, but that didn't 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 realize this is similar to Getting the true z value from the depth buffer, but I'd like to get the depth values into main memory, not in a shader.


Try using gluUnproject() after retrieving the normalized depth value from the Z-buffer as before. You'll need to supply the modelview matrix, projection matrix, and viewport values, which you can retrieve with glGetDoublev() and glGetIntegerv().


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

The depth buffer doesn't contain distance values from the camera. The depth values are the perpendicular distance to the plane of the camera. So if you really need radial distances to the camera, you'll need to compute them in a shader and write them to a buffer; the depth buffer isn't going to help.

but I'd like to get the depth values into main memory, not in a shader.

Then do what those shaders do, except in C/C++ (or whatever) code rather than in a shader. The math is the same either way. Just loop over each value in the depth buffer and transform it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜