Jump to content
  • Advertisement
Sign in to follow this  
Unfadable

Extracting world position using scene depth

This topic is 4127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!