I'm using this solution to render using a reversed Z buffer.

This looks fine and completely fixes all my z-fighting, but it breaks what I use in shader to derive the world position from the depth for various purposes such as deferred lighting and fog (it has worked for a regular projection matrix):

vec4 screenSpacePosition = vec4(texcoord * 2.0 - 1.0, depth * 2.0 - 1.0, 1);
vec4 worldSpacePosition = invProjView  * screenSpacePosition;
vec3 finalPosition = worldSpacePosition.xyz / worldSpacePosition.w;
return finalPosition;

I've thought that this was due to this using a single view projection matrix, so I've also tried this:

vec4 clipSpacePosition = vec4(texcoord * 2.0 - 1.0, depth * 2.0 - 1.0, 1.0);
vec4 viewSpacePosition = invProj * clipSpacePosition;
viewSpacePosition /= viewSpacePosition.w;
vec4 worldSpacePosition = invView * viewSpacePosition;
return worldSpacePosition.xyz;

That must be suboptimal performance wise, but it doesn't work either anyway. Any idea what I'm missing?

  • \$\begingroup\$ Sometimes z buffers can be scaled to give higher resolution for closer objects, have you checked for this? \$\endgroup\$ – Jay Apr 27 '20 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.