Could someone explain why my world reconstructed from depth position is incorrect?

Posted by yuumei on Game Development See other posts from Game Development or by yuumei
Published on 2012-05-22T11:01:28Z Indexed on 2013/07/02 11:16 UTC
Read the original article Hit count: 237

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)

Thanks! :D

Update:

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.

© Game Development or respective owner

Related posts about opengl-es2

Related posts about deferred-rendering