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