• Advertisement
Sign in to follow this  

Zbuffer Problem...

This topic is 3560 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

Wierd problem with DX9 Zbuffer... I've set it up every way you can imagine, and it still doesn't render properly. With CullMode set to None, black streaks are all over the model. With CullMode set to CounterClockwise or Clockwise, some faces that should be drawn over are on the top. I know its a problem with the Zbuffer, but I've tried everything and I'm stumped. I'm not using D3DXMeshes, I'm using VertexBuffers to draw meshes from a 3ds-like file format, but no matter how hard I try there is always a Zbuffer problem. The wierd thing is: I've worked with DirectX 8.1 and Visual Basic 6 before, and Zbuffering worked just fine with multiple D3DXMesh8's. Now, in DX9 and VB9 using VertexBuffers to render non-convex meshes, wierd overlap issues occur. Is there any specific renderstate I need to tweak? This is my Device initialization code:
Dim pp As New PresentParameters
mHeight = height
mWidth = width
pp.Windowed = iswindowed
pp.BackBufferWidth = width
pp.BackBufferHeight = height
pp.BackBufferCount = 1
pp.SwapEffect = SwapEffect.Flip
pp.EnableAutoDepthStencil = True
pp.AutoDepthStencilFormat = DepthFormat.D16
myDevice = New Direct3D.Device(0, DeviceType.Hardware, handle, CreateFlags.SoftwareVertexProcessing, pp)
myDevice.SetTransform(TransformType.Projection, Matrix.PerspectiveFovLH(Math.PI / 4, width / height, 0, 10000))
BackColor = System.Drawing.Color.Black
myDevice.RenderState.Ambient = System.Drawing.Color.FromArgb(&H202020)
myDevice.RenderState.AlphaBlendEnable = True
myDevice.RenderState.AlphaBlendOperation = BlendOperation.Add
myDevice.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha
myDevice.RenderState.AlphaSourceBlend = Blend.SourceColor
myDevice.RenderState.BlendOperation = BlendOperation.Add
myDevice.RenderState.AlphaTestEnable = True
myDevice.RenderState.SourceBlend = Blend.SourceAlpha
myDevice.RenderState.DestinationBlend = Blend.InvSourceAlpha
myDevice.RenderState.ZBufferEnable = True
myDevice.RenderState.ZBufferFunction = Compare.LessEqual
myDevice.RenderState.ZBufferWriteEnable = True

And this is how I render the vertex buffers:
myDevice.Clear(ClearFlags.ZBuffer, 0, 1.0F, 0)
myDevice.Clear(ClearFlags.Target, BackColor, 1.0F, 0)
Dim RotMat As Matrix
RotMat = Microsoft.DirectX.Matrix.Multiply(Microsoft.DirectX.Matrix.RotationX(Rotation.X), _
Microsoft.DirectX.Matrix.Multiply(Microsoft.DirectX.Matrix.RotationY(Rotation.Y), _
viewport.GetDevice.Material = Material
viewport.GetDevice.SetTexture(0, Texture)
viewport.GetDevice.SetTransform(TransformType.World, Microsoft.DirectX.Matrix.Multiply(RotMat, Microsoft.DirectX.Matrix.Multiply(Microsoft.DirectX.Matrix.Translation(Position), Microsoft.DirectX.Matrix.Scaling(Scale))))
viewport.GetDevice.VertexFormat = CustomVertex.PositionNormalTextured.Format
viewport.GetDevice.SetStreamSource(0, myVB, 0)
viewport.GetDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, primcount)

What could be wrong? :? I'm stumped... someone please help a poor programming llama in need XD [Edited by - Super Llama on June 19, 2008 10:15:32 AM]

Share this post

Link to post
Share on other sites

myDevice.SetTransform(TransformType.Projection, Matrix.PerspectiveFovLH(Math.PI / 4, width / height, 0, 10000))

Using 0 for the near plane distance typically causes an artifact that looks like a zbuffer problem (ie like you don't have a zbuffer at all). Try using a value of 1 for the near plane, that ought to do the trick.

Unrelated to your problem, you also might want to reduce the far plane distance to a smaller number if possible, so you don't get further depth fighting from precission issues. For more information about zbuffer precision, go here.

Hope this helps [smile]

Share this post

Link to post
Share on other sites
I'm not particularly familiar with VB but I don't see anything obvious except FromArgb(&H202020). That sets the alpha to 0, doesn't it? It's the ambient so I wouldn't think it makes much difference. Should you be setting a diffuse color also? Do you have lighting enabled somewhere?

In any case, it sounds like you have too much going on to see the problem.

Suggestions for trouble shooting:
- Start extremely simple!
- Don't assume you know what the problem is until you actually know what the problem is. It may be your vertices and not the zbuffer.
- Check your material for reasonable values for diffuse and ambient.
- ensure your texture is loaded
- turn off AlphaBlendEnable
- turn off AlphaTestEnable
- turn off all rotations
- render a single triangle (i.e.,primCount=1)

Get that to work; then render a few more primitives until you can see what's going on.

EDIT: regimus probably hit it on the head! znear=0 is going to give you problems!

Share this post

Link to post
Share on other sites
I had no idea the projection matrix could cauase that! Thank you :D :D :D
I knew there was a problem with the Z Values being too high / too low, but I didn't know the projection matrix determined that. Thanks again :)

And the SetAmbient function sets it to color 20, 20, 20 in hexidecimal, not 0. And yes, I have lighting enabled elsewhere. Also, I've tried all the things you (Buckeye) listed, but the mesh doesn't have a texture applied at all, and I'm very sure it's a Zbuffer problem, because faces on the back of the mesh are being rendered in front, etc.

I'll try the ZNear=1 and see if that fixes it. Thanks for the info :)


YAAAAAAAAAAY! IT WORKED! Thanks so much, I've been struggling with this for a week XD


Share this post

Link to post
Share on other sites
And the SetAmbient function sets it to color 20, 20, 20 in hexidecimal

I was talking specifically about the alpha value of the color, the A part of FromArgb. The color will be set to &H202020, or &H00202020, making the alpha value of the color 0 (with r=&H20, g=&H20 and b=&H20). [smile]

Share this post

Link to post
Share on other sites
Oh, I see what you mean. I think if alpha isn't specified it automatically assumes FF, but I'm not sure... also that value is for the overall ambience of the device. If that value is set to 0, everything will be black regardless of its color or any lights.

Also, I copied that line out of a tutorial =D

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement