日期:2014-02-09  浏览次数:20446 次


 
上一次中途结束了本来应该讲到的控制Mesh的细节程度的方法的,这一次补上。
我们这里使用的是简单的方法,并没有涉及到场景剔出等等复杂的方法,我这里主要还是用DX9提供给我们的类库,progressive meshe。
progressive meshes主要的优点就是允许我们控制顶点和面的数目,这样我们就可以灵活的控制mesh细节的显示程度。
和Mesh一样,progressive meshe也都是继承自BaseMesh这个类。比较重要的属性主要有2个NumberFaces和NumberVertices。从字面我们就可以看出两个属性的含义。
有了前面的基础在使用progressive mesh上也变得十分的简单,首先我们需要声明一个progressive mesh的变量,然后我们看看progressive mesh的构造函数:
public ProgressiveMesh(
    Mesh mesh,
    GraphicsStream adjacency,
    GraphicsStream vertexWeights, //顶点的权值,越高越不容易被剔出,如果设为null就全部被设为1
    int minValue, //设定被简化的最小值
    MeshFlags options
);
以下就是核心的代码:
private void LoadMesh(string file)
{
    ExtendedMaterial[] mtrl;
    GraphicsStream adj;
 
    // Load our mesh
    using(Mesh mesh = Mesh.FromFile(file, MeshFlags.Managed, device,
                out adj, out mtrl))
    {
 
        // If we have any materials, store them
        if ((mtrl != null) && (mtrl.Length > 0))
        {
            meshMaterials = new Material[mtrl.Length];
            meshTextures = new Texture[mtrl.Length];
 
            // Store each material and texture
            for (int i = 0; i < mtrl.Length; i++)
            {
                meshMaterials[i] = mtrl[i].Material3D;
                if ((mtrl[i].TextureFilename != null) &&
                    (mtrl[i].TextureFilename != string.Empty))
                {
                    // We have a texture, try to load it
                    meshTextures[i] = TextureLoader.FromFile(device,
                        @"..\..\" + mtrl[i].TextureFilename);
                }
            }
        }
 
        // Clean our main mesh
        using(Mesh tempMesh = Mesh.Clean(mesh, adj, adj))
        {
            // Create our progressive mesh
            progressiveMesh = new ProgressiveMesh(tempMesh, adj,
                null, 1, MeshFlags.SimplifyVertex);
 
            // Set the initial mesh to the max
            progressiveMesh.NumberFaces = progressiveMesh.MaxFaces;
&nbs