Position Reconstruction

Cameras, Math | Monday 7 December 2009 5:34 pm

There are many occasions when the fragment position in world space needs to be reconstructed from a texture holding the scene depth (depth texture). One example of use is in deferred rendering when trying to decrease memory usage by not saving the position but instead only the depth. This will result in one channel of data, instead of three channels needed when saving the whole position.

 

Viewspace scene depth

There are different ways to save the depth. The most popular are view space depth and screen space depth. Saving depth in view space instead of screen space gives two advantages. It’s faster, and it gives better precision because it’s linear in view space.

This is how screen space depth can be rendered in HLSL:

struct VS_OUTPUT
{
   float4 Pos: POSITION;
   float4 posInProjectedSpace: TEXCOORD0;
};
 
// vertex shader
VS_OUTPUT vs_main( float4 Pos: POSITION )
{
   VS_OUTPUT Out = (VS_OUTPUT) 0;
   Out.Pos = mul(Pos,matWorldViewProjection);
   Out.posInProjectedSpace = Out.Pos;
   return Out;
}
 
// pixel shader
float4 ps_main( VS_OUTPUT Input ) : COLOR
{
   float depth = Input.posInProjectedSpace.z / Input.posInProjectedSpace.w;
   return depth;
}

The HLSL pixel shader below shows how the position can be reconstructed from the depth map stored with the code above. Although this is one of the slowest ways of doing position reconstruction since it requires a matrix multiplication.

float4 ps_main(float2 vPos : VPOS;) : COLOR0
{
   float depth = tex2D(depthTexture,vPos*fInverseViewportDimensions + fInverseViewportDimensions*0.5).r;
 
   // scale it to -1..1 (screen coordinates)
   float2 projectedXY = vPos*fInverseViewportDimensions*2-1;
   projectedXY.y = -projectedXY.y;
 
   // create the position in screen space
   float4 pos = float4(projectedXY,depth,1);
 
   // transform position into world space by multiplication with the inverse view projection matrix
   pos = mul(pos,matViewProjectionInverse); 
 
   // make it homogeneous
   pos /= pos.w; // result will be (x,y,z,1) in world space
 
   return pos; // for now, just render it out
}

To reconstruct depth from view space, a ray from the camera position to the frustum far plane is needed. For a full screen quad, this ray can be precalculated for the four corners and passed to the shader. This is how the computer game Crysis did it [1] . But for arbitrary geometry, as needed in deferred rendering, the ray must be calculated in the shaders [2] .

[1] “Finding next gen: CryEngine 2″
http://ati.amd.com/developer/gdc/2007/mittring-finding_nextgen_cryengine2(siggraph07).pdf

[2] “Reconstructing Position From Depth, Continued”
http://mynameismjp.wordpress.com/2009/05/05/reconstructing-position-from-depth-continued/

Please share:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Current
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • StumbleUpon
  • Twitter
  • Reddit
  • Technorati
  • Yahoo! Bookmarks

10 Comments »

  1. Pingback by Real-Time Rendering · 7 things for December 22 — December 22, 2009 @ 4:03 pm

    [...] correspond to actual world distances along the camera’s view direction. This article and this one (oh, and this is related) give ways to get back to this linear space. Why get the view-space depth? [...]

  2. Comment by Detox Diets : — October 31, 2010 @ 9:20 pm

    i like computer games that are first person shooting and strategy games .

  3. Comment by Glass Shelving %0B — January 25, 2011 @ 5:35 pm

    ::* I am very thankful to this topic because it really gives useful information ,*.

  4. Comment by Outdoor Teak Furniture — May 22, 2011 @ 10:27 pm

    very nice submit, i certainly love this web site, keep on it

  5. Comment by Zrlqnipp — November 7, 2011 @ 4:13 pm

    I need to charge up my phone sexy petite lolita girls ftr

  6. Comment by Qlpndyoe — November 9, 2011 @ 9:43 pm

    I’ve got a full-time job Preteen Nude Toplist
    dknrs

  7. Comment by Qsesizca — November 16, 2011 @ 11:15 pm

    How much does the job pay? toplist grils tgupz adolescent nudes russian toplists 63714 fresh nudes toplist 336022 child pron pics toplist 8( lolta toplist 402915 gay teens toplist lka girls gallery toplist tsf toplist preeteen 8-O webcam toplist 97266 nn girl toplist myg

  8. Comment by Iddvggoa — December 8, 2011 @ 6:05 pm

    Who do you work for? Cp Lolitas
    :-) )

  9. Comment by Tuhggldj — December 8, 2011 @ 6:07 pm

    Do you like it here? Preteen Nude
    vusgra

  10. Comment by Qnygvmsr — December 8, 2011 @ 6:25 pm

    I’m from England youngleafs
    goqku

RSS feed for comments on this post. TrackBack URI

Leave a comment