I\'m trying to reconstruct 3D world coordinates from depth values in my deferred renderer, but I\'m having a heck of a time. Most of the examples I find online assume a standard
I only needed to make a tiny fix. The line:
clipSpaceLocation.z = texture(depthSampler, texcoord).r;
should read:
clipSpaceLocation.z = texture(depthSampler, texcoord).r * 2.0f - 1.0f;
The way I understand it, projection matrices are designed so they map the near and far planes to [-1,1], not [0,1] like I had always assumed. OpenGL then normalizes them to the range [0,1] (a.k.a. "Window Space"), so I needed to perform the inverse of that normalization.
This is all assuming glDepthRange(0, 1), which it is by default, and there's little reason to change it.