Indexed and Nonindexed Primitives

[Previous] [Next]

Immediate Mode offers two ways of grouping the vertices that define a primitive: using nonindexed primitives and using indexed primitives. To create a nonindexed primitive, you fill an array with an ordered list of vertices. By ordered, I mean that the order of the vertices in the array indicates how to build the triangles. The first triangle consists of the first three vertices in the array, the second triangle consists of the next three vertices, and so on. This means that vertices can't be shared by multiple triangles. If you have two triangles that are connected, you'll have to specify the same vertices multiple times. To create an indexed primitive, you fill an array with an unordered list of vertices and create a second array with an ordered list of indices into the unordered array. With indexed primitives, the order of vertices in the array isn't important. Instead, the order of the indices in the index array indicates how to build the triangles. The first triangle consists of the vertices referenced by the first three indices in the index array, the second triangle consists of the vertices referenced by the next three indices, and so forth. This means that vertices can be shared by multiple triangles, simply by having multiple entries in the index array refer to the same vertex. Triangle strips and fans, discussed later in this chapter, share vertices also, so you should choose between indexed and nonindexed primitives based on a 3D object's structure.

Most 3D models share a number of vertices. Therefore, you can save memory, bandwidth, and CPU time by sharing these vertices among multiple triangles (or lines). When 3D models share vertices, indexed vertices are quite useful. However, be aware that some vertices that look like candidates for sharing actually aren't. A cube is a classic example of this. It's tempting to use only 8 vertices and share them among the 12 triangles needed to form a cube. But if you do this, you'll have problems with the vertices' normal vectors. The face of each triangle has a vector, called a face normal, that is perpendicular to the face's plane. The face normal points away from the front side of the triangle's face, which in Direct3D is the only visible side. Because normals are defined per vertex rather than per face, two triangles with different normals won't be able to share a vertex. Well, they could, but the shared vertex would have only one normal, so the lighting effect probably wouldn't be what you want. In the case of the cube, it's actually better to use 24 vertices, which is still better than the 36 vertices that you'd need for the 12 triangles if you didn't use indexing. Figure 6-1 illustrates how these normals are organized.

click to view at full size.

Figure 6-1 Vertex normals of a cube's surface



Inside Direct3D
Inside Direct3D (Dv-Mps Inside)
ISBN: 0735606137
EAN: 2147483647
Year: 1999
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net