Sign in to follow this  

Recovering world position in postprocess

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

I have the depth information encoded in rendered quad's alpha channel. The process should be straightforward but I'm running into some trouble which seems to be projection related. So, it would be great if someone could lay out how to form the equations and the matrices. Thanks!

Share this post


Link to post
Share on other sites
When you rendered the depth buffer, you transformed the point from world-space to projection-space. Now you just need to invert the transform to get it back to world-space.

You have the projection-space Z value. You must recreate the X and Y values using texture coordinate interpolators. Then just multiply by the inverse of the world-to-projection matrix and divide by W. Viola!


// code from memory. not tested.
float2 UV; //(0,0)->(1,1) full screen quad UVs
float depth = tex2d(sampler, UV);
float2 clipUV = UV*2-1; // map 0,1 to -1,1
float4 projPos = float4(clipUV.x, clipUV.y, depth, 1);
//projToWorld = inverse(worldToProj);
float4 worldPos = mul(projToWorld, projPos);
worldPos /= worldPos.w;



If you are using DX9 or less, you will need to add that annoying half-texel offset when sampling the depth buffer, but don't let it slip into the clipUV value.

This is all from vague memory. No warranties of correctness. :P

[Edited by - corysama on February 21, 2008 1:40:23 AM]

Share this post


Link to post
Share on other sites
I'm still having the same problem, maybe someone can find a diagnosis from the symptoms:

When I look directly at an axis (eye vector is perpendicular to the axis), the world positions (for that axis) are correct. But when I rotate the view a little bit, the world positions change. Does that ring any bells?

Share this post


Link to post
Share on other sites
I have the same problem. Rotating the camera for some reason changes the world position.
All I do is

depth * viewDir + cameraPos

where viewDir is a interpolated vector from the VS, pointing to the far corners of the view frustum (as described in some other threads here)

I tried to visualize the y-component of the calculated worldpos using this line:

return lerp(float4(1,0,0,1), float4(1,1,1,1), worldPos.y / 90.0);

the maximum height of my scenery is about 90f, so this should interpolate nicely from a white to red color depending on the height. However, when I move my camera up/down, the colors entirely change, although neither the camera position changes, nor does the world pos of the actual scnenery.

what might be wrong here?

[Edited by - FoxHunter2 on February 19, 2008 10:01:57 AM]

Share this post


Link to post
Share on other sites
This issue is really bugging me, that's why I made some screenshots for you:



Left: normal scene (looks okay, floor is red and ceiling is white)
Middle: camera looks up
Right: camera looks down

Why do the colors change? Actually, the world positions should remain the same.
If I rotate the camera around the Y-axis (left, right) then the colors stay the same, which makes sense because the world position doesn't change.

Share this post


Link to post
Share on other sites

This topic is 3580 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this