问题
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
- 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.
- Can anyone explain to me how can we use Projection Matrix to fix the problem ? And how does it work?
- 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