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?