Help with loading model files

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
andrewnd
Posts: 3
Joined: Sun Dec 19, 2010 7:16 pm

Help with loading model files

Post by andrewnd »

Hello,

I just started trying out this physics engine and have had no problem getting the built-in Box type to render. The problem for me comes when trying to load in a simple box model I have made in Blender. When I run the program I get an exception on the following line:
StaticTriangleGroup.StaticTriangleGroupVertex[] vertices;
int[] indices;
StaticTriangleGroup.GetVerticesAndIndicesFromModel(boxModel, out vertices, out indices);
var mesh = new TriangleMesh(vertices, indices);
mesh.WorldMatrix = Matrix.CreateTranslation(new Vector3(0, 0, 0));
var group = new StaticTriangleGroup(mesh);
game.gameSpace.Add(group);

game.modelDrawer.Add(mesh);

The exception is Unsupported vertex type in mesh. I'm not really sure what is happening. The cube model is about as simple as it gets! Any help would be appreciated so I can better understand loading custom models. Thanks!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Help with loading model files

Post by Norbo »

The GetVerticesAndIndicesFromModel method is just a convenience method that grabs an index and vertex buffer from a content pipeline-loaded model. It was recently changed to support only few vertex types uniformly across all platforms. The old system handled more formats, but didn't work properly on the WP7 hardware due a known bug in one of the XNA VertexBuffer.GetData methods.

The convenience method will probably be improved in the future, but it's not a high priority since it doesn't do anything special (and isn't really related to physics :)). Usually, I just recommend using a custom content processor to extract vertices. An example of such a processor can be found in the Triangle Picking sample on the XNA website.

The mesh just wants a vertex and index list, it doesn't care where they come from :)
andrewnd
Posts: 3
Joined: Sun Dec 19, 2010 7:16 pm

Re: Help with loading model files

Post by andrewnd »

Thanks for the reply. I will check out that example on the XNA site and see if I can get it working!
Spankenstein
Posts: 249
Joined: Wed Nov 17, 2010 1:49 pm

Re: Help with loading model files

Post by Spankenstein »

Extracting the indices from a model is as follows:

Code: Select all

                // Load your model
		Model model = Game.Content.Load<Model>(assetName);	

                // Loop through each mesh and mesh part
		// MESH = model.Meshes[n];
		// MESHPART = model.Meshes[n].MeshParts[n];

                // Extract the indices
		int index = meshPart.StartIndex;

                int numTriangles = meshPart.PrimitiveCount;

                modelExtractData.indices = new ushort[numTriangles * 3];

                mesh.IndexBuffer.GetData<ushort>(
                    index * 2,
                    modelExtractData.indices,
                    0,
                    numTriangles * 3
                    );
Hope that helps :)
andrewnd
Posts: 3
Joined: Sun Dec 19, 2010 7:16 pm

Re: Help with loading model files

Post by andrewnd »

This helps a bit, but I'm still a little lost. Can you explain to me what your modelExtractData is? And how the indices are being stored in there? Thanks!
Spankenstein
Posts: 249
Joined: Wed Nov 17, 2010 1:49 pm

Re: Help with loading model files

Post by Spankenstein »

'ModelExtractData' is a simple class. It has a property called 'indices', which is an array:

Code: Select all

ushort[] indices;
This line sets the array size:

Code: Select all

modelExtractData.indices = new ushort[numTriangles * 3];
Which is the same as:

Code: Select all

ushort[] indices = new ushort[numTriangles * 3];
Only I'm accessing the indices array through the class as opposed to directly.
Post Reply