To allow the Mesh to work as efficiently as possible, the code should let the Mesh optimize its data. The following code shows how the example program does this. The optimization method takes as a parameter an array listing the faces adjacent to each of the Mesh's faces (fortunately, the Mesh's GenerateAdjacency
method can build this array for you so you don't need to do it yourself). The parameter 0.1
tells this method how precise the vertex coordinates are. In this example, if two vertices lie with 0.1 units of each other, the method considers them to be the same point:
Dim adjacency( _
m_Mesh.NumberFaces * 3 - 1) _
' Remember the number of subsets.
m_NumSubSets = m_Mesh. _
The preceding code creates an array big enough to hold the adjacency information and then calls the GenerateAdjacency
method to fill in the array's entries. It next calls the Mesh's OptimizeInPlace
method to optimize the Mesh, and finally, saves the number of subsets in the Mesh for later use.
The rest of the setup code creates materials and textures for each of the Mesh's subsets (that process is explained in Part 2
, so I won't repeat that discussion here). For now, all you need to know is that the program loads one material and texture for each subset into the arrays m_Materials
The code that draws the Mesh is very similar to the code used in Part 2 of this series. Because both approaches involve drawing scenes, you set up matrices, and then select a material and texture before calling a drawing method to produce a picture. The difference is in how the two draw objects in the scene. The previous approach used the Direct3D device's DrawPrimitives
method to generate results. In contrast, the d3dMakeMesh
program example calls the Mesh's DrawSubset
method to draw each of its subsets. The Mesh then handles the details of drawing triangle strips, fans, or whatever.
The following fragment shows the core of the d3dMakeMesh
program's drawing code. For each subset, the program selects that subset's material and texture, and then calls the Mesh's DrawSubset
|Figure 1. Pool Parlor: The sample program d3dMakeMesh builds a Mesh representing a brick courtyard that contains a pool of water and two blocks of wood.|
' Draw the mesh's subsets.
For i As Integer = 0 To m_NumSubSets - 1
m_Device.Material = m_Materials(i)
shows the complete CreateMesh
method. Figure 1
shows the sample d3dMakeMesh
program in action. As discussed, above, the program makes four subsets that represent the groups of faces that use four different textures. The different subsets define the brick ground, the bricks inside the pool area, the water, the sides of the wooden blocks, and the tops of the wooden blocks.
The result is an impressively complicated scene drawn with a few simple lines of code. However, a major part of the motivation behind using a Mesh is not to draw a complex scene, but to do so in a standardized way so you can load and save Mesh data. The next sections explain how to save a Mesh into a .x
file and load a Mesh from such a file.