problem loading mesh from .X file.

Hey guys... I'm having some trouble loading a mesh from a .X file in C#. I'm using the same code that I see pretty much everywhere as sample:

private Material[] meshMaterials;
private Texture[] meshTextures;
private Mesh mesh = null;

private void LoadMesh(string file)
{

//LOAD X FILE
ExtendedMaterial[] mtrl;

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

The problem is in the Mesh.FromFile line, I get a System Error because of a Null Object Reference. I've debugged to check, and the only thing that's null at that point is mesh. (I ruled out mtrl, because I tried the following two lines also):

mesh = Mesh.FromFile(file, MeshFlags.Managed, device);
mesh = Mesh.Box(device, 2.0f, 2.0f, 2.0f);

Both of them give me the same error as well.

None of the sample code I've seen ever shows mesh being initialized to anything other than null... and the constructor for Mesh takes arguments like the number of vertices, etc.

Any ideas what could be the problem here


Answer this question

problem loading mesh from .X file.

  • Howard Wilton

    Since mesh is on the left hand side you can't get a null object reference becuase of it. Given the other 2 lines also fail I have to suspect that its a problem with the device parameter. You say its not null though which is odd. What do you see if you bring up device in a watch window, can you expend its properties etc



  • ozhonetech

    Well... I'm not sure why it was failing on that line. I played with the code some more and discovered that a texture wasn't loading correctly, so later when I ran my DrawMesh code it failed with a null reference. That doesn't explain to me at all why it was stopping in the debugger at the mesh = Mesh.Box (or whatever) line, but commenting out the later texture code in the draw mesh allowed the mesh to be displayed.

  • Rclip

    Yes, I can expand it out. The main value shows {Microsoft.DirectX.Direct3D.Device}
    and it has many properties under it, AvailableTextureMemory, ClipPlanes, ClipStatus, Indices, Lights, etc... just to name a few.

    Though I just noticed that the Pixel and Vertex Shader properties of the device are null... how do I explicitly tell DirectX to not use these, or will they automatically not be used since they're null

  • JoshKraker

    null in those means that there is no shader, that is correct.

    My only guess is that something is not set up properly with the device since Mesh.Box() is failing. But I can't imagine what. Can you post the device creation code you use

    You can convince yourself its not the mesh varialbe by just using removing the 'mesh =' from this line. It will not make any sense but the code will compile and run and probably still fail in this line showing that it must be an issue with the device.

    mesh = Mesh.Box(device, 2.0f, 2.0f, 2.0f);

    becomes

    Mesh.Box(device, 2.0f, 2.0f, 2.0f);



  • problem loading mesh from .X file.