不规则三角形顶点索引

时间:2012-08-03 11:48:02

标签: c# windows xna

最后我展示了一些东西。切换到使用graphics.GraphicsDevice.DrawIndexedPrimitives ...下一个问题...只显示一个三角形。数据集大约是200个三角形。我格式化了进来的数据,以确保每三个连续的矢量形成一个三角形面。这些是形成不规则形状的不规则三角形。我不完全理解顶点的索引。看起来每个3个索引形成一个三角形。如果是这样,则索引与进来的数据匹配。我这样做了:

int i4 = -1;
        indices = new int[xData1.Count];
        for (int i2 = 0; i2 < xData1.Count; i2++)
        {
            i4++;
            cubeVertices[i4].Position = new Vector3((float)xData1[i2][0], (float)xData1[i2][1], (float)xData1[i2][2]);
            cubeVertices[i4].Color = Color.LawnGreen;
            indices[i4] = i4;
        }

使索引匹配进入的顶点..然后我使用Reimers normal calc来提供法线..这可能是错误的,因为他的例子是每个索引使用6个顶点(我想!),如下所示:

for (int i = 0; i < cubeVertices.Length; i++)
            cubeVertices[i].Normal = new Vector3(0, 0, 0);

        for (int i = 0; i < indices.Length / 3; i++)
        {
            int index1 = indices[i * 3];
            int index2 = indices[i * 3 + 1];
            int index3 = indices[i * 3 + 2];

            Vector3 side1 = cubeVertices[index1].Position - cubeVertices[index3].Position;
            Vector3 side2 = cubeVertices[index1].Position - cubeVertices[index2].Position;
            Vector3 normal = Vector3.Cross(side1, side2);

            cubeVertices[index1].Normal += normal;
            cubeVertices[index2].Normal += normal;
            cubeVertices[index3].Normal += normal;
        }

        for (int i = 0; i < cubeVertices.Length; i++)
            cubeVertices[i].Normal.Normalize();

我需要修理多少件事?我只看到了几百个三角形中的一个 :( 感谢你的耐心

public struct VertexPositionColorNormal
    {
        public Vector3 Position;
        public Color Color;
        public Vector3 Normal;

        public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
        (
            new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
            new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0),
            new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
        );
    }

...

private void CopyToBuffers()
    {
        vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionColorNormal.VertexDeclaration, 
            cubeVertices.Length, BufferUsage.WriteOnly);
        vertexBuffer.SetData(cubeVertices);

        myIndexBuffer = new IndexBuffer(graphics.GraphicsDevice, typeof(int), indices.Length, BufferUsage.WriteOnly);
        myIndexBuffer.SetData(indices);
    }

...

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
        {
            basicEffect.World = world;
            basicEffect.View = view;
            basicEffect.Projection = proj;

            pass.Apply();

            graphics.GraphicsDevice.Indices = myIndexBuffer;
            graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer);
            graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
                cubeVertices.Length, 0, indices.Length / 3);

1 个答案:

答案 0 :(得分:0)

你的正常计算是正确的,即使它是错误的,唯一会发生的事情是你的三角形会收到错误的闪电。

您使用的索引与进入的顶点的顺序完全匹配,这本身就是多余的。如果您切换到不设置索引并使用DrawPrimitives代替原始计数,那么会产生不同吗?

除此之外,您确定您提供的数据是否有效?顶点位置是否正确设置?