I am trying to extend XNA's content pipeline in order to import a model in .obj format (a very simple and text-based format, see here for details). Writing a full extension library seems like an overkill to me so I decided I would only create a custom importer and a processor which returns a ModelContent so that I could use the default writer and reader.
My processor sort of works but not exacly as expected: what should look like an aircraft is actually a bunch of triangles stick together! Before writing this I had a simple class derived from DrawableGameComponent which loaded the model in its LoadContent, but that's not exacly XNA-ish. The point is that the code worked, so there must be a problem in the construction of the MeshContent.
I have followed the pattern I found in this question during my researches.
Here's the relevant part:
[ContentProcessor( DisplayName = "dotObjProcessor" )]
public class dotObjProcessor : ContentProcessor<dotObjContent, ModelContent> {
public override ModelContent Process( dotObjContent input,
ContentProcessorContext context ) {
var mc = new MeshContent( );
foreach ( dotObjContent.Vertex p in input.Vertices )
mc.Positions.Add( new Vector3( p.X, p.Y, p.Z ) );
var gc = new GeometryContent( );
foreach ( dotObjContent.Face f in input.Faces ) {
gc.Vertices.Add( f.V1 );
gc.Vertices.Add( f.V2 );
gc.Vertices.Add( f.V3 );
gc.Indices.Add( f.V1 );
gc.Indices.Add( f.V2 );
gc.Indices.Add( f.V3 );
}
mc.Geometry.Add( gc );
MeshHelper.CalculateNormals( mc, true );
return context.Convert<MeshContent, ModelContent>( mc, "ModelProcessor" );
}
}
A dotObjContent is the output of my importer, it simply contains two collections for vertices and indices.