Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Z-fighting solutions in depth test in OpenGL - how do they work?

Description

I've had major problems with Z-Fighting in OpenGL and I've spent quite some time finding solutions for this problem. Some of the ones I've found and I understand and didn't like:

  • Moving polygons away from each other ( like glPolygonOffset in OpenGL )
  • Dividing the scene according to Z coordinate and drawing parts of the scene with separate clean z-buffers.

The ones I don't understand:

  • Using Projection Matrix https://software.intel.com/en-us/articles/alternatives-to-using-z-bias-to-fix-z-fighting-issues
  • Using "Logarithmic Depth Buffers" http://outerra.blogspot.com/2012/11/maximizing-depth-buffer-range-and.html

I've implemented the second one in my program by just puting this into the vertex shader of a ball (it z-fought with the ground) :

float C = 1.0; 
float far = 2000.0; 
   gl_Position = u_projView * a_position;      
gl_Position.z = 2.0*log(gl_Position.w*C + 1.0)/log(far*C + 1.0) - 1.0;
gl_Position.z *= gl_Position.w;

and it worked!

Actual Questions

  1. Can anyone explain me how did changing the Z coordinate of the vertex in the vertex shader solved the problem WITHOUT moving the vertex visibly to me ? (the scene looks the same to the human eye). How did it change the distribution of the z-depth values ? I'm guessing i'm missing some knowledge about rendering pipeline.
  2. Can anyone explain to me how can we use Projection Matrix to fix the problem ? And how does it work?
  3. Are there any other similarly effective ways to fixing z-fighting problem?

Thanks!

like image 350
michal.ciurus Avatar asked Jun 10 '14 13:06

michal.ciurus


1 Answers

Maybe there's a problem with depth buffer precision? When using 16-bit buffer, z-fighting is likely to happen. You can check it using:

glGetIntegerv( GL_DEPTH_BITS, &depthBits);
like image 179
jr_ Avatar answered Sep 23 '22 13:09

jr_