How to linearize the logarithmic depth buffer?
visualization of the linear depth buffer in the fragment shader
float n = 1.0; // camera z near float f = 27000000.0; // camera z far float z = texture( DepthTex, TexCoord ).x; float d = (2.0 * n) / (f + n - z * (f - n)); FragColor=vec4(d,d,d,1);
spherical vertex shader
vec4 ClipCoords(vec3 position,mat4 matrix) { vec4 clip = matrix * vec4(position,1.0f); clip.z =((2.0f * log(1.0f * clip.z + 1.0f) / log(1.0f * 27000000.0f + 1.0f)) - 1.0f) * clip.w; return clip; } gl_Position = ClipCoords(position,matrix);
The left side shows the linearization of the logarithmic depth of the buffer, or rather, its flaws, and the right side shows the linearization without log only gl_Position = matrix * vec4(position,1.0f); 
source share