   Questions regarding reconstructing position from depth map
  Im doing some research in reconstructing positions from depth maps, and there are a few things puzzling me.First off. Why is it common for people to be reconstructing from an NDC depth map? (ie 0-1 for Directx). If you have a lot of graphical code that requires positions in some world or view space, why not have a separate render target for that? rather than continualy converting to and from. Such asRenderTarger[0].Colour   // Normal rendering bufferRenderTarger[0].Depth    // Normal NDC depth buffer for normal depth testingRenderTarger[1].Colour   // Used to store position in world or view space (float3)RenderTarger[1].Depth    // Not usedSecondly, i keephearing people mentioning of using the inverse of the WorldViewProj to get back at the position values in Local space. But i thought that the projection matrix couldnt be inversed? As you effectivly lose the .z data.Ive been looking at a lot of posts by the member MJP, such ashttp://www.gamedev.net/community/forums/topic.asp?topic_id=474166&PageSize=25&WhichPage=1Where he talks ofStep 1:  create normalized eye-space depth bufferIn vertex shader:-calculate eye-space position of vertex (transform by worldView matrix for D3D, modelView matrix for GL)-pass eye-space position to fragment shaderIn fragment shader:-divide z-component of eye-space position by the z-value of the view frustum's far clip plane-Output the value to the depth bufferStep 2:  calculate world-space position from depth bufferIn application:-calculate world-space positions of the 4 corners of the view frustum's far clip plane-pass points to vertex shader as constants or as values in the vertices of the full-screen quad (the 4 corner points should be mapped to the 4 points of the quad)-pass the world-space position of the camera to the fragment shader-render the quadIn vertex shader:-retrieve the position of the the frustum corner for the current vertex-pass as "viewDirection" to the fragment shader (do not normalize!)In fragment shader:-do not normalize "viewDirection"!-read depth value from the depth buffer-world-space position of the fragment is "cameraPos + (viewDirection * depth)"I dont understand how this is working. How is it producing the position in 3d, if all you have stored in the depth map in step 1 is the z value?Are the X and Y coming from the fact that the quad stretches to the back plane of the frustum?   
Quote:Original post by maya18222Are the X and Y coming from the fact that the quad stretches to the back plane of the frustum?Exactly that's where you get both your cordinates. You first construct the position of a particular point in post projection space, by using the current texel's position (in post projection) and the depth from the rendertarget. Then you transfrom the point to world space, by multiplying with the inverse view projection matrix.I think that's how I did it, when implementing motion blur as a post process effect.   ;  I compiled a lot of what I said there into this blog entry.  It should be a lot easier to follow than the posts in that thread.  [smile]   ;  hmm, this snippet has me confusedfloat3 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;  }The projection is applied as :-1. mul(Pos, ProjMatrix)2. pos.xyz /= pos.wso to inverse that, dont you also have to reverse the order of the operations that were applied? :-1. pos.xyz *= pos.w2. mul(Pos, InvProjMatrix)yet you seem to do it as :-1. mul(Pos, InvProjMatrix)2. pos.xyz /= pos.wI know you dont have the .w at that point, so i can see why my method wouldnt work. But how is yours working?   ;  Is this idea described any where else? If anyone knows a link or book that explains the math above that would be great.   