Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Direct3D, Part 3: Using Meshes to Save and Load Complex Scenes : Page 3

Direct3D's Mesh class lets you manage, save, and load complex scenes quickly and easily.




Application Security Testing: An Integral Part of DevOps

Optimizing Meshes
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:

' Optimize. Dim adjacency( _ m_Mesh.NumberFaces * 3 - 1) _ As Integer m_Mesh.GenerateAdjacency( _ CSng(0.1), adjacency) m_Mesh.OptimizeInPlace _ (MeshFlags.OptimizeVertexCache, _ adjacency) ' Remember the number of subsets. m_NumSubSets = m_Mesh. _ GetAttributeTable().Length

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 and m_Textures.

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 method:

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) m_Device.SetTexture(0, m_Textures(i)) m_Mesh.DrawSubset(i) Next i

Listing 1 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.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date