我对 DirectX 还是很陌生,但是我接近实现在 C# 中呈现特殊文件格式的目标。我有最后一个障碍。我已经编写了一些代码来将文件解析为网格对象。我这样做是为了允许渲染多个纹理。正如我之前所说,我对 DirectX 比较陌生,并且从未从头开始创建网格对象。我在互联网上看了很多,试图更好地了解如何从头开始创建一个。然而,关于这个主题的许多教程都没有处理子网格,甚至缺乏微软自己的文档。我遇到的问题是模型渲染不正确(见下文)。我真的不确定 AttributeRanges 的属性是否正确。或者我的索引整体不正确(渲染所有模型时)。
我可以排除的一件事是我的数据的有效性。我 100% 确定我的数据是正确的。我的顶点位置、纹理映射和我的索引(对于每个单独模型的顶点数据更正)。如果我调整我的循环并只渲染第一个模型。这是我的结果。
这是我的代码。我知道这很乱:|
public void RenderModel(Model[] models,bool isTextured = false,bool wireFrame = false)
{
//Model object contains all vertex data and indices for that model
//The indices do not hold any relation to other models.
this.modelss = models;
int vertexCount = 0;
for (int currentModelID = 0; currentModelID < models.Length; currentModelID++)
{
Model model = models[currentModelID];
vertexCount += model.renderDataSource.vertices.Length;
AttributeRange range = new AttributeRange();
range.AttributeId = currentModelID;
range.FaceCount = model.getIndices().Length / 3;
range.FaceStart = Indices.Count /3;
range.VertexCount = model.getTexturedVertices().Length;
range.VertexStart = vertices.Count;
this.vertices.AddRange(model.getTexturedVertices());
this.Indices.AddRange(model.getIndices());
Console.WriteLine("ID: " + range.AttributeId + " FaceCount: " + range.FaceCount + " FaceStart: " + range.FaceStart + " vercount " + range.VertexCount + " verstart: " + range.VertexStart );
attributeRanges.Add(range);
}
this.vertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured),vertices.Count,
device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Default);
vertexBuffer.SetData(vertices.ToArray(), 0, LockFlags.None);
getTextures();
this.indexBuffer = new IndexBuffer(typeof(ushort), Indices.Count, device, Usage.WriteOnly, Pool.Default);
indexBuffer.SetData(Indices.ToArray(), 0, LockFlags.None);
int faces = Indices.Count /3;
int verCount = vertices.Count;
model3d = new Mesh(Indices.Count /3 , vertices.Count, MeshFlags.SystemMemory,CustomVertex.PositionNormalTextured.Format, device);
model3d.IndexBuffer.SetData(Indices.ToArray(), 0, LockFlags.None);
model3d.VertexBuffer.SetData(vertices.ToArray(), 0, LockFlags.None);
model3d.SetAttributeTable(attributeRanges.ToArray());
Render(isTextured);
}
private void Render(bool isTextured)
{
float x = (float)Math.Cos(rot);
float z = (float)Math.Sin(rot);
device.BeginScene();
setupLightingAndCamera();
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 50f);
device.Transform.View = Matrix.LookAtLH(new Vector3(x, 8, z), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
DrawMesh();
device.EndScene();
}
private void DrawMesh()
{
for (int i = 0; i < modelss.Length; i++)
{
Model model = modelss[i];
material = new Material();
material.Diffuse = Color.White;
material.Specular = Color.LightGray;
material.SpecularSharpness = 15.0F;
device.Material = material;
//Just obtains correct file from info in dictionaries
device.SetTexture(0, textures[textureName[model.renderDataSource.shader]]);
model3d.DrawSubset(i);
}
}
最后是我的调试输出,可能会有所帮助。
ID: 0 FaceCount: 22 FaceStart: 0 vercount 34 verstart: 0
ID: 1 FaceCount: 16 FaceStart: 22 vercount 24 verstart: 34
ID: 2 FaceCount: 16 FaceStart: 38 vercount 24 verstart: 58
ID: 3 FaceCount: 16 FaceStart: 54 vercount 24 verstart: 82
ID: 4 FaceCount: 446 FaceStart: 70 vercount 562 verstart: 106
ID: 5 FaceCount: 1176 FaceStart: 516 vercount 900 verstart: 668
ID: 6 FaceCount: 28 FaceStart: 1692 vercount 32 verstart: 1568
ID: 7 FaceCount: 47 FaceStart: 1720 vercount 76 verstart: 1600
ID: 8 FaceCount: 28 FaceStart: 1767 vercount 32 verstart: 1676
ID: 9 FaceCount: 47 FaceStart: 1795 vercount 76 verstart: 1708
ID: 10 FaceCount: 94 FaceStart: 1842 vercount 90 verstart: 1784
任何形式的帮助,将不胜感激。谢谢你