1

I use this function to linearize the depth value in my shaders:

float linearize_depth(float d, float zNear, float zFar)
{
    return zNear * zFar / (zFar + d * (zNear - zFar));
}

What function would do the exact inverse transformation?

HenriV
  • 23
  • 4

1 Answers1

0

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(Cz + 1) / log(CFar + 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_valuelog(Cfar+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.

Credit goes to camenomizoratojoakizunewake. Original answer can be found here.