日期:2012-02-29  浏览次数:20422 次





这里我想继续写点和Mesh有关的东西,毕竟我们可能还需要对它有很多别的要求。在3D游戏的实际运用中,一般来说都是运用低多边形模型,简称低模。这样才能有更加好的速度来运行游戏,恰好DX中有提供给我们这样的函数让我们来控制读入的Mesh的复杂程度。



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 )



这个方法能实现简化模型的目的,前2个参数用来确定怎么简化模型,



第一个标志一共包括以下几个:



Member



Value



Description



DoNotSplit



8



Instructs the weld to allow vertices to be modified only, not removed. This flag is valid only if WeldPartialMatches is set. It is useful to modify vertices so that they are equal, but not to allow vertices to be removed.



只有当WeldPartialMatches参数指定时才能生效,不允许分离定点



DoNotRemoveVertices



4



Instructs the weld to allow vertices to be modified only, not removed. This flag is valid only if WeldPartialMatches is set. It is useful to modify vertices to be equal, but not to allow vertices to be removed.



只有当WeldPartialMatches参数指定时才能生效,不能移除定点,只能修改



WeldPartialMatches



2



If a given vertex component is within epsilon, instructs the weld to modify partially matched vertices so that both components are equal. If all components are equal, one of the vertices is removed.



修改符合在WeldEpsilons结构中给定的顶点的条件



WeldAll



1



Welds all vertices that are marked by adjacency as being overlapping.



焊接所有的adjacency指定的定点



例如我们可以这样简单的调用这个方法



mesh.WeldVertices(WeldEpsilonsFlags.WeldAll, new WeldEpsilons(), null, null);

当然 前提是你必须在这之前对变量mesh进行付值。

到程序里面看看,效果还是不错的,已经简化了很多顶点。


 


或许你还觉得不够简化,没关系这里我们还可以把复杂的模型拆成小块,用下面这个函数:

public static Microsoft.DirectX.Direct3D.Mesh[]
Split
(



Mesh mesh , //要拆分的mesh



int[] adjacencyIn ,



System.Int32 maxSize ,// 拆分后新的Mesh的最大顶点数量



MeshFlags options , //标志



    out GraphicsStream adjacencyArrayOut , //这三个out 参数返回新mesh的一些信息



    out GraphicsStream faceRemapArrayOut ,



    out GraphicsStream vertRemapArrayOut )




 


我们也可以使用简单的重载版本的函数。



在拆分前,我们需要建立保存拆分后的各个mesh的容器,数组不错。



我们这么写:



Mesh  splitmeshes[]=meshes = Mesh.Split(mesh, null, 500, mesh.Options.Value);




 


这样我们就把mesh根据顶点的树木分成了很多个部分



在我们这个事例程序里面,通过控制对数组的遍历来实现对所有拆分出来的mesh的遍历。



完整代码如下:



using System;



using System.Drawing;



using System.Collections;



using System.ComponentModel;



using System.Windows.Forms;



using System.Data;



using Microsoft.DirectX;



using Microsoft.DirectX.Direct3D;




 


namespace Chapter7Code



{



     /// <summary>



     /// Summary description for Form1.



     /// </summary>



     public class Form1 :