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.

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);

//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 on other sites
Is there a reason you can't just use the inverse WorldViewProjection matrix and save yourself the hassle of manually unprojecting it?

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

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 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:

Hope this helps!

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• Forum Statistics

• Total Topics
634111
• Total Posts
3015560
×