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);
}