1
\$\begingroup\$

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.

Any clues about this?

\$\endgroup\$
  • \$\begingroup\$ Can you include some more info on the object or tell us if there is any extra info before / after that error? \$\endgroup\$ – Tom 'Blue' Piddock Jul 1 '14 at 8:37
  • \$\begingroup\$ @Blue I've added a few more info. I'm new in 3d programming and was tinkering with this certain project. \$\endgroup\$ – Bahamut Jul 1 '14 at 9:09
  • \$\begingroup\$ Excellent - this is a lot better: more info will make it easier to provide an answer. \$\endgroup\$ – Tom 'Blue' Piddock Jul 1 '14 at 12:03
  • \$\begingroup\$ What is the actual type of and message associated with the exception? \$\endgroup\$ – Josh Jul 2 '14 at 15:02
  • \$\begingroup\$ 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. \$\endgroup\$ – Bahamut Jul 2 '14 at 17:18
0
\$\begingroup\$

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.

When you say the error only says "primitiveCount", that's must be the exception's description, while the type is ArgumentOutOfRangeException.

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.

Either the .obj file is corrupt, your loader doesn't support something in the file, or there's another possible exception that's not listed in the reference. I'll put my money on the broken loader.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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