/// <summary> /// Read a mesh from an X file, and load the textures which are /// assumed to be in the same directory. /// </summary> public static AutoMesh LoadFromXFile(string path, MeshFlags flags, Direct3d d3d) { ExtendedMaterial[] extendedMaterials; String []fileNames; AutoMesh mesh = new AutoMesh(d3d, Mesh.FromFile(path, MeshFlags.SystemMemory, d3d.Dx, out extendedMaterials)); mesh.mTextures = new AutoTexture[extendedMaterials.Length]; mesh.mMaterials = new Material[extendedMaterials.Length]; fileNames = new string[extendedMaterials.Length]; // Load all the textures for this mesh for (int i = 0; i < extendedMaterials.Length; i++) { if (extendedMaterials[i].TextureFilename != null) { string textureFileName = System.IO.Path.Combine( System.IO.Path.GetDirectoryName(path), extendedMaterials[i].TextureFilename); fileNames[i] = textureFileName; // Scan to see if we already have this texture bool alreadyHaveTexture = false; for (int j = 0; j < i; j++) if (textureFileName == fileNames[j]) { mesh.mTextures[i] = mesh.mTextures[j]; alreadyHaveTexture = true; break; } // Load texture (if we don't already have it) if (!alreadyHaveTexture) mesh.mTextures[i] = new AutoTexture(d3d, TextureLoader.FromFile(d3d.Dx, textureFileName)); } mesh.mMaterials[i] = extendedMaterials[i].Material3D; mesh.mMaterials[i].Ambient = mesh.mMaterials[i].Diffuse; } return mesh; }
/// <summary> /// Cone the mesh (and textures that it contains), /// optionally converting the vertex and texture format. /// </summary> public AutoMesh Clone(Direct3d d3d, MeshFlags flags, VertexFormats vertexFormat, Format textureFormat, Usage usage, Pool pool) { // Clone the mesh vertex info Mesh mesh = mMesh.Clone(flags, vertexFormat, d3d.Dx); AutoMesh autoMesh = new AutoMesh(d3d, mesh); // Clone AutoMesh variables autoMesh.Tag = Tag; // Clone textures and materials if (mTextures.Length != 0) { // Clone materials autoMesh.mMaterials = new Material[mMaterials.Length]; for (int i = 0; i < mMaterials.Length; i++) autoMesh.mMaterials[i] = mMaterials[i]; // Clone textures autoMesh.mTextures = new AutoTexture[mTextures.Length]; for (int i = 0; i < mTextures.Length; i++) if (mTextures[i] != null) { // Already cloned this texture? bool alreadyConvertedTexture = false; for (int j = 0; j < i; j++) if (mTextures[i] == mTextures[j]) { alreadyConvertedTexture = true; autoMesh.mTextures[i] = autoMesh.mTextures[j]; break; } // Clone new texture if (!alreadyConvertedTexture) autoMesh.mTextures[i] = mTextures[i].Clone(d3d, textureFormat, usage, pool); } } return autoMesh; }
/// <summary> /// User wants to add normals to the mesh /// </summary> private void buttonAddNormals_Click(object sender, EventArgs e) { AutoMesh mesh = mMesh.Clone(d3dModel, MeshFlags.Managed, mMesh.M.VertexFormat | VertexFormats.Normal, Format.A8R8G8B8, Usage.AutoGenerateMipMap, Pool.Managed); mesh.M.ComputeNormals(); mMesh.Dispose(); mMesh = mesh; DisplayMeshInfo(); }
public void viewGrid(Gosub.Direct3d d3d, Microsoft.DirectX.Direct3D.Device dx, occupancygrid grid) { bool ground_plane_drawn = false; mCellMesh = new AutoMesh(d3d, Mesh.Box(dx, 1, 1, 1)); //show the grid cells for (int z = grid.dimension-1; z >= 0 ; z--) { int plane_hits = 0; for (int x = 1; x < grid.dimension - 1; x++) { for (int y = 1; y < grid.dimension-1; y++) { if (grid.display_cell[x, y, z]) { plane_hits++; occupancyGridCell c = grid.cell[x, y, z]; int r = 0; int g = 0; int b = 255; if (c != null) { r = c.colour[0]; g = c.colour[1]; b = c.colour[2]; } dx.Transform.World = Matrix.Translation(-grid.dimension / 2, -grid.dimension / 2, -grid.dimension / 2) //Center model //* Matrix.Scaling(1, 1, 1) // Make it bigger //* Matrix.RotationYawPitchRoll(0, 0, 0) * Matrix.Translation(grid.dimension - 1 - x, y, z) * Matrix.RotationYawPitchRoll(0, tilt, 0); // Then move it where you want dx.Material = GraphicsUtility.InitMaterial(Color.FromArgb(r, g, b)); mCellMesh.M.DrawSubset(0); } //for (int z = 0; z < grid.dimension; z++) { if (z == grid.dimension / 2) { if ((x == 1) || (x == grid.dimension - 2) || (y == 1) || (y == grid.dimension - 2)) { dx.Transform.World = Matrix.Translation(-grid.dimension / 2, -grid.dimension / 2, -grid.dimension / 2) //Center model * Matrix.Translation(x, y, z) * Matrix.RotationYawPitchRoll(0, tilt, 0); // Then move it where you want dx.Material = GraphicsUtility.InitMaterial(Color.Green); mCellMesh.M.DrawSubset(0); } } } } } if ((plane_hits > 30) && (!ground_plane_drawn)) { ground_plane_drawn = true; for (int x = 1; x < grid.dimension - 1; x++) { for (int y = 1; y < grid.dimension - 1; y++) { if (grid.empty[x, y]) { //occupancyGridCell c = grid.cell[x, y, z]; int r = 0; int g = 0; int b = 255; dx.Transform.World = Matrix.Translation(-grid.dimension / 2, -grid.dimension / 2, -grid.dimension / 2) //Center model //* Matrix.Scaling(1, 1, 1) // Make it bigger //* Matrix.RotationYawPitchRoll(0, 0, 0) * Matrix.Translation(grid.dimension - 1 - x, y, z) * Matrix.RotationYawPitchRoll(0, tilt, 0); // Then move it where you want dx.Material = GraphicsUtility.InitMaterial(Color.FromArgb(r, g, b)); mCellMesh.M.DrawSubset(0); } } } } } }
/// <summary> /// Setup the form by loading the mesh /// </summary> private void FormViewMesh_Shown(object sender, EventArgs e) { // ToDo: Show a message form while loading AutoMesh autoMesh = null; // Load the mesh (or fail and exit) try { autoMesh = AutoMesh.LoadFromXFile(mFileName, MeshFlags.SystemMemory, d3dModel); } catch { // Dispose whatever we have try { autoMesh.Dispose(); } catch { } autoMesh = null; MessageBox.Show(this, "Error loading mesh"); Hide(); return; } // Detect unsupported texture formats VertexFormats format = autoMesh.M.VertexFormat; if ( (format & VertexFormats.TextureCountMask) != VertexFormats.Texture0 && (format & VertexFormats.Texture1) != VertexFormats.Texture1 ) { MessageBox.Show(this, "Multiple textures not supported"); autoMesh.Dispose(); Hide(); return; } // Unsupported vertex formats VertexFormats unsupported = VertexFormats.Specular | VertexFormats.LastBetaD3DColor | VertexFormats.LastBetaUByte4 | VertexFormats.PointSize | VertexFormats.Transformed; if ( (int)(format & unsupported) != 0) { MessageBox.Show(this, "Unsupported vertex format"); autoMesh.Dispose(); Hide(); return; } // No position (sometimes this happens) if ( (format & VertexFormats.Position) != VertexFormats.Position ) { MessageBox.Show(this, "Unsupported vertex format (no position detected!)"); autoMesh.Dispose(); Hide(); return; } // Clone to new format, using 32 bit pixel format. try { mMesh = autoMesh.Clone(d3dModel, MeshFlags.Managed, autoMesh.M.VertexFormat, Format.A8R8G8B8, Usage.AutoGenerateMipMap, Pool.Managed); } catch { MessageBox.Show(this, "Error cloning mesh"); try { autoMesh.Dispose(); } catch { } try { mMesh.Dispose(); } catch { } Hide(); return; } autoMesh.Dispose(); autoMesh = null; // Display mesh info DisplayMeshInfo(); }