2
\$\begingroup\$

I've searched about this topic for awhile and i couldn't find it on google. I've come across several ways to avoid z-fighting which are linear z-buffer, logarithmic z-buffer and reversed z-buffer. I decided to leave reversed z-buffer for now as i'm not planning to use OpenGL 4.5. The thing is i don't know the difference between linear and logarithmic z-buffer.

By "linear" i mean this:

float fz = gl_Position.z * gl_Position.w;
gl_Position.z = (fz - near) * 2.0 / (far - near) - 1.0;

And by "logarithmic" i mean this:

float w = gl_Position[3];
float C = 0.001;
gl_Position.z = (2 * log(C * w + 1) / log(C * far + 1) - 1) * w;

If i understand those codes correctly, the linear try to map all of the z value evenly. So does the logarithmic one. Sure both of them are totally different but i have tried both and see no or if any minimal differences, both eliminate the z-fighting. So there must be something i missed or don't understand.

edit: As Jimmy said, logarithmic is better than linear. Please add why logarithmic is better than linear z-buffer as i want to know why it is better and not just which one is better.

\$\endgroup\$
  • \$\begingroup\$ naive Z < linear Z < log Z in terms of consistency of number of depth bits available at all levels. If linear Z is enough to eliminate-fighting for you, it's probably faster to use that than to use log Z. \$\endgroup\$ – Jimmy Nov 1 '17 at 18:13
-2
\$\begingroup\$

A logaritmic z-buffer provides more accuracy for objects near the camera in exchange for a drop in precision for far away objects. In comparison a linear z-buffer provides uniform precision for all distances and it's a bit faster.

You should try to find which algorithm fits your game best. e.g If you are developing a game with closeup elements like a FPS a logaritmic z-buffer may be the best for you.

\$\endgroup\$
  • 1
    \$\begingroup\$ I'm pretty sure that' not a common case where one would want to use logarithmic z-buffer. \$\endgroup\$ – Greffin28 Nov 2 '17 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.