对数深度缓冲区线性化 [英] Logarithmic depth buffer linearization

查看:350
本文介绍了对数深度缓冲区线性化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to linearize logarithmic depth buffer?

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

visualization of linear depth buffer in 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);

球形顶点着色器

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);

左侧显示对数深度缓冲区线性化或缺少线性化,而右侧显示没有loggl_Position = matrix * vec4(position,1.0f);的线性化

The left part shows Logarithmic depth buffer linearization or rather its lacks, while right shows linarization without log just gl_Position = matrix * vec4(position,1.0f);

推荐答案

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

With logarithmic depth buffer, the mapping of scene (camera space) depth to values that ultimately end up in the depth buffer (0..1) is:

depth_value = log(C*z + 1) / log(C*Far + 1)

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

where z is the positive depth into the scene, otherwise obtainable from the w component in clip space after the projection (in your code you can use ..log(clip.w + 1.0)..).

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

To retrieve the camera space depth in a fragment shader, the equation needs to be inverted:

z = (exp(depth_value*log(C*far+1)) - 1)/C

或等效地

z = (pow(C*far+1,depth_value)-1)/C

要将线性映射从0..far转换为0..1,只需将其除以far值即可.

To get a linear mapping from 0..far into a 0..1, just divide it by the far value.

这篇关于对数深度缓冲区线性化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆