WEBINAR:
OnDemand
Application Security Testing: An Integral Part of DevOps
Drawing Primitives
Direct3D supports several other drawing primitives in addition to the triangle list, including:
 
Figure 6. Triangle Strip: A triangle strip contains a series of triangles that share common edges. 
 Point List—A list of points drawn as dots.
 Line List—A list of line segments drawn separately.
 Line Strip—A connected series of line segments.
 Triangle List—A list of independent triangles.
 Triangle Strip—A series of triangles where each triangle shares the last edge of the previous triangle as shown in Figure 6.
 Triangle Fan—A series of triangles that all share a common corner as shown in Figure 7.
 
Figure 7. Triangle Fan: A triangle fan contains a series of triangles that share a common corner. 
Triangle strips and fans may give slightly better performance than triangle lists because they contain fewer duplicated points. The difference may be small but might add up for complicated scenes containing thousands of triangles.
 
Figure 8. Surface in Triangle Strips: The d3dDrawTriangleStrips program uses triangle strips to draw a threedimensional surface. 
Notice that the triangles in a triangle strip have alternating orientations. In
Figure 6, the first triangle defined by points 1, 2, and 3 is oriented clockwise but the second triangle defined by points 2, 3, and 4 is oriented counterclockwise. For culling purposes, Direct3D orients the oddnumbered triangles normally and switches the orientation of the evennumbered triangles.
The sample program
d3dDrawTriangleStrips, shown in
Figure 8, uses triangle strips to draw a threedimensional surface. It uses the following code to generate the strips. For each Z value between
ZMIN and
ZMAX, the program defines a triangle strip. For each strip, it loops through X values between
XMIN and
XMAX and makes two triangles in the strip for the unit square with a corner at point (x, z):
' Make the vertexes.
Dim i As Integer = 0
For z As Integer = ZMIN To ZMAX  1
For x As Integer = XMIN To XMAX
MakePoint(vertices, i, Color.Green, x, F(x, z), z)
MakePoint(vertices, i, Color.Green, x, F(x, z + 1), z + 1)
Next x
Next z
The following code shows the function
F, which gives a Y value for each X and Z value:
' A threedimensional function to draw.
Private Function F(ByVal x As Single, ByVal z As Single) As Single
Return (Sin(x) + Sin(z)) * 0.5
End Function
For each triangle strip, the program calls the
DrawPrimitive method, passing it the TriangleStrip flag, the index of the first point in the triangle strip, and the number of triangles that the strip contains. It then increases the index variable by the number of points in each strip so it points to the first point in the next strip:
' Draw the primitives in the data stream.
Dim index As Integer = 0
For i As Integer = 1 To NUM_TRIANGLE_STRIPS
m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, _
index, NUM_TRIANGLES_PER_STRIP)
index += NUM_POINTS_PER_STRIP
Next i
 
Figure 9. Surface in Lines: The d3dDrawLineStrips program uses line strips to draw a threedimensional surface. 
The sample program d3dDrawLineStrips, shown in
Figure 9, draws the same surface with line strips. Here's how the program generates its point data. For each Z value, it creates a line strip with that Z value and that has X values between
XMIN and
XMAX. It then repeats those steps, swapping the roles of X and Z to create line strips in the other direction:
' Make the vertexes.
Dim i As Integer = 0
For z As Integer = ZMIN To ZMAX
For x As Integer = XMIN To XMAX
MakePoint(vertices, i, _
Color.Red, x, F(x, z), z)
Next x
Next z
For x As Integer = XMIN To XMAX
For z As Integer = ZMIN To ZMAX
MakePoint(vertices, i, _
Color.Red, x, F(x, z), z)
Next z
Next x
The code below shows how the
d3dDrawLineStrips draws its data, which is very similar to the code used by the
d3dDrawTriangleStrips program:
Dim index As Integer = 0
For i As Integer = 1 To NUM_LINE_STRIPS
m_Device.DrawPrimitives(PrimitiveType.LineStrip, _
index, NUM_LINES_PER_STRIP)
index += NUM_POINTS_PER_STRIP
Next i
The
d3dColoredSurface program shown in
Figure 10 uses triangle strips to draw a similar scene, but here, each point's color depends on its Y coordinate. It colors the large Y coordinates red and small Y coordinates blue.
 
Figure 10. Colored Surface: In the d3dColoredSurface program, a point's color depends on its Y coordinate. 

 
Figure 11. Fanfare: The d3dDrawTriangleFans program uses two triangle fans to draw a spinning top. 


The final program example described in this column,
d3dDrawTriangleFans, uses two triangle fans to draw the spinning toplike shape shown in
Figure 11.