View Full Version : Question about reconstructing World Position from depth buffer

10-09-2014, 11:53 AM

I'm trying to reconstruct the world position of my geometry in my deferred shader like this:

vec2 texcoord = vec2(gl_FragCoord.x / screenres_x, gl_FragCoord.y / screenres_y);
float d = texture(uniform_depth, texcoord).r;
vec4 pos = vec4(texcoord.xy * 2 - 1, d * 2 - 1, 1) * inverse_projectionmatrix;
pos /= pos.w; // get normalized device coordinates (ndc)

Now this seems so give me the position in view space, right? But how do I transform this position back into geometry's original world space?

Any Ideas?

Best regards

Dark Photon
10-09-2014, 07:39 PM
I'll take your word for it that that gives you EYE-SPACE (sometimes called view space or camera space); haven't checked your math. The PositonFromDepth_DarkPhoton() function listed in this post (https://www.opengl.org/discussion_boards/showthread.php/173019-View-Space-Light-Position-Moving?p=1212628&viewfull=1#post1212628) definitely does (for a perspective projection).

From there, getting back to WORLD-SPACE is simple. Since VIEWING transform is the WORLD-to-EYE space transform, invert that to get a transform that'll take you from EYE-to-WORLD space transform.

10-10-2014, 04:05 AM
I took a closer look at your function PositonFromDepth_DarkPhoton(). It seems that You do not apply a transposed inverse projection matrix. Isn't this neccessary? Instead you're using right/left/bottom/top variables. What are they?

Dark Photon
10-15-2014, 06:19 AM
I think you mean inverse projection matrix. Inverse transpose would be for transforming normals.

No it's not necessary. Think about the classic application of matrix math to linear algebra. Consider what you use the inverse matrix for. Now consider that if you know the forms of the equation (i.e. you know the equations you're solving) you can analytically solve for the solution without resorting to inverse matrix computation in terms of the input parameters for the projection. Which brings us to....

For the meaning of left/right/bottom/top, see the glFrustum() man page. Yes, this solution is specifically formulated for a perspective projection.

10-20-2014, 10:15 AM
Oh yeah, q quick look at the actual glFrustum() implementation gave me a clue. Thank m8