Jump to content
  • Advertisement
Sign in to follow this  
ninjaindark

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

This topic is 4791 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 mesh
Mesh tempMesh = Mesh.Clean(mesh, adj, adj);

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




However, the next stop I'm stuck at:


// Get our new simplified mesh
simplifiedMesh = 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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!