Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reconstrucing World Position from Depth?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 XBTC   Members   -  Reputation: 122

Like
0Likes
Like

Posted 21 December 2011 - 01:13 PM

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

Sponsor:

#2 ZBethel   Members   -  Reputation: 544

Like
0Likes
Like

Posted 21 December 2011 - 02:18 PM

Is there a reason you can't just use the inverse WorldViewProjection matrix and save yourself the hassle of manually unprojecting it?

#3 XBTC   Members   -  Reputation: 122

Like
0Likes
Like

Posted 21 December 2011 - 02:54 PM

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

#4 bzroom   Members   -  Reputation: 646

Like
0Likes
Like

Posted 21 December 2011 - 04:15 PM

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.

#5 XBTC   Members   -  Reputation: 122

Like
0Likes
Like

Posted 21 December 2011 - 04:20 PM

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...Posted Image

#6 ZBethel   Members   -  Reputation: 544

Like
0Likes
Like

Posted 21 December 2011 - 04:32 PM

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.

#7 JorenJoestar   Members   -  Reputation: 173

Like
0Likes
Like

Posted 11 January 2012 - 07:42 AM

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!
---------------------------------------http://badfoolprototype.blogspot.com/




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS