z-fighting

Started by
9 comments, last by MasterWorks 19 years, 2 months ago
I don't know exactly what z-fighting is but I am fighting something that is not nice. Lets take an easy example. I load two solid cubes from xfile's. The first cube is half the size of the first. I place the small cube inside the large cube. To my surprise some of the color of the small cube shows up in bits of jagged lines in face of the large cube. Is this z-fighting and how do you get rid of it? Thanks
Advertisement
It could very well be Z-fighting. I presume you have a depth buffer configured and enabled for your device?

To solve it is a bit less precise.. some ideas:
- Use a higher resolution depth buffer (D3DFMT_D24** or D3DFMT_D32).
- Alter the projection matrix. Tweaking the near/far plane values can have positive results on the quality/accuracy of the depth buffering
- Use the D3DRS_SLOPEDSCALEDBIAS and/or D3DRS_DEPTHBIAS when rendering one of the other cubes
- Changing the scale of your cubes so that any floating point precision errors are ruled out.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

My bet is on a really small near plane in the projection matrix.
Clicky for details and an interactive &#106avascript thingy</a>
first off, why are you loading cubes from xfiles? it takes about 10 minutes to write a class for cubes. anyhow, "z fighting" is non-ideal depth ordering. this used to be solved by creative ordering drawing algorighms, but now the depth of drawn objects is stored in a 2d array where each element is the closest point yet (to the view) that wants to draw to that pixel.

// enable depth sorting.
pDevice->SetRenderState ( D3DRS_ZENABLE, TRUE );

when you create your d3d device, u set a depth stencil format too, just make sure that is some normal constant like

D3DFMT_D24 or D3DFMT_D32

i doubt that precision is your problem.
Quote:Original post by jollyjeffers
It could very well be Z-fighting. I presume you have a depth buffer configured and enabled for your device?

To solve it is a bit less precise.. some ideas:
- Use a higher resolution depth buffer (D3DFMT_D24** or D3DFMT_D32).
- Alter the projection matrix. Tweaking the near/far plane values can have positive results on the quality/accuracy of the depth buffering
- Use the D3DRS_SLOPEDSCALEDBIAS and/or D3DRS_DEPTHBIAS when rendering one of the other cubes
- Changing the scale of your cubes so that any floating point precision errors are ruled out.

hth
Jack


I guess that the slopescaledbias/ depthbias gives priority to that object
so anything close and that object will get rendered. IS there in any
good documention on these values. HOw do you set the resolution
of z-buffer? Does resolution of z-buffer have anything to do with resolution
of the surfaces you are using?
Quote:Original post by Namethatnobodyelsetook
My bet is on a really small near plane in the projection matrix.
Clicky for details and an interactive &#106avascript thingy</a><!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>Good article but it is a little deep for me (new to 3d). But the <br>near values do make sense because I have my view point at a distance<br>so I am probably losing accuracy. It seems like api should be<br>smart enough to set the near and far planes according to what you<br>are rendering. Any how do you happen to know how to set the near and<br>far planes in directx?
Quote:Original post by programmer_tom
first off, why are you loading cubes from xfiles? it takes about 10 minutes to write a class for cubes. anyhow, "z fighting" is non-ideal depth ordering. this used to be solved by creative ordering drawing algorighms, but now the depth of drawn objects is stored in a 2d array where each element is the closest point yet (to the view) that wants to draw to that pixel.

// enable depth sorting.
pDevice->SetRenderState ( D3DRS_ZENABLE, TRUE );

when you create your d3d device, u set a depth stencil format too, just make sure that is some normal constant like

D3DFMT_D24 or D3DFMT_D32

i doubt that precision is your problem.


I am not just loading cubes but I used as a simple test in my program.
Does the stencil relate to accuracy of the z-buffer (number of bits).
if not What does stencil have to do with z-buffer.
When you create your projection matrix (D3DXMatrixPerspectiveFov[LH|RH]) you set the near and far planes.

Setting near very low (like, below 1) causes all sorts of grief, and precision will definately become a problem.
Quote:Original post by Namethatnobodyelsetook
When you create your projection matrix (D3DXMatrixPerspectiveFov[LH|RH]) you set the near and far planes.

Setting near very low (like, below 1) causes all sorts of grief, and precision will definately become a problem.


Hey some good info so I take it that has the scene changes or whatever
we change the projection matrix accordingly.

Quote:I guess that the slopescaledbias/ depthbias gives priority to that object
so anything close and that object will get rendered. IS there in any
good documention on these values.

This page gives some descriptions:
Quote:D3DRS_DEPTHBIAS
A floating-point value that is used for comparison of depth values. See Depth Bias. The default value is 0.

Quote:D3DRS_SLOPESCALEDEPTHBIAS
Used to determine how much bias can be applied to co-planar primitives to reduce z-fighting. The default value is 0.

bias = (max * D3DRS_SLOPESCALEDEPTHBIAS) + D3DRS_DEPTHBIAS.

where max is the maximum depth slope of the triangle being rendered.



Quote:HOw do you set the resolution of z-buffer?

It's in the present parameters that you configure before you create your IDirect3DDevice9. Have a look at D3DPRESENT_PARAMETERS Structure, in particular the parameter AutoDepthStencilFormat. Look up the corresponding depth formats in D3DFORMAT: D3DFMT_D16_LOCKABLE, D3DFMT_D32, D3DFMT_D15S1, D3DFMT_D24S8, D3DFMT_D24X8, D3DFMT_D24X4S4, D3DFMT_D32F_LOCKABLE, D3DFMT_D24FS8 and D3DFMT_D16

The resolution of the stencil buffer is not important to the depth buffer, so unless you actually use it you can probably ignore the 'X' and 'S' parts of the above listed formats.

Right, I need to get another beer - hope the above is of some use to you [smile].

Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement