# XNA Library DrawPrimitives throwing primitiveCount error

I downloaded the project from here http://silverlight.bayprince.com/tutorials.php?tutorial=13 and tried adding a new OBJ file with a few thousand vertices listed.

The classic teapot works but when I tried loading a different model with alot more vertices included, it suddenly throws this primitiveCount error when the DrawPrimitives gets called.

EDIT:

The code includes a class that would read the data from the OBJ file. This will then return a VertexBuffer object back to the main program. The drawing starts when the Draw event is called.

There are 417936 vertices created from the OBJ file and since I'm using a trianglelist, I divided the vertices by 3 to get the total primitives count.

here's the code for the draw event:

private void DrawingSurface_Draw(object sender, DrawEventArgs e)
{
GraphicsDevice device = GraphicsDeviceManager.Current.GraphicsDevice;

device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, new Microsoft.Xna.Framework.Color(0, 0, 0, 0), 10.0f, 0);

device.RasterizerState = new RasterizerState()
{
CullMode = CullMode.None
};

device.SetVertexBuffer(_vertexBuffer);

foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
{

pass.Apply();
device.SamplerStates[0] = SamplerState.LinearClamp;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, _vertexBuffer.VertexCount / 3);
}

// set camera
_effect.World = Matrix.Identity;
_effect.View = Matrix.CreateLookAt(new Vector3(_x, _y, _z), Vector3.Zero, Vector3.Up);
_effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 2.0f, 1.0f, 100.0f);

// move camera along a circumference
_x = (float)(_radius * Math.Sin(_hAngle * (Math.PI / 180)));
_z = (float)(_radius * Math.Cos(_hAngle * (Math.PI / 180)));

// move camera along a circumference
//_y = (float)(_radius * Math.Cos(_hAngle * (Math.PI / 180)));

e.InvalidateSurface();
}


then the error occurs on the DrawPrimitives.

• Can you include some more info on the object or tell us if there is any extra info before / after that error? – Tom 'Blue' Piddock Jul 1 '14 at 8:37
• @Blue I've added a few more info. I'm new in 3d programming and was tinkering with this certain project. – Bahamut Jul 1 '14 at 9:09
• Excellent - this is a lot better: more info will make it easier to provide an answer. – Tom 'Blue' Piddock Jul 1 '14 at 12:03
• What is the actual type of and message associated with the exception? – user1430 Jul 2 '14 at 15:02
• it only says primitiveCount. just that. it's not much of a clue but this never happens on simple model files like rendering a torus, cube, teapot etc. – Bahamut Jul 2 '14 at 17:18

According to the reference page for GraphicsDevice.DrawPrimitives(), it can throw one of two exceptions - ArgumentOutOfRangeException and InvalidOperationException - and only the former has anything to do with primitiveCount.
In other words, you're passing a number less than or equal to 0 as the third parameter, which means _vertexBuffer.VertexCount is less than or equal to 2 (probably 0), which means that .obj file with 417936 vertices probably didn't get loaded properly.