private void createVertices()
{
vertices = new VertexPositionNormalTexture[nVertices];
// Calculate the position offset that will center the terrain at (0, 0, 0)
Vector3 offsetToCenter = -new Vector3(((float)width / 2.0f) * cellSize,
0, ((float)length / 2.0f) * cellSize);
//Vector3 offsetToCenter = new Vector3((width-3) * pos.X * cellSize,
// 0, (length-3) *pos.Y * cellSize);
// For each pixel in the image
for (int z = 0; z < length; z++)
for (int x = 0; x < width; x++)
{
// Find position based on grid coordinates and height in heightmap
Vector3 position = new Vector3(x * cellSize, heights[x, z],
z * cellSize) + offsetToCenter;
// UV coordinates range from (0, 0) at grid location (0, 0) to (1, 1) at grid location (width, length)
Vector2 uv = new Vector2((float)x / width, (float)z / length);
// Create the vertex
vertices[z * width + x] = new VertexPositionNormalTexture(position,
Vector3.Zero, uv);
}
}