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

折月煮酒 提交于 2020-01-12 04:40:33

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/24142068/z-fighting-solutions-in-depth-test-in-opengl-how-do-they-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!