- Main Page
- Packages
- Classes
- Files

- File List
- File Members

# D:/guillaume/guillara/3D/DXGfx/DXGfxLib/MeshRessource.cs

Go to the documentation of this file.

    00001 using System;
    00002 using System.Collections.Generic;
    00003 using System.Text;
    00004 using Microsoft.DirectX;
    00005 using Microsoft.DirectX.Direct3D;
    00006 using Microsoft.Samples.DirectX.UtilityToolkit;
    00007
    00008 namespace DXGfxLib
    00009 {
    00019     public class MeshRessource
    00020     {
    00024         string name;
    00025
    00029         string fileName;
    00030
    00034         protected Mesh originalMesh = null;
    00035
    00039         protected Mesh localMemMesh = null;
    00040
    00045         protected ExtendedMaterial[] extendedMaterials;
    00046
    00050         protected EffectInstance[] effectIntances;
    00051
    00052         protected GraphicsStream adjacency = null;
    00053
    00058         public Material[] meshMaterials;
    00059
    00063         public Texture[] meshTextures;
    00064
    00068         public Effect[] effects;
    00069
    00073         public float bSphereRadius = 1.0f;
    00074
    00078         public Vector3 barycenter = new Vector3();
    00079
    00083         public BBox localBoundingBox = new BBox();
    00084
    00088         public Vector3 extents = new Vector3();
    00089
    00095         public MeshRessource(string fileName)
    00096         {
    00097             if (DXGfxManager.GetGlobalInstance().d3dDevice == null)
    00098             {
    00099                 throw new Exception("Manager does not have any reference to any valid d3ddevice, this constructor can not be executed");
    00100             }
    00101             LoadFromFile(DXGfxManager.GetGlobalInstance().d3dDevice, fileName);
    00102         }
    00103
    00109         public MeshRessource(Device d3ddevice, string fileName)
    00110         {
    00111             LoadFromFile(d3ddevice, fileName);
    00112         }
    00113
    00120         public void LoadFromFile(Device d3ddevice, string fileName)
    00121         {
    00122             this.fileName = fileName;
    00123             this.name = StringUtil.GetName(fileName);
    00124
    00125             originalMesh = Mesh.FromFile(fileName, MeshFlags.SystemMemory, d3ddevice, out adjacency, out extendedMaterials, out effectIntances);
    00126
    00127             originalMesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeCompact |
    00128                     MeshFlags.OptimizeAttributeSort, adjacency);
    00129
    00130             originalMesh.ComputeNormals();
    00131
    00132             localMemMesh = originalMesh.Clone((originalMesh.Options.Value & ~MeshFlags.SystemMemory),
    00133                 originalMesh.VertexFormat, d3ddevice);
    00134
    00135             // We need to extract the material properties and texture names
    00136             meshTextures = new Texture[extendedMaterials.Length];
    00137             meshMaterials = new Material[extendedMaterials.Length];
    00138             for (int i = 0; i < extendedMaterials.Length; i++)
    00139             {
    00140                 meshMaterials[i] = extendedMaterials[i].Material3D;
    00141                 // Set the ambient color for the material (D3DX does not do this)
    00142                 meshMaterials[i].Ambient = meshMaterials[i].Diffuse;
    00143
    00144                 // Create the texture
    00145                 meshTextures[i] = ResourceCache.GetGlobalInstance().CreateTextureFromFile(d3ddevice,
    00146                                                                                   extendedMaterials[i].TextureFilename);
    00147             }
    00148
    00149             localBoundingBox = CalculateLocalBBox(originalMesh);
    00150             extents = localBoundingBox.Size();
    00151
    00152             // Find the mesh's center, then generate a centering matrix
    00153             using (VertexBuffer vb = originalMesh.VertexBuffer)
    00154             {
    00155                 using (GraphicsStream stm = vb.Lock(0, 0, LockFlags.NoSystemLock))
    00156                 {
    00157                     try
    00158                     {
    00159                         this.bSphereRadius = Geometry.ComputeBoundingSphere(stm,
    00160                             originalMesh.NumberVertices, originalMesh.VertexFormat, out this.barycenter);
    00161                     }
    00162                     finally
    00163                     {
    00164                         vb.Unlock();
    00165                     }
    00166                 }
    00167             }
    00168         }
    00169
    00176         public virtual void Draw(Device d3ddevice, int subsetId, bool setMeshMaterial)
    00177         {
    00178             if (setMeshMaterial)
    00179             {
    00180                 d3ddevice.Material = meshMaterials[subsetId];
    00181                 d3ddevice.SetTexture(0, meshTextures[subsetId]);
    00182             }
    00183             localMemMesh.DrawSubset(subsetId);
    00184         }
    00185
    00191         public virtual void Draw(Device d3ddevice, int subsetId)
    00192         {
    00193             Draw(d3ddevice, subsetId, true);
    00194         }
    00195
    00200         public virtual void Draw(Device d3ddevice)
    00201         {
    00202             for (int i = 0; i < meshMaterials.Length; i++)
    00203             {
    00204                 Draw(d3ddevice, i);
    00205             }
    00206         }
    00207
    00213         public BBox CalculateLocalBBox(Mesh meshToUse)
    00214         {
    00215             return AABBox.CalculateLocalBBox(meshToUse);
    00216         }
    00217
    00223         public Mesh GetDXMesh()
    00224         {
    00225             return originalMesh;
    00226         }
    00227     }
    00228 }