1
\$\begingroup\$

I am trying to render the vertices of a scene with a cube I exported as an OBJ from Blender. The 8 vertices become 24 when imported into XNA but when I render it I dont see all faces. This is not an issue of missing meshes because I'm pretty sure this is being stored as the first and only mesh. I suspect this is because of the vectors defined in the Assimp Mesh object but I can't seem to find any help for this.

THIS IS SOLVED! The code shows how to render a 3D model using only Assimp and XNA with vertex and index buffers. oScene is the Scene object imported using Assimp. This code only draws faces in a single color without textures.

Here is the relevant code:

   private void SetUpVertices()
    {
        mMesh = oScene.Meshes[0];
        vertices = new VertexPositionColor[mMesh.VertexCount];
        indices = new  short[mMesh.FaceCount*3];

        int i=0;
        foreach (Vector3D mVec in mMesh.Vertices)
        {
            vertices[i] = new VertexPositionColor(new Vector3(mVec.X, mVec.Y, mVec.Z), Color.Red);
            i++;
        }

        int f = 0;
        Face mFace;
        for (i = 0 ; i < mMesh.FaceCount*3 ; i=i+3) 
        {
            mFace = mMesh.Faces[f];
            f++;
            indices[i] = (short)mFace.Indices[0];
            indices[i + 1] = (short)mFace.Indices[1];
            indices[i + 2] = (short)mFace.Indices[2];

        }

        vertexBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
        vertexBuffer.SetData(vertices);

        indexBuffer = new IndexBuffer(device, typeof(short), indices.Length, BufferUsage.None);
        indexBuffer.SetData(indices);

    }

    private void SetUpCamera()
    {
        cameraPos = new Vector3(0, 5, 9);
        viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 1), new Vector3(0, 1, 0));
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 200.0f);
    }

    protected override void Draw(GameTime gameTime)
    {
        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);
        effect = new BasicEffect(GraphicsDevice);
        effect.VertexColorEnabled = true;

        effect.View = viewMatrix;
        effect.Projection = projectionMatrix;
        effect.World = Matrix.Identity;

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            device.SetVertexBuffer(vertexBuffer);
            device.Indices = indexBuffer;
            device.DrawIndexedPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleList, 0, 0, oScene.Meshes[0].VertexCount, 0, mMesh.FaceCount);
        }

        base.Draw(gameTime);
    }
\$\endgroup\$
2
  • \$\begingroup\$ I just noticed that using the MakeLeftHanded PostProcessStep generates more triangles but still not all. \$\endgroup\$ May 8, 2014 at 3:13
  • \$\begingroup\$ I also set the RasterizerState Cullmode to None and my cube and uvspheres render fine now but more complex shapes like cylinders still have issues. \$\endgroup\$ May 8, 2014 at 3:46

1 Answer 1

0
\$\begingroup\$
device.DrawPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleStrip, 0, oScene.Mesh[0].VertexCount);

I believe the problem may be that you are using TriangleStrip instead of TriangleList. It also primarily seems to be the draw call as well (based on my own quick test using your draw call). Try this:

device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, oScene.Mesh[0].VertexCount, 0, triangleCount);

You'll have to set triangleCountfrom the oScene.Mesh[0] object (don't know what it is in Assimp, sorry). I'm currently using a draw call like this, and .OBJ files loaded from my own .OBJ importer work fine.

\$\endgroup\$
6
  • \$\begingroup\$ Yes. An index buffer does seem to help but I must not be generating one properly because I'm still not getting all faces. I'm using triangulation to make sure the number of Indices for each face is always 3 but there is something wrong with my index generation. could you take a look at it please? I've modified the code in my original question. \$\endgroup\$ May 8, 2014 at 10:46
  • \$\begingroup\$ Btw, TriangleStrip generates a LOT more faces than TriangleList and also I'm doing this with CullMode.None turned on. \$\endgroup\$ May 8, 2014 at 10:56
  • \$\begingroup\$ Could you re-include your original code so I can see how everything connects together now? It also helps anyone else who happens to have a look. \$\endgroup\$
    – Seta
    May 8, 2014 at 11:09
  • \$\begingroup\$ I made some changes to the code and changed to TriangleList. Now it works! Thanks you! \$\endgroup\$ May 8, 2014 at 11:25
  • \$\begingroup\$ I am including the relevant portions of this solutions in the code above for anyone who is interested. I know there is too little info on how to integrate XNA with Assimp without using OpenGL or DirectX out there. Or its in bits and pieces on different sites. \$\endgroup\$ May 8, 2014 at 11:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .