Search code examples
directx-9sharpdxslimdx

Polygon draw order issue


I'm working on a software and I'd to move the rendering API from SlimDX to SharpDX. I've done that but I noticed that the solid polygon are not drawn correctly i.e., even if the polygon is at the back side of another polygon, part of it is visible, also part of the polygon lie inside terrain that is also visible. Below are the images from SlimDX implementation and SharpDX implementation for reference:

enter image description here

enter image description here

I tried to set the Stencil & ZBuffer but there are not working as expected. I've already applied DepthBias to it which worked fine with SlimDX but not with SharpDX.

Please let me know if, I'm missing something or I'm doing something wrong.

Update 1:

// set the render state to device
device.SetRenderState(RenderState.AlphaTestEnable,
                      true);
device.SetRenderState(RenderState.AlphaBlendEnable,
                      true);
device.SetRenderState(RenderState.SourceBlend,
                      Blend.SourceAlpha);
device.SetRenderState(RenderState.SeparateAlphaBlendEnable,
                      true);
device.SetRenderState(RenderState.DestinationBlendAlpha,
                      Blend.One);
device.SetRenderState(RenderState.SourceBlendAlpha,
                      Blend.InverseDestinationAlpha);

// set the render state to the device
device.SetRenderState(RenderState.DepthBias,
                      -1E-06f);

this.effectVectoring.Begin(FX.None);

// binds the vertex buffer to device data stream
device.SetStreamSource(0,
                       this.vertexBuffer,
                       dataGeometry.FillVertexOffset,
                       stride);
// pass 1 to shader as a pass if it is 3D mode else 0
this.effectVectoring.BeginPass(is3D ? 1 : 0);
// renders the non-index geometric primitives using the data from current input stream
device.DrawPrimitives(SharpDX.Direct3D9.PrimitiveType.TriangleList,
                      0,
                      primitiveCount);
// end the pass
this.effectVectoring.EndPass();

this.effectVectoring.End();
device.SetRenderState(RenderState.AlphaBlendEnable,
                    false);
device.SetRenderState(RenderState.AlphaTestEnable,
                    false);
device.SetRenderState(RenderState.DepthBias,
                    0);
device.SetRenderState(RenderState.FillMode,
                    FillMode.Solid);
device.SetRenderState(RenderState.SeparateAlphaBlendEnable,
                    false);

Solution

  • After much debugging and understanding the code I found that the rendering code was correct but while creating the Viewport of the device I forgot to set the Min & Max Depth of it. In SlimDX they are set by default but in SharpDX you need to set them explicitly, this solves my issue.