1

我正在尝试创建一个具有索引和顶点的立方体。我可以画它们,但它们看起来有点奇怪。

在此处输入图像描述

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

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

1 回答 1

1

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

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

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

于 2013-06-26T06:10:19.180 回答