在AR中有时候会用到Eyez转换到buffer z或者buffer z转换成Eyez的问题,把计算方法记录一下。
n = near
f = far
z = depth buffer Z-value (opengl:[-1..1], dx:[0..1])
EZ = eye Z value
LZ = depth buffer Z-value remapped to a linear [0..1] range (near plane to far plane)
LZ2 = depth buffer Z-value remapped to a linear [0..1] range (eye to far plane)
DX:
EZ = (n * f) / (f - z * (f - n))
LZ = (eyeZ - n) / (f - n) = z / (f - z * (f - n))
LZ2 = eyeZ / f = n / (f - z * (f - n))
GL:
EZ = (2 * n * f) / (f + n - z * (f - n))
LZ = (eyeZ - n) / (f - n) = n * (z + 1.0) / (f + n - z * (f - n))
LZ2 = eyeZ / f = (2 * n) / (f + n - z * (f - n))
The distance between the camera and the current pixel (the z axis value) is obtained by the following relation :
float z = gl_FragCoord.z / gl_FragCoord.w;
参考文献: