Take a look at this link:
http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/
Posted 21 January 2013 - 07:03 PM
Take a look at this link:
http://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/
Posted 21 January 2013 - 06:47 PM
// Depth pass vertex shader
output.vPositionCS = mul(input.vPositionOS, g_matWorldViewProj);
output.vDepthCS.xy = output.vPositionCS.zw;
// Depth pass pixel shader (output z/w)
return input.vDepthCS.x / input.vDepthVS.y;
// Function for converting depth to view-space position
// in deferred pixel shader pass. vTexCoord is a texture
// coordinate for a full-screen quad, such that x=0 is the
// left of the screen, and y=0 is the top of the screen.
float3 VSPositionFromDepth(float2 vTexCoord)
{
// Get the depth value for this pixel
float z = tex2D(DepthSampler, vTexCoord);
// Get x/w and y/w from the viewport position
float x = vTexCoord.x * 2 - 1;
float y = (1 - vTexCoord.y) * 2 - 1;
float4 vProjectedPos = float4(x, y, z, 1.0f);
// Transform by the inverse projection matrix
float4 vPositionVS = mul(vProjectedPos, g_matInvProjection);
// Divide by w to get the view-space position
return vPositionVS.xyz / vPositionVS.w;
}
Posted 21 January 2013 - 06:45 PM
You cannot just reconstruct the position from the depth alone. You need to have another buffer which contains the screen space position as well in order to that.