Cloning Mesh Data

So you've finally gotten your mesh loaded and rendered in your scene. The scene has a few lights, and yet your mesh just appears completely black. Glancing at the properties of your mesh, you notice the vertex format doesn't include normal data, which as we already know is required for lighting calculations.

What we need is a simple way to take all of the mesh data we already have, and simply add the normal data to it. If you guessed that there is already a mechanism for this, then you've guessed correctly. Look at Listing 7.1:

Listing 7.1 Ensuring That Your Mesh Contains Normal Data
 // Check if mesh doesn't include normal data if ((mesh.VertexFormat & VertexFormats.Normal) != VertexFormats.Normal) {     Mesh tempMesh = mesh.Clone(mesh.Options.Value,         mesh.VertexFormat | VertexFormats.Normal, device);     tempMesh.ComputeNormals();     // Replace existing mesh     mesh.Dispose();     mesh = tempMesh; } 

Here we take an existing mesh object we've created named "mesh", and check to see if it already includes normal data. The VertexFormat property returns a list of VertexFormats that are combined via a bitwise OR, so we use our bitwise AND operator to determine whether the normal bit is set. If it is not, we then create a second temporary mesh via the Clone method on our original mesh. The Clone method has three overloads:

 public Microsoft.DirectX.Direct3D.Mesh Clone (     Microsoft.DirectX.Direct3D.MeshFlags options ,     Microsoft.DirectX.Direct3D.GraphicsStream declaration ,     Microsoft.DirectX.Direct3D.Device device ) public Microsoft.DirectX.Direct3D.Mesh Clone (     Microsoft.DirectX.Direct3D.MeshFlags options ,     Microsoft.DirectX.Direct3D.VertexElement[] declaration ,     Microsoft.DirectX.Direct3D.Device device ) public Microsoft.DirectX.Direct3D.Mesh Clone (     Microsoft.DirectX.Direct3D.MeshFlags options ,     Microsoft.DirectX.Direct3D.VertexFormats vertexFormat ,     Microsoft.DirectX.Direct3D.Device device ) 

In our example, we used the final overload. In each of the overloads, the first and last parameters are the same. The options parameter allows the newly created mesh object to have a different set of options than the original mesh did. If you want to keep the same options in the new mesh, then you can do what we've done in our example code above, using the existing options from the original mesh; however, you can also change them. Say you wanted the new mesh to reside in system memory rather than managed memory. Any of the MeshFlags that are available during mesh creation are also available during cloning.

The last parameter of the clone method is the device the new mesh will be created on. In many instances, this device will be the same device you used to create the original mesh, but it is possible to clone a mesh to an entirely different device. For example, let's say you were writing an application that was multiple-monitor aware, and had each monitor running in full-screen mode. While your mesh object was on your first monitor, and being rendered there, it isn't necessary to have an "instance" of this mesh on the secondary monitor. If the mesh now needs to be rendered on the secondary monitor as well, you can easily clone it to the new device, and have it available there as well. Our preceding example simply used the existing device we already had.

The middle parameter determines how the data will actually be rendered. In the overload we use, we pass in the vertex format of the newly cloned mesh (which can be different from the original). In each of the other overloads, the parameter is intended to be the vertex declaration of the newly created mesh. Vertex declarations are used in the programmable pipeline, which hasn't been covered yet, but they are essentially a much more powerful version of the fixed function vertex format options.

As you can see, there is also a helper function on the mesh class that can automatically compute the normals of a mesh. There are three overloads for this helper function as well. There is the parameter-less one we used, as well as two others that take in a single parameter for the adjacency information, either as an array of integers, or as a GraphicsStream. If you have the adjacency information, feel free to use it.

You can use the mesh's cloning ability to create new instances of the same mesh on a new device, or to change the options of an already created mesh. You can use it to create a mesh with new vertex format items, or even to remove existing vertex format data from your mesh. If the mesh you have currently doesn't meet your needs, you can probably clone it into a more suitable version.



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