[MDX] Run time error when the mesh is simplified, Please Help!

Started by
2 comments, last by cider123 18 years, 6 months ago
Hi, I am leanring MDX using Tom Miller's "Managed DirectX® 9 Kick Start: Graphics and Game Programming". The source code of chapter 7 he provided inclues a code listed below. However, the line:

     Mesh tempMesh = Mesh.Clean(mesh, adj, adj);


produces a compile error since Clean does not take 3 parameters. It seems due to the recent update of DirectX. However, the line:

     simplifiedMesh = Mesh.Simplify(mesh, adj, null, 1, MeshFlags.SimplifyVertex);


produces a runtime error with an error message: Additional information: Object reference not set to an instance of an object. Can anyone tell me what happened and how to fix it? Many Thanks, ninjaindark

private void LoadMesh(string file)
{
     ExtendedMaterial[] mtrl;
     GraphicsStream adj;

     // Load our 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 = mtrl.Material3D;
              if ((mtrl.TextureFilename != null) && (mtrl.TextureFilename != string.Empty))
              {
                   // We have a texture, try to load it
                   meshTextures = TextureLoader.FromFile(device, @"..\..\" + mtrl.TextureFilename);
              }
         }
      }

      // Clean our main mesh
      Mesh tempMesh = Mesh.Clean(mesh, adj, adj);
            // Replace our existing mesh with this one
      mesh.Dispose();
      mesh = tempMesh;

      // Get our new simplified mesh
      simplifiedMesh = Mesh.Simplify(mesh, adj, null, 1, MeshFlags.SimplifyVertex);

      // Save our simplified mesh for later use
      // First we need to generate the adjacency for this mesh
      int[] simpleAdj = new int[simplifiedMesh.NumberFaces * 3];
      simplifiedMesh.GenerateAdjacency(0.0f, simpleAdj);
      using (Mesh cleanedMesh = Mesh.Clean(CleanType.Simplification, simplifiedMesh,simpleAdj, out simpleAdj))
      {
          cleanedMesh.Save(@"..\..\simple.x", simpleAdj, 
                    mtrl, XFileFormat.Text);
      }
      Console.WriteLine("Number of vertices in original mesh: {0}", mesh.NumberVertices);
      Console.WriteLine("Number of vertices in simplified mesh: {0}", simplifiedMesh.NumberVertices);
}

Advertisement
First this I would do is go the www.thezbuffer.com and get the updated code for the Book. ZMan has updated toms book so that it now works with the latest SDK Release.
Mykre - BlogVirtual Realm :- XNA News and Resources from Down Under** For those Interested in an Australian XNA User Group Contact me though my site.
Thanks. I have missed a lot of resources. :)

ninjaindark
I grabbed the latest updates which notes they work with June / August 2005 SDKs.

I'm running the Visual C# Express Beta 2 while clawing my way through these chapters using the DirectX 9 August 2005 SDK.

In Chapter 7, the first stop was changing the Simplifying Existing Meshes:

From:// Clean our main meshMesh tempMesh = Mesh.Clean(mesh, adj, adj);To:// Clean our main meshMesh tempMesh = Mesh.Clean(CleanType.Simplification, mesh, adj, adj);


However, the next stop I'm stuck at:

// Get our new simplified meshsimplifiedMesh = Mesh.Simplify(mesh, adj, null, 1, MeshFlags.SimplifyVertex);It returns the following error:System.AccessViolationException was unhandled  Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."  Source="Microsoft.DirectX.Direct3DX"  StackTrace:       at D3DXSimplifyMesh(ID3DXMesh* , UInt32* , _D3DXATTRIBUTEWEIGHTS* , Single* , UInt32 , UInt32 , ID3DXMesh** )       at Microsoft.DirectX.Direct3D.Mesh.SimplifyMeshInternal(Mesh mesh, IntPtr adjacency, AttributeWeights& vertexAttributeWeights, IntPtr vertexWeights, Int32 minValue, MeshFlags options)       at Microsoft.DirectX.Direct3D.Mesh.Simplify(Mesh mesh, GraphicsStream adjacency, GraphicsStream vertexWeights, Int32 minValue, MeshFlags options)       at Chapter7Code.Form1.LoadMesh(String file) in C:\Program Files\Sams Publishing\MDXKickStart\C# Code\Chapter 7\SimplifyMesh\Form1.cs:line 94       at Chapter7Code.Form1.InitializeGraphics() in C:\Program Files\Sams Publishing\MDXKickStart\C# Code\Chapter 7\SimplifyMesh\Form1.cs:line 58       at Chapter7Code.Form1.Main() in C:\Program Files\Sams Publishing\MDXKickStart\C# Code\Chapter 7\SimplifyMesh\Form1.cs:line 217       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)       at System.Threading.ThreadHelper.ThreadStart()



I understand the concept behind it, just looking for a working solution. As I read further into the book, it looks like maybe welding vertices or patching meshes might be more ideal. Just looking to be well rounded when learning this stuff.

[Edited by - cider123 on October 3, 2005 12:01:38 PM]

This topic is closed to new replies.

Advertisement