VertexBuffer and IndexBuffer

http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series1/VertexBuffer_and_IndexBuffer.php

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);

to this:

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[0].SetSource(myVertexBuffer, 0, VertexPositionNormalColored.SizeInBytes);

to this:

device.SetVertexBuffer(myVertexBuffer);

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!

Advertisements

2 thoughts on “VertexBuffer and IndexBuffer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s