Horde3D
http://horde3d.org/forums/

Some trouble to reconstruct World Position from Depth
http://horde3d.org/forums/viewtopic.php?f=2&t=1664
Page 1 of 1

Author:  ArthurD [ 27.04.2012, 20:01 ]
Post subject:  Some trouble to reconstruct World Position from Depth

Hello everyone,

I am asking here for help as I have been working on a very simple way to reconstruct the World Position from the Depth buffer inside the lighting pass of the deferred renderer, and I can't seem to find what is wrong.

Here is the code, this is the main function from the FS_LIGHTING shader to which I have simply added uniform mat4 viewProjMatInv; to get the Inverse View Projection Matrix.
I have also bound the depth buffer in the pipeline through <BindBuffer sampler="depthBuf" sourceRT="GBUFFER" bufIndex="32" />.

Code:
void main( void )
{
   vec2 fragCoord = (vpos.xy / vpos.w) * 0.5 + 0.5;
   
   if( getMatID( fragCoord ) == 1.0 )   // Standard phong material
   {
      // World Position taken from the GBuffer for comparison
      vec3 posR = getPos( fragCoord ) + viewerPos;

      float z = texture2D( depthBuf, fragCoord ).r;
      
      // Get x/w and y/w from the viewport position
      float x = fragCoord.x * 2 - 1;
      float y = fragCoord.y * 2 - 1;
      vec4 vProjectedPos = vec4(x, y, z, 1.0f);
      
      // Transform by the inverse projection matrix
      vec4 vPositionVS = mul(vProjectedPos, viewProjMatInv); 

      // Divide by w to get the view-space position and visualize the World Position
      gl_FragColor.rgb = vPositionVS.xyz / vPositionVS.w;
   }
   else discard;
}


So I build the position of the pixel in Projected Space [-1;1]*[-1;1]*[-1;1] and through the matrix multiplication, I should go back to the World Position.

Even though every step seems to do what I expect, the Transform does not give me the correct World Position, any comments on something I do wrong or a false assumption is welcome!

Thank you

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/