michal.ciurus
michal.ciurus

Reputation: 3664

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:

The ones I don't understand:

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!

Upvotes: 13

Views: 5788

Answers (1)

jr_
jr_

Reputation: 31

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

Upvotes: 2

Related Questions