奇怪的立方体形状

时间:2013-06-26 05:19:21

标签: c# xna

我正在尝试创建一个包含索引和顶点的多维数据集。我能够画出它们,但它们看起来有点奇怪。

enter image description here

这是我的代码。它与顶点或索引有关,但我不确定是哪个:

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);
}

1 个答案:

答案 0 :(得分:1)

我可以做一个疯狂的猜测并说出它因为你的索引中的顶点乱序(我会进一步称它们为三角形)。

通常在3d引擎中,你必须设置三角形顶点的顺序,这样当它们从它们形成的形状外部观察它们时,它们的顺序相同 - 即顺时针或逆时针。 从数学上讲,形状中所有三角形的法线应指向形状的内部或外部。法线方向告诉3d引擎何时绘制三角形 - 如果仅在一侧绘制三角形,则引擎可以减少两倍的工作 - 用户不会看到99.99%的情况下固体对象的内部。

在您的情况下,查看索引032和476 - 它们应该是032/467或023/476。等等。