# z-buffer problem? (pics inside)

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

## Recommended Posts

I'm having a problem when trying to draw 2 cubes. In both pictures, the crate is 1 unit closer to the screen than the brick cube. crate.Vector3 = new Vector3(0,0,1); brick.Vector3 = new Vector3(0,0,0); When I draw the crate first, the bricks are still showing even though they're behind the crate. When I draw the bricks first, the crate is correctly drawn over them. The draw order is accounted for when drawing, even though I thought the z buffer would take care of this. What am I missing?

##### Share on other sites
Do you enable/setup/clear the depth buffer properly? Post the code for that.

##### Share on other sites
I found the problem. I had the znear plane set to 0.

Before:
device.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded((float)Math.PI / 3.0f, aspect, 0.0f, 1000.0f);

After:
device.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded((float)Math.PI / 3.0f, aspect, 0.1f, 1000.0f);

But now there's a new problem. When both cubes occupy the same space, when drawn, they "interlace". It doesn't happen all the time either, it seems to be based on viewing angle or the specific side of the cube (top and left faces show it, right and bottom don't).

How do I fix this?

##### Share on other sites
The problem is because they are both on the same plane so theres really no way to tell which is to be occluding the other.

I think you can solve or minimize this by playing with the values of depthbias.

##### Share on other sites
Quote:
 Original post by ProudestI found the problem. I had the znear plane set to 0.Before:device.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded((float)Math.PI / 3.0f, aspect, 0.0f, 1000.0f);After:device.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded((float)Math.PI / 3.0f, aspect, 0.1f, 1000.0f);But now there's a new problem. When both cubes occupy the same space, when drawn, they "interlace". It doesn't happen all the time either, it seems to be based on viewing angle or the specific side of the cube (top and left faces show it, right and bottom don't).How do I fix this?

Z fighting .....

The one you want to show 'ontop' should have a position offset nearer to the viewer.

The range of Z values (far and near planes) on the frustum needs to be set small enough that the value you seperate them by works at enough view angles.
Using 32 bit versus 16 bit Z buffer can give you a finer resolution to prevent Z fighting.

If all else fails get rid of the overlap (more complex mesh...)

##### Share on other sites
I tried setting the depthbias to 1 and it fixes the "interlacing" problem. The only problem is, the original problem is back.

Here's the relevant code:

//These settings give me the "interlacing" problem but fix the drawing order problem.PresentParameters pp = new PresentParameters();pp.AutoDepthStencilFormat = DepthFormat.D16;pp.EnableAutoDepthStencil = true;//device.RenderState.DepthBias = 1;device.RenderState.Lighting = false;device.RenderState.ZBufferEnable = true;device.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded((float)Math.PI / 3.0f, aspect, 0.1f, 1000.0f);device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);

When I un-comment the depthbias line, it fixes the interlacing, but not the drawing order.

I also tried changing the z-buffer to 32bit, but it's not available. So I set it to "DepthFormat.D24X8" and the cubes don't still interlace, but not as frequently.

##### Share on other sites
Co-planar geometry will always z-fight, no matter what the depth buffer precision, unless you use z-bias. (I don't know why it's acting like there's no depth buffering when you use them together) However, geometry that's not coplanar but close will be affected by the precision. Also note that to get the best precision you want to choose the most bits in the buffer as well as set your near plane as far from the camera and your far plane as close to the camera as you are willing to. The range you currently have, .1 to 1000 is pretty reasonable with a 24 bit depth buffer though.