In the other tutorials in the Primitives category we have drawn only lists of triangles. There are other choices though. In this tutorial, we will take a quick look at some of the other types of primitives that you can draw, and then briefly discuss some of the things that you will need to think about when you are using the various primitive types.
Different Primitive Types
I'm sure you remember that all we really send to the graphics card is a list of vertices and a list of indices to give a drawing order for the vertices. This by itself, isn't enough for the graphics card to draw anything, and so we have also had to tell the graphics device what it is exactly that we are trying to draw. Go ahead and find the line in your Draw() method that looks like this:
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 12, 0, 20);
In this line, we state that our vertices represent triangles, and in fact, it is simply a list of triangles. But there are other choices, though, which we will discuss here.
This mode says that each pair of vertices creates a line. That is, the vertex referred to by the first index is one endpoint, and the vertex referred to by the second index is the other endpoint. The third index will mark the beginning point of a new line, which will be connected to index 4.
Occasionally, we want to draw from one point to the next, and then the next, and so on in succession, like connect-the-dots. If we used the LineList type, we would have to say "I want a line from 1 to 2, then another from 2 to 3, then another from 3 to 4" and so on. You can see that we'd be repeating a lot. The LineStrip type is designed for this type of situation. It will draw a line from index 1 to index 2, then another to the next index, then another to the next index.
This is the type that we've been using so far. In this mode, we are saying "take the first set of three vertices and make a triangle out of them, then the second set of three and make a second set of triangles" and so on.
This type is a little fancier than the triangle list type. What this does, is it takes the 1st, 2nd, and 3rd vertices and makes a triangle. Then it takes the 2nd, 3rd, and 4th and makes a second triangle. Then it takes the 3rd, 4th, and 5th and makes a third triangle, and so on. This is actually a very efficient method of doing this, and actually, many models or meshes could be rendered like this, depending on how they are constructed.
Using Other Primitive Types
Using a different primitive type is pretty easy. You just change the line in question to use the primitive type that you want. For example, I have changed the primitive type to PrimitiveType.LineList in the code from the previous tutorial, and I get the image shown below:
Of course this looks a little funny, but that is because the index buffer data was set up with triangles in mind, not lines. This brings up an important point, though, that you need to make sure the data in the index buffer matches up with the primitive type you are planning on using.
That covers the various types of primitive rendering that you can do. This is currently the last tutorial in the Primitives category, but we've covered quite a bit, and it should be enough to get you started with any sort of project that you can dream up that uses primitives.
|Having problems with this tutorial? Try the troubleshooting page!|