# Recovering world position in postprocess

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

## 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 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 UVsfloat depth = tex2d(sampler, UV);float2 clipUV = UV*2-1;  // map 0,1 to -1,1float4 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 on other sites
Thanks corysama. One question: Why are you multiplying projPos by WorldToProj? Isn't it supposed to be multiplied by the inverse of it (projToWorld)?

##### Share on other sites
Quote:
 Thanks corysama. One question: Why are you multiplying projPos by WorldToProj? Isn't it supposed to be multiplied by the inverse of it (projToWorld)?

Yeah i think it should be projToWorld

##### 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 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 on other sites
Any help regarding the camera issue from the experts? :)

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

1. 1
Rutin
48
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009845
• ### Who's Online (See full list)

There are no registered users currently online

×