Advertisement Jump to content
Sign in to follow this  
XBTC

Reconstrucing World Position from Depth?

This topic is 2566 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

Hi Guys,

I have a horrible time reconstructing the World-Position of Pixels in a ShadowMap.


I generate the Shadowmap by rendering from the light's point of view into a texture. I store the ViewSpace z-value of the vertices as depth values in the texture.

Then it should be possible to reconstruct the world position in the pixel shader like that:

//From Texcoords to NDC's
//tpos is the position of the pixel in the Shadowmap
posViewSpace.x = 2 * tpos.x - 1.0;
posViewSpace.y = - 2 * tpos.y + 1.0;

//Undo Projection
posViewSpace.x *= tan(FOVinRadians / 2) * AspectRatio;
posViewSpace.y *= vol_pos.y * tan(FOVinRadians / 2);

//Read Depth Value
posViewSpace.z = g_txShadowMap.SampleLevel(samShadow, tpos.xy, 0);

//Get World Position
posWorldSpace = mul(posViewSpace, InverseWorldViewMatrix);

The resulting posWorldSpace is wrong unfortunately. Please please let me know where I am doing wrong...

P.s.: I know there are many approaches which can be found via the search on these boards and I looked at them, however I would like to get my solution to work as it fits very will with other things I am using the Shadowmap for...

Share this post


Link to post
Share on other sites
Advertisement
Thanks for your reply. The reason for it is that I am using the linear view-space z-value of the vertices into the ShadowMap...

Share this post


Link to post
Share on other sites
Have you looked at this? http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/

It uses the frustum corners and the linear depth to compute the position.

You would use your FOV, tan, etc to compute the frustum corners, pass them in as constants to your pixel shader.
Then interpolate those to compute a ray and use your linear depth along that ray to compute the position.

Share this post


Link to post
Share on other sites
Thanks for the link! It's one of the best descriptions for the frustum corner method. I still would like to know WHY my approach does not work...dry.gif

Share this post


Link to post
Share on other sites
My guess is that the math you're doing to unproject the x and y coordinates is not a direct inverse of what the projection matrix is doing. I don't know if you're using OpenGL or DirectX, but you could look at how the projection matrix is constructed and compute the formula for the inverse. Alternatively you could do two matrix multiplies--one for the inverse projection and one for the inverse world view.

Zach.

Share this post


Link to post
Share on other sites
Possibly this line is broken:

posWorldSpace = mul(posViewSpace, InverseWorldViewMatrix);

because you are moving from the viewSpace to object space. To be back to world space you need to multiply for the inverse of the view matrix, that maps from view to world space.


Also, if you need other more informations I've put some stuff together on my blog:

http://badfoolprototype.blogspot.com/

Hope this helps!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!