1
\$\begingroup\$

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 (Polygon rendering):

// 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);

Update 2 (Terrain rendering):

device.SetRenderState(RenderState.AlphaBlendEnable,
                      false);
device.SetRenderState(RenderState.AlphaTestEnable,
                      false);
device.SetRenderState(RenderState.CullMode,
                      Cull.Counterclockwise);

device.Indices = this.terrainIB;
device.SetStreamSource(0,
                       this.terrainVB,
                       0,
                       12);
device.VertexFormat = VertexFormat.Position;

// Set texture in effect...

effect.Begin(FX.None);

effect.BeginPass(pass);
device.DrawIndexedPrimitive(PrimitiveType.TriangleStrip,
                            baseVertexIndex,
                            0,
                            vertexCount,
                            startIndex,
                            primitiveCount);
effect.EndPass();

effect.End();
device.SetRenderState(RenderState.FillMode,
                      FillMode.Solid);
device.SetRenderState(RenderState.CullMode,
                      Cull.None);
\$\endgroup\$
6
  • \$\begingroup\$ Please show us a Minimal Complete Verifiable Example of the code needed to reproduce this problem, including where you set up your ZBuffer and depth writing/depth comparison tests. With that in hand, we can test potential solutions to ensure they will work for you. \$\endgroup\$ – DMGregory Jul 10 '20 at 12:07
  • \$\begingroup\$ @DMGregory I've updated the code snippet, please take a look. Thanks! \$\endgroup\$ – Pankaj Sachdeva Jul 10 '20 at 13:33
  • \$\begingroup\$ Is there a difference between how you're drawing the terrain, versus the structure on top of it? \$\endgroup\$ – DMGregory Jul 10 '20 at 15:24
  • \$\begingroup\$ @DMGregory it is different, above is the sample source of terrain rendering. \$\endgroup\$ – Pankaj Sachdeva Jul 13 '20 at 7:52
  • \$\begingroup\$ I don't see where your terrain's depth writing is set up. Is it possible it's defaulting to not write to the depth buffer, or that something is clearing depth between drawing the terrain and drawing what's on top? (Or conversely, if you draw the stuff on top first, could the terrain be always failing the z test?) \$\endgroup\$ – DMGregory Jul 13 '20 at 11:59
0
\$\begingroup\$

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. Thank you all for the help.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.