worldspace vertex position from depth buffer

I am trying to implement deferred shading and have a problem with calculating vertex position(world/eye space) from depth buffer.

from the function describe in the document “Deferred rendering in leadwerk engine”



vec3 screencoord; screencoord = vec3(((gl_FragCoord.x/buffersize.x)-0.5) * 2.0,((-gl_FragCoord.y/buffersize.y)+0.5) * 2.0 / (buffersize.x/buffersize.y),DepthToZPosition( depth )); screencoord.x *= screencoord.z; screencoord.y *= -screencoord.z;

float DepthToZPosition(in float depth) { return camerarange.x / (camerarange.y - depth * (camerarange.y - camerarange.x)) * camerarange.y; }


the result of the function is vertex position in screen space.

Since i calculate lighting in world space . Is it possible to tranform the result to world space ?

Looks a little fishy for computing clip space, but let’s just assume it’s correct.

Since i calculate lighting in world space . Is it possible to tranform the result to world space ?

Given the right transform matrix, why not? To get from clip space back to world you of course need to multiply by inverse projection and inverse viewing in that order. You can pre-compute that product and pass into your shader

But I hope your world is small or you’ve only got directional light sources, or it’s gonna get ugly (float precision issues).