# OpenGL Reconstructing Position from Depth

I know this has been asked a lot of time before but none of those answers fixed my problem. I try to implement deferred shading and to do so I need to reconstruct the world space position from the depth. (Having a position texture in the gbuffer would be a waste of data)

The calculation works fine for rotationing the camera (-> moving the mouse) but as soon as I move the camera (-> pressing wasd) they get weird.

GLSL:

vec4 screenSpacePosition = vec4(pass_Texture * 2.0 - 1.0, texture(gbuffer_texture[2],     pass_Texture).r, 1);
vec4 worldSpacePosition = invertedViewProjection  * screenSpacePosition;
vec3 finalPosition = worldSpacePosition.xyz / worldSpacePosition.w;


gbuffer_texture[2] is the depth attachment of OpenGL. invertedViewProjection is the matrix I used to render the scene inverted on the CPU.

• What's your question/problem? – Fault Mar 9 '14 at 23:07
• @Fault Facepalm@me. I would help to descripe the problem. – user205695 Mar 9 '14 at 23:10
• You need to use a linear depth value for your computation (mapped to the range [-1, 1]). Check out the answer over here for how to get the linear value: stackoverflow.com/questions/19750351/… – Fault Mar 9 '14 at 23:47

The result sampled from gbuffer_texture[2] will be in the [0, 1] range, but in OpenGL, NDC space ranges from -1 to 1 along all three axes. (This is different from D3D, where the NDC space ranges from 0 to 1 along the z axis.)
So, you need to multiply the depth result by 2 and subtract 1 to convert the range to [-1, 1], just as you're doing already for the xy components of screenSpacePosition. Then you'll have the proper NDC position, and you can apply the inverse view-projection matrix as you're doing.