对数深度缓冲线性化

use*_*940 3 opengl zbuffer

如何线性化对数深度缓冲区?

片段着色器中线性深度缓冲区的可视化

  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);
Run Code Online (Sandbox Code Playgroud)

球体顶点着色器

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);
Run Code Online (Sandbox Code Playgroud)

左侧显示了对数深度缓冲线性或者更确切地说,它的缺乏,而右边显示linarization不log只是 gl_Position = matrix * vec4(position,1.0f); 在此输入图像描述

cam*_*ake 6

使用对数深度缓冲区,场景(相机空间)深度到最终在深度缓冲区(0..1)中结束的值的映射是:

depth_value = log(C*z + 1) / log(C*Far + 1)
Run Code Online (Sandbox Code Playgroud)

其中z是进入场景的正深度,否则可以在投影后从剪辑空间中的w组件获得(在您的代码中可以使用..log(clip.w + 1.0)..).

要在片段着色器中检索相机空间深度,需要反转等式:

z = (exp(depth_value*log(C*far+1)) - 1)/C
Run Code Online (Sandbox Code Playgroud)

或者等价的

z = (pow(C*far+1,depth_value)-1)/C
Run Code Online (Sandbox Code Playgroud)

要获得从0..far到0..1的线性映射,只需将其除以far值即可.