Sign in to follow this  
TobyWilson

[SlimDX] Problem - depth buffer?

Recommended Posts

TobyWilson    100
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:

[code]
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);
[/code]

My render method starts like this:

[code]
d3d.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
d3d.BeginScene();
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));
[/code]

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:
[code]
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);
ds.WriteRange(verts);
ds.Write(verts[0]);
vbuf.Unlock();

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),
VertexElement.VertexDeclarationEnd
};

vdecl = new VertexDeclaration(d3d, velems);

primitives = verts.Length / 2;
hasNormals = true;
}
[/code]

And render method:
[code]
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);
[/code]

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

Share this post


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

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