I am attempting to reconstruct the world position in the fragment shader from a depth texture. I pass in the 8 frustum points in world space and interpolate them across fragments and then interpolate from near to far by the depth:
highp float depth = (2.0 * CameraPlanes.x) / (CameraPlanes.y + CameraPlanes.x - texture( depthTexture, textureCoord ).x * (CameraPlanes.y - CameraPlanes.x)); // Reconstruct the world position from the linear depth highp vec3 world = mix( nearWorldPos, farWorldPos, depth );
CameraPlanes.x is the near plane CameraPlanes.y is the far.
Assuming that my frustum positions are correct, and my depth looks correct, why is my world position wrong?
(My depth texture is of format GL_DEPTH_COMPONENT32F if that matters)
Screenshot of world position http://imgur.com/sSlHd
So you can see it looks nearly correct. However as the camera moves, the colours (positions) change, which they shouldnt.
I can get this to work, if I do the following:
Write this into the depth attachment in the previous pass:
gl_FragDepth = gl_FragCoord.z / gl_FragCoord.w / CameraPlanes.y;
and then read the depth texture like so:
depth = texture( depthTexture, textureCoord ).x
However this will kill the hardware z buffer optimizations.