Reputation:
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:
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.
Upvotes: 2
Views: 218
Reputation: 8881
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 :)
Upvotes: 2