C# DirectX中的网格渲染不正确
我对DirectX还是一个新手,但我即将实现在C#中呈现特殊文件格式的目标。我还有最后一个障碍。我已经编写了一些代码来将文件解析为网格对象。我这样做是为了允许渲染多个纹理。正如我之前所说,我是DirectX的新手,从未从头创建过网格对象。为了更好地理解如何从头开始创建一个,我在互联网上浏览了很多次。然而,关于这个主题的许多教程都没有涉及子网格,甚至微软自己的文档也缺乏。我遇到的问题是模型渲染不正确(请参见下文)。我真的不确定我的定语属性是否正确。或者我的索引整体上不正确(渲染所有模型时)。 我可以排除的一件事是我的数据的有效性。我100%确信我的数据是正确的。我的顶点位置、纹理贴图和索引(针对每个单独模型的顶点数据进行更正)。如果我调整循环并仅渲染第一个模型。这是我的结果。 这是我的密码。我知道这很混乱:|C# DirectX中的网格渲染不正确,c#,3d,directx,render,C#,3d,Directx,Render,我对DirectX还是一个新手,但我即将实现在C#中呈现特殊文件格式的目标。我还有最后一个障碍。我已经编写了一些代码来将文件解析为网格对象。我这样做是为了允许渲染多个纹理。正如我之前所说,我是DirectX的新手,从未从头创建过网格对象。为了更好地理解如何从头开始创建一个,我在互联网上浏览了很多次。然而,关于这个主题的许多教程都没有涉及子网格,甚至微软自己的文档也缺乏。我遇到的问题是模型渲染不正确(请参见下文)。我真的不确定我的定语属性是否正确。或者我的索引整体上不正确(渲染所有模型时)。 我
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);
}
}
任何形式的帮助都将不胜感激。
谢谢只是一个猜测:如果指定
range.FaceStart=index.Count代码>?如果单独渲染子网格,我仍然会得到相同的结果,因此首先使用子网格1运行程序,然后使用子网格2、3等运行程序?在我看来,进入输入汇编程序的步幅大小似乎不正确。我在你的代码中看不太清楚,也许是一些hlsl顶点着色器代码?
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