Welding Vertices in a Mesh

Another way to simplify vertices with much less control, but at a much higher speed, is to weld similar vertices together. There is a method on the mesh class, "WeldVertices", that can be used to weld together vertices that are replicated and have equal attribute values. The method prototypes contain the following:

 public void WeldVertices ( Microsoft.DirectX.Direct3D.WeldEpsilonsFlags flags ,     Microsoft.DirectX.Direct3D.WeldEpsilons epsilons ,     Microsoft.DirectX.Direct3D.GraphicsStream adjacencyIn ,     Microsoft.DirectX.Direct3D.GraphicsStream adjacencyOut ,     out int[] faceRemap ,     Microsoft.DirectX.Direct3D.GraphicsStream vertexRemap ) 

The last four parameters have already been discussed at length, so we won't delve into those here once more. They are the same as they were in the clean function we've discussed previously. The first two parameters, however, control how the various vertices are welded together. The first parameter can be one or more of the values found in Table 7.2:

Table 7.2. Flags for Welding Vertices

WeldEpsilonsFlags.WeldAll

Welds all vertices marked by adjacency as being overlapping.

WeldEpsilonsFlags.WeldPartialMatches

If the given vertex is within the epsilon value given by the WeldEpsilons structure, modify the partially matched vertices to be identical. If all components are equal, then remove one of the vertices.

WeldEpsilonsFlags.DoNotRemoveVertices

Can only be used if WeldPartialMatches is specified. Only allows modification of vertices, but not removal.

WeldEpsilonsFlags.DoNotSplit

Can only be used if WeldPartialMatches is specified. Does not allow the vertices to be split.

The WeldEpsilons structure itself is strikingly similar to the AttributeWeights structure used for simplifying a mesh. The only difference is there is a new member for this structure that controls the tessellation factor. Tessellating a mesh is the act of changing the level of detail in the model by either removing triangles, or splitting a single triangle into two or more smaller triangles. All other members remain the same.

To show the effect of using this method, we'll once again modify the existing MeshFile example to weld its vertices together. Since this is really only one call that operates in place, there isn't much code we will need to add. The first time a key is pressed we will do the weld, so replace the OnKeyPress method with the following:

 protected override void OnKeyPress(KeyPressEventArgs e) {     Console.WriteLine("Before: {0}", mesh.NumberVertices);     mesh.WeldVertices(WeldEpsilonsFlags.WeldAll, new WeldEpsilons(), null, null);     Console.WriteLine("After: {0}", mesh.NumberVertices); } 

Running the example now and pressing the space bar will result in the following text output:

 Before: 4432 After: 3422 

Not quite the 91% reduction we got when simplifying, but it was much faster than before. However, did you notice how the texture coordinates changed when you hit the space bar? This was caused by the removal of some of the vertices in that area. Up close, it's quite noticeable, but at a distance, once again, it's hardly obvious unless you're looking for it.

SHOP TALK: VALIDATING YOUR MESH

Wouldn't it be great if there was a way to detect whether or not your mesh needed cleaning or was ready to be optimized? Naturally, there is. The mesh class has a method, "Validate", which contains four different overloads. One of the overloads looks like the following:

[View full width]

public void Validate ( Microsoft.DirectX.Direct3D.GraphicsStream graphics/ccc.gif adjacency , System.String errorsAndWarnings )

There are also overloads that support integer arrays as the adjacency information, plus you can use the functions with or without the errors parameter.

If the mesh is valid, this method will succeed, and the errors output string (if specified) will be System.String.Empty. If the mesh isn't valid (for example, invalid indices), then the behavior is dependant on whether or not the errors output string is specified. If it is, the function will return the errors in the mesh in this string, but succeed. If it is not, the function will throw an exception.

Using the Validate method before any simplification or optimization methods can help eliminate any unnecessary failures of those methods.



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