luigivampa
luigivampa

Reputation: 397

xna texture coordinates

I've been trying to combine a couple of riemers tutorials to make a terrain that is textured and lit. I'm almost there but I can't get the application of the texture right. I believe the problem is in SetUpVertices() with the setting of the texture coordinates. I know currently the code reads that they're all set to (0, 0) and I need to have it so that they are set to the corners of the texture but I can't seem to get the code right. Anybody out there able to assist?

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've added the full code of Game1.cs to this pastie http://pastebin.com/REd8QDZA

Upvotes: 3

Views: 1365

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564413

You can stretch the texture across 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);

Upvotes: 3

Related Questions