[SOLVED] How can I create world-space depth fog in my game?

So… add a projectionMatrixInverse to your shader, etc:

Matrix4f mat = cam.getProjectionMatrix();
mat = mat.invert();
material.setMatrix4("ProjMatInv", mat);

Inside shader, compute the distance to camera:

float depthN = getDepth(m_DepthTexture,texCoord).r;
vec4 clip = vec4(texCoord*2.0-1.0, depthN*2.0-1.0, 1.0);
vec4 view = m_ProjMatInv*clip;	
float dist = length(view.xyz/view.w);
2 Likes