Sign in to follow this  
HeartFlowing

OpenGL How to solve Z-fighting

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
You need to offset exactly one of the two polygons, or they'll end up still having the same Z value.

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 buffer
glDepthMask(GL_FALSE); // disable writing to depth buffer, but depth-test is still active
drawA();

// STEP 2: draw polygon B as usual (to both colour and depth buffer)
glDepthMask(GL_TRUE); // enable writing to depth buffer
drawB();

// STEP 3: Finally write depth values of polygon A to depth buffer
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // disable writing to color buffer
drawA();

// restore colour buffer writing
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this