如何在xna中为动态生成的地形顶点添加纹理

时间:2013-02-13 19:13:10

标签: c# xna coordinates textures

我正在尝试为每个顶点添加纹理坐标,以便为每个三角形添加草纹理。我的代码在整个区域内延伸了纹理,但是不能很好地扩展。如何正确添加(0,0),(0,1),(1,1)等顶点? 目前它们被添加到SetUpVertices()方法中,如果它们被添加到SetUpIndices()方法中,当代码可以区分它是左上角,左下角,右下角等。任何帮助都将非常感激。相关方法如下,完整的Game1.cs代码在http://pastebin.com/REd8QDZA

    private void SetUpVertices()
    {
        vertices = new VertexPositionNormalTexture[terrainWidth * terrainHeight];
        for (int x = 0; x < terrainWidth; x++)
        {
            for (int y = 0; y < terrainHeight; y++)
            {
                vertices[x + y * terrainWidth].Position = new Vector3(x, -y, heightData[x, y]);
                vertices[x + y * terrainWidth].TextureCoordinate.X = x / (terrainWidth - 1.0);
                vertices[x + y * terrainWidth].TextureCoordinate.Y = y / (terrainHeight - 1.0);
            }
        }
    }
    private void SetUpIndices()
    {
        indices = new short[(terrainWidth - 1) * (terrainHeight - 1) * 6];
        int counter = 0;
        for (int y = 0; y < terrainHeight - 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++] = (short)topLeft;
                indices[counter++] = (short)lowerRight;
                indices[counter++] = (short)lowerLeft;

                indices[counter++] = (short)topLeft;
                indices[counter++] = (short)topRight;
                indices[counter++] = (short)lowerRight;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

只需指定

即可
vertices[x + y * terrainWidth].TextureCoordinate.X = x;
vertices[x + y * terrainWidth].TextureCoordinate.Y = y;

默认情况下,将包裹大于1的纹理坐标,并重复纹理。