How to solve Z-fighting

Started by
7 comments, last by HeartFlowing 16 years, 10 months ago
with two coplanar faces to display, there is a problem:z-fighting someone suggests:using z-bias like this in opengl glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1.0, 1.0); however , I found that ,it's only useful for GL_POLYGON_OFFSET_LINE, which I means I can get a good display of framework-line without z-fighting. while useless to two coplanar faces, there still exists z-fighting how can I use the functions wrongly? can someone give me a simple integrated project(or just codes) to solve the problem?
Advertisement
Sorry if these are stupid and you have already tried it, but you could try increasing the size of your depth buffer, which I realize you may not want to do. If possible you could also decrease the gap between your near clip and far clip, that will give you a higher z buffer resolution as well.
I do have tried many possible values of 'factor and units' of glPolygonOffset(), and change the near and far clip too, but haven't got any effect.
I think that maybe I didn't use these functions correctly, so I plead someone send me a simple demo. then I can find some mistakes of my project.
You need to offset exactly one of the two polygons, or they'll end up still having the same Z value.
"You need to offset exactly one of the two polygons, or they'll end up still having the same Z value"
right!!
I have misunderstood the ture meaning of glPolygonOffset();
here is the right method:
draw one thing
glPolygonOffset(1.0, 1.0);
draw another thing
glPolygonOffset(2.0, 1.0);
draw others
…………
then there may be several coplanar faces (more than two) be drawn with different offset, then no z-fighting appears.

but, here brings another problem:
If I set polygon offset, does the z-value be the same as which when polygon-offset haven't been set?If they differ, other problems may come.?

Maybe I do should get more understanding of the polygon offset. Thank for all.

now ,my prehension of polygon-offset is:
suppose:z1 is the a z-value of z-buffer after drawing a face
z2 is the calculated z-value which will be compared with z1
here is the way opengl process the offset:
if((z2+offset)<z1)
z=z2+offset;//why not z2 ?, z2 rather than (z2+offset) is the right z-value
else
z=z1;

if there are many( more than 3) coplanar face, and you sometimes you get even more face without knowing which of them will be coplanar.

now, how do you solve the coplanar-face problem?
The redbook (OpenGL Programming Guide) describes one solution;
Making Decals
[EDIT] Also, check out the stencil buffer method.

The basic idea is drawing the base(bottom layered) polygon twice; once for color buffer and the second rendering for depth buffer. By doing this, the top-layered polygon does not suffer from z-fighting.

Here is steps:
1. Draw polygon "A" to color buffer only, but disable writing depth buffer.
2. Draw polygon "B" as usual to both color and depth buffer.
3. Draw polygon "A" to depth buffer only, but disable color buffer.

// STEP 1: draw polygon A to only colour bufferglDepthMask(GL_FALSE);                              // disable writing to depth buffer, but depth-test is still activedrawA();    // STEP 2: draw polygon B as usual (to both colour and depth buffer)glDepthMask(GL_TRUE);                               // enable writing to depth bufferdrawB();    // STEP 3: Finally write depth values of polygon A to depth bufferglColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // disable writing to color bufferdrawA();    // restore colour buffer writingglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
i can't get glPolygonOffset(1.0, 1.0); to do anything either,

this seems to indicate you can only use it to set a relative bias between polygons and lines, is there no easy way to set a universal z-buffer offset
Quote:Original post by songho
The redbook (OpenGL Programming Guide) describes one solution;
Making Decals
[EDIT] Also, check out the stencil buffer method.

The basic idea is drawing the base(bottom layered) polygon twice; once for color buffer and the second rendering for depth buffer. By doing this, the top-layered polygon does not suffer from z-fighting.

Here is steps:
1. Draw polygon "A" to color buffer only, but disable writing depth buffer.
2. Draw polygon "B" as usual to both color and depth buffer.
3. Draw polygon "A" to depth buffer only, but disable color buffer.

*** Source Snippet Removed ***

the way here you suggeste supposes that you have all ready known about the hierarchy of these objects.(maybe you have used BSP or other structures to manage the objects in a scene)
how about the situation when you know nothing about their hierarchy-relation, or more than three objects interlace together?(eg.A overlap on B, B overlap on C and may be more)
Quote:
if( (z2+offset)<z1)
z=z2;//not z2+offset
else
z=z1;

why doesn't openGL implement the polygon-offset method this way? Is it wrong, or other reasons that it will slow down the pipeline or complicate the architecture?

This topic is closed to new replies.

Advertisement