Xna texture coordinates

I was trying to combine multiple riemers to create a terrain that is textured and lit. I'm almost there, but I can't seem to apply the texture correctly. I believe the problem is with SetUpVertices () with setting texture coordinates. I know the code currently reads that they are all set to (0, 0) and I need them to be set to the corners of the texture, but I can't get the code right. Can anyone please help?

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 = 0;
            vertices[x + y * terrainWidth].TextureCoordinate.Y = 0;
        }
    }
}

      

I have added the complete Game1.cs code to this paste http://pastebin.com/REd8QDZA

+3


source to share


1 answer


You can stretch the texture over the surface by interpolating from 0 to 1:



vertices[x + y * terrainWidth].TextureCoordinate.X = x / (terrainWidth - 1.0);
vertices[x + y * terrainWidth].TextureCoordinate.Y = y / (terrainHeight - 1.0);

      

+3


source







All Articles