2

something is not working as it should. If you take alook at the screenshot you will see that the result is weird. The floor of the pavilion is rendered correctly, but the columns are kinda transparent, and the roof is completele weird. I used Assimp.NET to import his mesh from a .obj file. In other engines it looked correctly. Another thing was: if i set CullMode to Back - it will cull the front faces?! I think it could be 3 things: 1,the mesh was imported wrong, or the z Buffer is not working, or maybe i need multiple world matrices (im using only one).

Does maybe anybody know what this could be?!

Screenshot:

enter image description here

Here is some code:

DepthBuffer/DepthStencilView

var depthBufferDescription = new Texture2DDescription
                {
                    Format = Format.D32_Float_S8X24_UInt,
                    ArraySize = 1,
                    MipLevels = 1,
                    Width = BackBuffer.Description.Width,
                    Height = BackBuffer.Description.Height,
                    SampleDescription = swapChainDescription.SampleDescription,
                    BindFlags = BindFlags.DepthStencil
                };
                var depthStencilViewDescription = new DepthStencilViewDescription
                {
                    Dimension = SwapChain.Description.SampleDescription.Count > 1 || SwapChain.Description.SampleDescription.Quality > 0 ? DepthStencilViewDimension.Texture2DMultisampled : DepthStencilViewDimension.Texture2D
                };
                var depthStencilStateDescription = new DepthStencilStateDescription
                {
                    IsDepthEnabled = true,
                    DepthComparison = Comparison.Always,
                    DepthWriteMask = DepthWriteMask.All,
                    IsStencilEnabled = false,
                    StencilReadMask = 0xff,
                    StencilWriteMask = 0xff,
                    FrontFace = new DepthStencilOperationDescription
                    {
                        Comparison = Comparison.Always,
                        PassOperation = StencilOperation.Keep,
                        FailOperation = StencilOperation.Keep,
                        DepthFailOperation = StencilOperation.Increment
                    },
                    BackFace = new DepthStencilOperationDescription
                    {
                        Comparison = Comparison.Always,
                        PassOperation = StencilOperation.Keep,
                        FailOperation = StencilOperation.Keep,
                        DepthFailOperation = StencilOperation.Decrement
                    }
                };

Loading mesh files:

 public static Mesh Stadafax_ModelFromFile(string path)
        {
            if (_importContext.IsImportFormatSupported(Path.GetExtension(path)))
            {
                var imported = _importContext.ImportFile(path, PostProcessSteps.Triangulate | PostProcessSteps.FindDegenerates | PostProcessSteps.FindInstances | PostProcessSteps.FindInvalidData | PostProcessSteps.JoinIdenticalVertices | PostProcessSteps.OptimizeGraph | PostProcessSteps.ValidateDataStructure | PostProcessSteps.FlipUVs);
                Mesh engineMesh = new Mesh();
                Assimp.Mesh assimpMesh = imported.Meshes[0];

                foreach(Face f in assimpMesh.Faces)
                {
                    engineMesh.Structure.Faces.Add(new Rendering.Triangle((uint)f.Indices[0], (uint)f.Indices[1], (uint)f.Indices[2]));
                }
                List<Vector3D>[] uv = assimpMesh.TextureCoordinateChannels;
                for(int i = 0; i < assimpMesh.Vertices.Count; i++)
                {
                    engineMesh.Structure.Vertices.Add(new Vertex(new Vector4(assimpMesh.Vertices[i].X, assimpMesh.Vertices[i].Y, assimpMesh.Vertices[i].Z, 1), RenderColorRGBA.White, new Vector2(uv[0][i].X, uv[0][i].Y)));
                }

                return engineMesh;
            }

            else
                NoëlEngine.Common.Output.Log("Model format not supported!", "Importeur", true); return null;
        }
    }

If anybody only has the smallest idea what this could be please just write a comment.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Haven't looked at DX much, but pretty sure Comparison.Always and fail / pass being both "Keep" makes your Z buffer useless. – Ray Jun 18 '18 at 10:17
  • Maybe you are right - but to what should i change it then? –  Jun 18 '18 at 10:22
  • Darn me, I meant the DepthComparison property, not how you configure your stencil buffer (though you should check it too in case you're going to use it). Your `DepthComparison` should probably be `Comparison.Less`. You can look at samples here (albeit in C++, but that's easily ported to SharpDX by just looking at the names of stuff): https://msdn.microsoft.com/en-us/library/windows/desktop/bb205074(v=vs.85).aspx – Ray Jun 18 '18 at 10:27
  • DepthComparison = Comparison.Less does the trick!! Thank you so much!! :) –  Jun 18 '18 at 10:40
  • Glad to hear that. I'll write it as an answer so you can accept it in a few :) – Ray Jun 18 '18 at 10:41
  • Sure no problem ;) –  Jun 18 '18 at 10:45

1 Answers1

2

What you see are polygons actually behind others still being drawn above them.

When you configure the depth buffer via DepthStencilStateDescription, you set up the DepthComparison to Comparison.Always. This is not what you want, you want to use Comparison.Less.

What's the logic behind it? Every depth value for a pixel is checked whether it can actually write to the depth buffer. This check is configured with the Comparison you specified.

  • Comparison.Always always allows the new value to be written. So no matter if a polygon is actually behind others or above them or whatever, it will always override ("draw above") what's already there - even if it's behind it spatially.
  • Comparison.Less only writes the value if it is less than the current value in the depth buffer. Don't forget that smaller depth values are closer to the viewer. So a polygon closer to an existing one will override ("draw above") the old one. But if it is behind it, it won't draw. That's exactly what you want.

You can also guess what the other Comparison enumerations now do, and play around with them :)

Ray
  • 7,940
  • 7
  • 58
  • 90