0

I already looked at http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF here they explain how to draw 2D lines in xna, but i get an exeption(see script)

            {
                int points = 3;//I tried different values(1,2,3,4)
                VertexPositionColor[] primitiveList = new VertexPositionColor[points];

                for (int x = 0; x < points / 2; x++)
                {
                    for (int y = 0; y < 2; y++)
                    {
                        primitiveList[(x * 2) + y] = new VertexPositionColor(
                            new Vector3(x * 100, y * 100, 0), Color.White);
                    }
                }
                // Initialize an array of indices of type short.
                short[] lineListIndices = new short[(points * 2) - 2];

                // Populate the array with references to indices in the vertex buffer
                for (int i = 0; i < points - 1; i++)
                {
                    lineListIndices[i * 2] = (short)(i);
                    lineListIndices[(i * 2) + 1] = (short)(i + 1);
                }
                GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
                    PrimitiveType.LineList,
                    primitiveList,
                    0,  // vertex buffer offset to add to each element of the index buffer
                    8,  // number of vertices in pointList
                    lineListIndices,  // the index buffer
                    0,  // first index element to read
                    7   // number of primitives to draw
                    ); <---This parameter must be a valid index within the array. Parameter name: primitiveCount
            }

but i have no idea on how to fix this, since this is my first time making 2d graphics using 3d rendering

Background:

im making an 2d game engine for xna, and i want to make a class for drawing simple figures, i already know you can use a 1x1 Texture2D pixel trick for drawing, but i want to know this way as well, because else the CPU will be doing all of the calculations while the GPU can easily handle this.

1

2 Answers 2

0

Since you really didn't provide an error or anything, I'm just going to show you how I draw them.

I use this extension method for drawing a single line, you will need a white 1x1 texture

public static void DrawLine(this SpriteBatch spriteBatch, Vector2 begin, Vector2 end, Color color, int width = 1)
{
    Rectangle r = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length()+width, width);
    Vector2 v = Vector2.Normalize(begin - end);
    float angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX));
    if (begin.Y > end.Y) angle = MathHelper.TwoPi - angle;
    spriteBatch.Draw(Pixel, r, null, color, angle, Vector2.Zero, SpriteEffects.None, 0);
}

This will also draw a shape made of points, closed defines if the shape should be closed or not

    public static void DrawPolyLine(this SpriteBatch spriteBatch, Vector2[] points, Color color, int width = 1, bool closed = false)
    {


        for (int i = 0; i < points.Length - 1; i++)
            spriteBatch.DrawLine(points[i], points[i + 1], color, width);
        if (closed)
            spriteBatch.DrawLine(points[points.Length - 1], points[0], color, width);

    }
0

The first thing is the definition of your vertices. This looks a bit weird. If points is 3, then the outer loop goes from 0..0 and the inner loop from 0..1. So you have two vertices. There is only one line you can draw with those points. If you specify points = 4, then you actually have four points.

It seems that you want to draw a continuous line with no repeating vertices. So you don't really need an indexed representation (with an index buffer). Specifying the vertices one after another would be enough.

In order to draw a continuous line you should use PrimitiveType.LineStrip. This will automatically connect the vertices with lines. So you need points - 1 indices (if you really want to use them).

In the call to DrawUserIndexedPrimitives() there are some wrong arguments that cause the exception:

  1. You say number of vertices is 8, which is definitely not true. It's (points / 2) * 2.
  2. You say number of primitives is 7, which is not true either. This should be points - 1.

But again, your definition of vertices and indices is not consistent. You have to correct this before you go on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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