我正在尝试创建一个具有索引和顶点的立方体。我可以画它们,但它们看起来有点奇怪。
这是我的代码。它与顶点或索引有关,但我不确定哪个:
public void Draw(BasicEffect effect)
{
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(cubeVertexBuffer);
device.Indices = iBuffer;
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);
}
}
private void SetUpIndices()
{
indices = new short[36];
indices[0] = 0;
indices[1] = 3;
indices[2] = 2;
indices[3] = 2;
indices[4] = 1;
indices[5] = 0;
indices[6] = 4;
indices[7] = 7;
indices[8] = 6;
indices[9] = 6;
indices[10] = 5;
indices[11] = 4;
indices[12] = 1;
indices[13] = 2;
indices[14] = 6;
indices[15] = 6;
indices[16] = 5;
indices[17] = 1;
indices[18] = 4;
indices[19] = 7;
indices[20] = 3;
indices[21] = 3;
indices[22] = 0;
indices[23] = 4;
indices[24] = 4;
indices[25] = 0;
indices[26] = 1;
indices[27] = 1;
indices[28] = 5;
indices[29] = 4;
indices[30] = 3;
indices[31] = 7;
indices[32] = 6;
indices[33] = 6;
indices[34] = 2;
indices[35] = 3;
iBuffer = new IndexBuffer(device, typeof(short), 36, BufferUsage.WriteOnly);
iBuffer.SetData(indices);
}
private void SetUpVertices()
{
vertices = new VertexPositionColor[8];
vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), color);
vertices[1] = new VertexPositionColor(new Vector3(0, 1, 0), color);
vertices[2] = new VertexPositionColor(new Vector3(1, 1, 0), color);
vertices[3] = new VertexPositionColor(new Vector3(1, 0, 0), color);
vertices[4] = new VertexPositionColor(new Vector3(0, 0, -1), color);
vertices[5] = new VertexPositionColor(new Vector3(0, 1, -1), color);
vertices[6] = new VertexPositionColor(new Vector3(1, 1, -1), color);
vertices[7] = new VertexPositionColor(new Vector3(1, 0, -1), color);
cubeVertexBuffer = new VertexBuffer(device, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly);
cubeVertexBuffer.SetData<VertexPositionColor>(vertices);
}