从List <vector3> </vector3>获取顶点和索引

时间:2013-07-19 14:18:06

标签: c# 3d xna

我正在尝试从Vector3元素列表中实现绘制三角形。

以前我使用高度图来创建顶点和索引,但是这很好,因为它是2d数组中的矩形而不是列表。

我如何处理(或修改)现有代码来处理列表而不是二维数组。

我现有的顶点代码:

public VertexPositionNormalTexture[] getVerticies(float[,] heightData)
    {
        VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[terrainLength * terrainWidth];

        for (int y = 0; y < terrainLength; y++)
        {
            for (int x = 0; x < terrainWidth; x++)
            {

                // position the vertices so that the heightfield is centered
                // around x=0,z=0
                vertices[x + y * terrainWidth].Position.X = terrainScale * (x - ((terrainWidth - 1) / 2.0f));
                vertices[x + y * terrainWidth].Position.Z = terrainScale * (y - ((terrainLength - 1) / 2.0f));

                vertices[x + y * terrainWidth].Position.Y = (heightData[x, y] - 1);

                vertices[x + y * terrainWidth].TextureCoordinate.X = (float)x / terrainScale;
                vertices[x + y * terrainWidth].TextureCoordinate.Y = (float)y / terrainScale;
            }
        }

        return vertices;
    }

以下是索引的代码:

public int[] getIndicies()
    {
        int counter = 0;
        int [] indices = new int[(terrainWidth - 1) * (terrainLength - 1) * 6];

        for (int y = 0; y < terrainLength - 1; y++)
        {
            for (int x = 0; x < terrainWidth - 1; x++)
            {
                int lowerLeft = x + y * terrainWidth;
                int lowerRight = (x + 1) + y * terrainWidth;
                int topLeft = x + (y + 1) * terrainWidth;
                int topRight = (x + 1) + (y + 1) * terrainWidth;

                indices[counter++] = topLeft;
                indices[counter++] = lowerRight;
                indices[counter++] = lowerLeft;

                indices[counter++] = topLeft;
                indices[counter++] = topRight;
                indices[counter++] = lowerRight;
            }
        }

        return indices;
    }

1 个答案:

答案 0 :(得分:0)

您正在查看List<List<float>或您在此处使用的任何类型。

语法可能略有改变。