Jump to content
  • Advertisement
Sign in to follow this  

[SlimDX] Problem - depth buffer?

This topic is 2593 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'm using SlimDX to create a basic AutoCAD viewer, and have run into an interesting problem. I'm not really sure where to start in terms of trying to fix it as everything looks fine to me from the DirectX tutorials I've been following, etc.

Basically, it looks like the Z buffer is getting screwed up, or there is a similar depth related problem. Further-z polygons seem to be overlapping closer ones. Attached are images to illustrate the problem (gray coloured one is my application, green is Autodesk DWG TrueView).

Feel free to suggest it may be a triangle culling problem - AutoCAD polys go both ways so I turn triangle culling off, but from my understanding this shouldn't affect z culling?

My initialisation routine looks like this:

pparams = new PresentParameters();
pparams.SwapEffect = SwapEffect.Flip;
pparams.BackBufferCount = 2;
pparams.BackBufferWidth = viewPanel.Width;
pparams.BackBufferHeight = viewPanel.Height;
pparams.EnableAutoDepthStencil = true;
pparams.AutoDepthStencilFormat = Format.D16;

direct3D = new Direct3D();
caps = direct3D.GetDeviceCaps(0, DeviceType.Hardware);

d3d = new Device(direct3D, 0, DeviceType.Hardware, viewPanel.Handle, CreateFlags.HardwareVertexProcessing | CreateFlags.FpuPreserve, pparams);

SlimDX.Direct3D9.Material material = new SlimDX.Direct3D9.Material();
d3d.SetRenderState(RenderState.Ambient, Color.White.ToArgb());
d3d.SetRenderState(RenderState.AmbientMaterialSource, ColorSource.Material);
d3d.SetRenderState(RenderState.DiffuseMaterialSource, ColorSource.Material);
material.Ambient = new Color4(1, 1, 1, 1);
material.Diffuse = new Color4(1, 1, 1, 1);
d3d.Material = material;
d3d.SetRenderState(RenderState.SourceBlend, SlimDX.Direct3D9.Blend.SourceAlpha);
d3d.SetRenderState(RenderState.DestinationBlend, SlimDX.Direct3D9.Blend.InverseSourceAlpha);
d3d.SetRenderState(RenderState.AlphaBlendEnable, true);
d3d.SetSamplerState(0, SamplerState.MipFilter, TextureFilter.Point);
d3d.SetRenderState(RenderState.CullMode, Cull.None);

SlimDX.Direct3D9.Light light = new SlimDX.Direct3D9.Light();
light.Diffuse = new Color4(1.0f, 1.0f, 1.0f, 1.0f);
light.Type = LightType.Directional;
light.Direction = new Vector3(1, 1, 1);
light.Position = new Vector3(-1, -1, -1);
light.Range = 10000.0f;
d3d.SetLight(0, light);

My render method starts like this:

d3d.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
d3d.SetTransform(TransformState.World, SlimDX.Matrix.Identity);
d3d.SetTransform(TransformState.View, cam.GetTransformation());
cam.SetLens(45, (float)viewPanel.Width / (float)viewPanel.Height);
d3d.SetTransform(TransformState.Projection, SlimDX.Matrix.PerspectiveFovLH(cam.GetFOV(), cam.GetAspect(), 0.0f, 1000.0f));

The entities themselves are quads consisting of four points (sometimes the 3rd can equal the 4th to make a triangle; that's AutoCAD's doing, not mine). I render them as triangle fans with the following initialisation:

struct normvert
public Vector3 pos;
public Vector3 normal;
public int col;

public PolylinePolygon(normvert[] verts, Device d3d)
vbuf = new VertexBuffer(d3d, (verts.Length + 1) * 4 * 7 , Usage.WriteOnly, VertexFormat.None, Pool.Managed);
DataStream ds = vbuf.Lock(0, 0, LockFlags.None);

VertexElement[] velems = new VertexElement[] {
new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
new VertexElement(0, 24, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),

vdecl = new VertexDeclaration(d3d, velems);

primitives = verts.Length / 2;
hasNormals = true;

And render method:

d3d.SetStreamSource(0, vbuf, 0, 28);
d3d.EnableLight(0, true);
d3d.SetRenderState(RenderState.AlphaBlendEnable, false);
d3d.SetRenderState(RenderState.Ambient, Color.Black.ToArgb());
d3d.SetRenderState(RenderState.CullMode, Cull.Counterclockwise);

d3d.VertexDeclaration = vdecl;
d3d.DrawPrimitives(PrimitiveType.TriangleFan, 0, primitives);

d3d.SetRenderState(RenderState.AlphaBlendEnable, true);
d3d.EnableLight(0, false);
d3d.SetRenderState(RenderState.CullMode, Cull.None);

Can anyone see a problem or suggest what I should be looking for?

Share this post

Link to post
Share on other sites
It seems like you're setting your near plane to 0.0. This is a bad idea, and might be the cause of your precision issues. Try setting it to something like 0.1 and seeing if it helps.

Share this post

Link to post
Share on other sites
That has helped greatly (i.e, fixed the z order completely).

I still have some minor z-fighting issues though; and the integrated Intel graphics chip on this machine doesn't support 32-bit depth buffers (can't expect clients platforms to either). Can anyone suggest other ways of reducing z-fighting other than possibly enlarging the geometry?

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!