Optimizing Mesh Data

Cloning a mesh isn't the only way to enhance an existing mesh. Meshes can also be optimized in multiple ways. The Optimize function on a mesh is similar to the clone method, in that it can generate a new mesh with different options; however it can also perform optimizations during the generation of this new mesh. You cannot use the Optimize function to add or remove vertex data or clone to a new device though. Look at the main overload for the Optimize method:

 public Microsoft.DirectX.Direct3D.Mesh Optimize (     Microsoft.DirectX.Direct3D.MeshFlags flags ,     int[] adjacencyIn , out int[] adjacencyOut ,     out int[] faceRemap ,     out Microsoft.DirectX.Direct3D.GraphicsStream vertexRemap ) 

Each of the four overloads for this method takes this set of arguments, or simply the flags and adjacency in information. Note, that the adjacencyIn parameter can be used as an array of integers (as shown above), or as a GraphicsStream.

The flags parameter is used to determine how the new mesh can be created. This member can be one or more flags from the MeshFlags enumeration (excluding the Use32Bit or the WriteOnly flags). However, there are numerous Optimize flags in this enumeration that can be used specifically for this function. See Table 7.1:

Table 7.1. Mesh Optimization Options

MeshFlags.OptimizeCompact

Reorders the faces in the mesh to remove unused vertices and faces.

MeshFlags.OptimizeAttrSort

Reorders the faces of the mesh so that there are fewer attribute state changes (i.e., materials), which can enhance DrawSubset performance.

MeshFlags.OptimizeDeviceIndependent

Using this option affects the vertex cache size by specifying a default cache size that works well on legacy hardware.

MeshFlags.OptimizeDoNotSplit

Using this flag specifies that vertices should not be split if they are shared between attribute groups.

MeshFlags.OptimizeIgnoreVerts

Optimize faces only; ignore vertices.

MeshFlags.OptimizeStripeReorder

Using this flag will reorder the faces to maximize the length of adjacent triangles.

MeshFlags.OptimizeVertexCache

Using this flag will reorder the faces to increase the cache hit rate of the vertex caches.

The adjacencyIn parameter is also required, and can either be an integer array containing three integers per face that specify the three neighbors for each face in the mesh, or a graphics stream containing this same data.

OPTIMIZING MESHES IN PLACE

What if you don't want to create a whole new mesh though? You don't really want to change the various creation flags; you just want the benefits of the optimizations. There is a near identical method on the mesh object called "OptimizeInPlace" that takes the same parameters, with only two differences. The flags parameter must be one of the optimization flags (you cannot change any of the creation flags), and this method has no return value. All of the optimizations will happen directly on the mesh that calls this method.

If you choose one of the two overloads that has more than those two overloads, the last three parameters are all data that will be returned to you via an out parameter. The first member of this returned data will be the new adjacency information returned to you. The second member will be the new index for each face in the mesh. The final member will be a graphics stream that contains the new index for each vertex. Many applications will not require this information, and can skip using these overloads.

The following small section of code shows sorting a mesh by its attribute buffer, and ensuring that it is in managed memory:

 // Compact our mesh Mesh tempMesh = mesh.Optimize (MeshFlags.Managed |     MeshFlags.OptimizeAttrSort | MeshFlags.OptimizeDoNotSplit, adj); mesh.Dispose(); mesh = tempMesh; 

GENERATING ADJACENCY INFORMATION

You may notice that many of the advanced methods of the mesh take in adjacency information. You can get this information during mesh creation, but what if you are receiving a mesh that has already been created? There is a method on the mesh object "GenerateAdjacency" that you can use to get this information. The first parameter to this function is a float value that you can use to specify that vertices whose position differ by less than this amount will be treated as coincident. The second parameter is an integer array that will be filled with the adjacency information. This array must be at least three * mesh.NumberFaces.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

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