Time for action – adding the BuildVertexBuffer() method
1. Add the
Vertex Buffer
region to theTerrain
class as follows:#region Vertex Buffer private void BuildVertexBuffer( int width, int height, float heightScale) { VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture [width*height]; for (int x=0; x<width; x++) for (int z=0; z<height; z++) { vertices[x + (z*width)].Position = new Vector3(x, heights[x,z], z); } vertexBuffer = new VertexBuffer( device, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.WriteOnly); vertexBuffer.SetData(vertices); } #endregion
What just happened?
After
creating an empty array of VertexPositionNormalTexture
objects, we loop through the width and height of our terrain, creating a new VertexPositionNormalTexture
for each vertex. The
X and Z coordinates of the vertex are directly equal...