The MD2 format also contains instructions to let you use triangle fans and strips, instead ofjust raw triangles. Using triangle strips and fans can help you speed up the rendering of your 3D models. The secret? Triangles share two of their vertices with another triangle, rather than having three of their own.
Say you have an object with four connected triangles. Using regular triangles, you would need a total of twelve vertex calls, three for each triangle. When using fans or strips, however, this is not the case. The first three vertices of a triangle fan or triangle strip define the first triangle in the series. Each vertex after that defines a new triangle,
meaning a total of only six vertices are needed. The reason for this is simple. A new triangle is formed using the new vertex, plus two of the vertices that have already been sent to the renderer.
With a triangle strip, the most recent two vertices are used in conjunction with the new vertex to create a whole triangle, creating a shape like the one shown in the center of Figure 3.8. Triangle fans work almost the same way. However, instead of using the most recent two vertices to complete the triangle, a triangle fan uses the most recent vertex and the very first vertex instead. This creates a shape like the one shown on the right side of Figure 3.8.
An MD2 model contains a set of commands; each one represents a triangle strip or fan. Each fan or strip will contain a set of vertex indexes to use in place of the normal face indexes defined earlier in the file. Each vertex index will also contain its associated texture coordinate to replace the texture coordinates loaded earlier.
At the offset designated by the header, there are a set number of GL commands (also designated by the header). Each command is exactly four bytes and can be an integer or a floating-point value.
Each primitive starts out with an integer. This integer tells you how many vertices are in the primitive. It also tells you what type of primitive it is; if the number is negative, it is a triangle fan, positive means it is a triangle strip. The number of vertices for the fan or strip is the absolute value of that integer.
Immediately following that are the vertices. For each vertex, there are two floating-point texture coordinates ready to be plugged into the renderer without modification. Right after the two texture coordinates is an integer that is the index into the array of vertices. This pattern repeats for every vertex.
After all the vertices have been cycled through for the current primitive, you start rendering another primitive in the same way. For time and clarity’s sake, I did not include code to do this, but hopefully this description should be enough to get you going.