Logarithmic depth buffer linearization

你。 提交于 2019-12-07 15:04:25

问题


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

sphere 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 part shows Logarithmic depth buffer linearization or rather its lacks, while right shows linarization without log just gl_Position = matrix * vec4(position,1.0f);


回答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)

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

or equivalently

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

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



来源:https://stackoverflow.com/questions/18182139/logarithmic-depth-buffer-linearization

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!