Sign in to follow this  

z-fighting

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
My bet is on a really small near plane in the projection matrix.
Clicky for details and an interactive javascript thingy


Good article but it is a little deep for me (new to 3d). But the
near values do make sense because I have my view point at a distance
so I am probably losing accuracy. It seems like api should be
smart enough to set the near and far planes according to what you
are rendering. Any how do you happen to know how to set the near and
far planes in directx?

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
The near and far planes are an optimization that YOU can make, it would be (impossible/difficult) for D3D to "know" what's important to you and make these decisions for you. The far clip plane can save you from rendering geometry that's not visible/relevent/etc. The projection matrix also lets you determine the FOV of the scene, ie how wide angle the camera lens is. (Can also let you 'zoom' the camera with a low FOV.)

For some simple cubes any reasonable Z values should suffice...

Share this post


Link to post
Share on other sites

This topic is 4688 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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