Sign in to follow this  
Proudest

z-buffer problem? (pics inside)

Recommended Posts

Proudest    100
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 this post


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


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


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Proudest
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?



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 this post


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


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

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