Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
Have you tried the custom model importer before? Are you trying to add compatibility for more kinds of .obj models? –  ChrisC Feb 1 '13 at 23:00
@ChrisC: no, I didn't know anything about it –  BlackBear Feb 2 '13 at 12:50

1 Answer 1

up vote 3 down vote accepted

Alright, brand new here, so I couldn't comment to try and draw out information to help you out better...

I'm assuming your mesh looks like a bunch of triangles roughly at the right places but with some unsightly gaps? The shader hasn't been given the hint that it needs to draw the tri's continuously, so that is likely what is happening.

XNA has a class specifically for what you are trying to do, they call it MeshBuilder. MSDN page is here:


This will take the triangles your OBJ has defined and stitch them together in the way you're expecting. You just need to call StartMesh(), add the vertexes basically the same way you were in your code but with AddTriangleVertex(), and then FinishMesh() will return the MeshContent you want.

Hope this helps.

share|improve this answer
Worked perfectly, thank you! :) –  BlackBear Feb 2 '13 at 12:38

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.