Question

I've been unsuccessful at getting a simple cube geometry with shading turned on to display correctly.

This is c# code, but the values are being passed through SlimDX directly to C++ code.

            pParams.BackBufferWidth = 0;
            pParams.BackBufferHeight = 0;
            pParams.BackBufferCount = 1;
            pParams.BackBufferFormat = Format::X8R8G8B8;
            pParams.Multisample = MultisampleType::None;
            pParams.MultisampleQuality = 0;
            pParams.DeviceWindowHandle = this.Handle;
            pParams.Windowed = true;
            pParams.AutoDepthStencilFormat = Format.D24X8;
            pParams.EnableAutoDepthStencil = true;
            pParams.PresentFlags = PresentFlags.None;
            pParams.FullScreenRefreshRateInHertz = 0;
            pParams.PresentationInterval = PresentInterval.Immediate;
            pParams.SwapEffect = SwapEffect.Discard;

... are the values in the PresentParameter struct used to set up my Direct3D9Device object.

During a rendering, SetRenderState is called as follows:

        this.D3DDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, this.BackColor, 10000.0f, 0);
        this.D3DDevice.SetRenderState(RenderState.Ambient, false);
        this.D3DDevice.SetRenderState(RenderState.ZEnable, ZBufferType.UseZBuffer);
        this.D3DDevice.SetRenderState(RenderState.ZWriteEnable, true);
        this.D3DDevice.SetRenderState(RenderState.ZFunc, Compare.LessEqual);
        this.D3DDevice.BeginScene();

Again, this is passed through to C++ code, which marshals the values in to calls a C++ programmer would not fear.

The primitives are diffuse colored vertices (D3DFVF_XYZ | D3DFVF_DIFFUSE). The wireframe view looks like this: wireframe view http://gallery.me.com/robert.perkins/100045/z-fightingwireframe/web.jpg

The nearer pair of larger triangles is the near face of a cube.

The filled view looks like this: full view 1 http://gallery.me.com/robert.perkins/100045/Z-fighting/web.jpg

Or this, on a subsequent rendering call: full view 2 http://gallery.me.com/robert.perkins/100045/zfight2/web.jpg

I'm not sure how to fix this. Where should I begin looking?

Edit: The camera projection matrix looks about like this for one of the frames:

{[[M11:0.6281456 M12:0.7659309 M13:0.1370506 M14:0] 
  [M21:0.7705086 M22:-0.5877584 M23:-0.2466911 M24:0] 
  [M31:-0.1083957 M32:0.2605566 M33:-0.9593542 M34:0] 
  [M41:-3.225646 M42:-1.096823 M43:20.91392 M44:1]]}

And, the view matrix looks like this:

camera.ViewMatrix = {[[M11:0.6281456 M12:0.7659309 M13:0.1370506 M14:0] 
                      [M21:0.7705086 M22:-0.5877584 M23:-0.2466911 M24:0] 
                      [M31:-0.1083957 M32:0.2605566 M33:-0.9593542 M34:0] 
                      [M41:-3.225646 M42:-1.096823 M43:20.91392 M44:1]]}
Was it helpful?

Solution

Clear the Z-Buffer to 1.0f not 10000.0f.

From the Clear docs in the SDK:

[in] Clear the depth buffer to this new z value which ranges from 0 to 1.. 

It may also be useful to see your projection matrix and viewport settings ...

Edit: How do you build that projection matrix? You have set zNear to 0 and zFar to 1. Try setting your zNear to 0.001f and zFar to 1000.0f and see whether that helps you at all...

OTHER TIPS

A hunch: Try enabling the Z-Buffer before you clear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top