【问题标题】:XNA - Drawing 2D linesXNA - 绘制二维线
【发布时间】:2013-06-24 12:19:09
【问题描述】:

我已经看过http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF 在这里他们解释了如何在 xna 中绘制 2D 线,但我得到了一个例外(见脚本)

            {
                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
            }

但我不知道如何解决这个问题,因为这是我第一次使用 3d 渲染制作 2d 图形

背景:

我正在为 xna 制作一个 2d 游戏引擎,我想制作一个用于绘制简单图形的类, 我已经知道您可以使用 1x1 Texture2D 像素技巧进行绘图,但我也想知道这种方式,因为否则 CPU 将完成所有计算,而 GPU 可以轻松处理。

【问题讨论】:

标签: 3d xna 2d vertex-array


【解决方案1】:

由于您确实没有提供错误或任何内容,因此我将向您展示我是如何绘制它们的。

我使用这种扩展方法来绘制一条线,你需要一个白色的 1x1 纹理

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

这也会绘制一个由点组成的形状,closed 定义该形状是否应该闭合

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

    }

【讨论】:

    【解决方案2】:

    首先是顶点的定义。这看起来有点奇怪。如果points3,则外循环来自0..0,内循环来自0..1。所以你有两个顶点。你只能用这些点画一条线。如果你指定points = 4,那么你实际上有四个点。

    您似乎想绘制一条没有重复顶点的连续线。所以你真的不需要索引表示(带有索引缓冲区)。一个接一个地指定顶点就足够了。

    为了画一条连续的线,你应该使用PrimitiveType.LineStrip。这将自动将顶点与线连接起来。所以你需要points - 1 索引(如果你真的想使用它们)。

    在对DrawUserIndexedPrimitives() 的调用中存在一些导致异常的错误参数:

    1. 你说顶点数是8,这绝对不是真的。我是(points / 2) * 2
    2. 您说基元数是 7,这也不是真的。这应该是points - 1

    但同样,您对顶点和索引的定义并不一致。在继续之前,您必须更正此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 2016-07-20
      • 1970-01-01
      • 2013-02-27
      相关资源
      最近更新 更多