Extracting world position using scene depth

Started by
3 comments, last by Yann L 17 years ago
If I store my scene depth in the alpha channel of my framebuffer, I'd like to be able to retrieve the world position using that and the pixel's location. Is there a way to do this?
Advertisement
Query the depth value, get the direction from the camera to the pixel (you'll need to use the frustum as a reference point to account for all possible viewport width's (lens angles really)), calculate new position by adding the normalized direction from the camera to the point multiplied by the depth value to the camera's position and you'll have the point's position in what ever coordinate system you're storing your camera position in (usually world coordinates, which is what you most likely want).
Thanks for the tip.
PS - a thing about depth values - unfortunately I have no idea how to help you with this in either OpenGL or D3D: at least on OpenGL, depth values are normalized in the depth buffer (at least using glReadPixels()), so just querying them won't be enough. Things become more complicated by the fact that the depth buffer has exponential precision, which means that it is pretty imprecise. Just something to keep in mind...

The foolproof solution would be tracking the point on CPU and applying all of the transformations to the required object yourself (you'll just need to cast a ray from the eye to into the scene to get a point of intersection).

Then again, maybe there's an extension out there that does all of this for you.
Quote:Original post by Unfadable
If I store my scene depth in the alpha channel of my framebuffer,

You can't, unless you are using a floating point (or other high precision) framebuffer. A typcial 8bit alpha component has not enough precision (by far) to hold anything coming even near to an accurate depth value. At least 16 bit are needed for this, better more.

Instead, you can of course use the depth buffer itself. The fact that values are normalized doesn't matter. Under OpenGL, you can just use gluUnProject() to turn a depth value (with screen space x and y) back to a world space position. D3D probably has a similar function. Or you can apply the reverse transformation yourself.

This topic is closed to new replies.

Advertisement