This is the last tutorial for this series on terrain and what better to finish up on than some optimizations, and we don’t have much to change in this one.
In XNA Game Studio 4.0 the VertexBuffer syntax changed slightly now we don’t need to tell the graphics card the size of memory to allocate XNA does that under the hood once again thanks to the vertex declaration changes so now we simply tell it the type of vertex data we are sending and the amount of vertices that it will contain. So we change this line in the CopyToBuffers() method:
myVertexBuffer = new VertexBuffer(device, vertices.Length * VertexPositionNormalColored.SizeInBytes, BufferUsage.WriteOnly);
myVertexBuffer = new VertexBuffer(device, typeof(VertexPositionColorNormal), vertices.Length, BufferUsage.WriteOnly);
In the same method we may need to alter the index buffer statement depending on the graphics profile we are using, which I explained in earlier topics, as follows:
HiDef profile =
myIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
Reach profile =
myIndexBuffer = new IndexBuffer(device, typeof(short), indices.Length, BufferUsage.WriteOnly);
Now we can remove this line from the Draw() method again!
device.VertexDeclaration = myVertexDeclaration;
And last but not least also in the Draw() method the statement we use to set the vertex buffer on the graphics card is much simpler, we can now just pass it the buffer with no other arguments, so without further ado change this:
device.Vertices.SetSource(myVertexBuffer, 0, VertexPositionNormalColored.SizeInBytes);
So that concludes this series on terrain, I hope you’ve enjoyed following the updates to these tutorials as much as I have enjoyed writing them. If you have any questions feel free to ask comments are very welcome!