我仍然是DirectX的新手,但是我接近达到了在C#中渲染特殊文件格式的目标。我有一个最后的障碍。我编写了一些代码来将文件解析为网格对象。我这样做是为了允许渲染多个纹理。正如我之前所说的,我对DirectX相对较新,并且从未从头开始创建网格对象。我在网上看了很多,试着更好地了解如何从头开始创建一个。然而,关于这个主题的许多教程都没有处理子网格,甚至缺乏微软自己的文档。我遇到的问题是模型没有正确呈现(见下文)。我真的不确定我是否正确使用AttributeRanges的属性。或者我的索引作为一个整体是不正确的(渲染所有模型时)。 Image of rendering issues http://s11.postimage.org/56dms3hlf/incorrectrender.png
我可以排除的一件事是我的数据的有效性。我100%确定我的数据是正确的。我的顶点位置,纹理映射和我的索引(校正每个单独模型的顶点数据)。如果我调整我的循环并仅渲染第一个模型。继承人我的结果。 First model render http://s12.postimage.org/de89qlw8t/correctrenderof0.png
继承我的代码。我知道它很乱:|
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
任何形式的任何帮助,将不胜感激。 谢谢