XNA DrawPrimitives意外行到0,0

时间:2016-03-02 09:59:08

标签: c# xna

我正在尝试学习XNA。 我目前正在用户点击的屏幕上绘制一个数字。 我使用indexbuffer和PrimitiveType.LineList。 它工作正常,但偶尔在图形的第一个顶点和看起来像位置0,0之间画一条线。为什么这样做?

截图 enter image description here

绘制代码

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);

        #region Transform //Covered later in the Course
        effect.VertexColorEnabled = true;
        effect.World = Matrix.Identity;
        effect.View = new Matrix(
            1.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f, 0.0f,
            0.0f, 0.0f, -1.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 1.0f
        );
        effect.Projection = Matrix.CreateOrthographicOffCenter(
              0f, this.GraphicsDevice.DisplayMode.Width,
              this.GraphicsDevice.DisplayMode.Height, 0f,
            -10f, 10f
        );
        #endregion -> Wordt later behandeld
        if (vertex.Count > 0)
        {
            VertexBuffer vBuffer = new VertexBuffer(this.GraphicsDevice, typeof(VertexPositionColor), vertex.Count, BufferUsage.None);
            vBuffer.SetData<VertexPositionColor>(vertex.ToArray());

            this.GraphicsDevice.SetVertexBuffer(vBuffer);


            effect.CurrentTechnique.Passes[0].Apply();

            this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 0, 0, this.vertex.Count, 0, vertex.Count * 2);
        }

        base.Draw(gameTime);
    }

添加图代码

    private void addFigure(Microsoft.Xna.Framework.Point location, Size size, Microsoft.Xna.Framework.Color color)
    {
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X, location.Y - 40, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X + 10, location.Y - 20, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X + 20, location.Y - 20, 0), Microsoft.Xna.Framework.Color.Red));
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X + 20, location.Y - 10, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X + 40, location.Y, 0), Microsoft.Xna.Framework.Color.Red));
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X + 20, location.Y + 10, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X + 20, location.Y + 20, 0), Microsoft.Xna.Framework.Color.Red));
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X + 10, location.Y + 20, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X, location.Y + 40, 0), Microsoft.Xna.Framework.Color.Red));
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X - 10, location.Y + 20, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X - 20, location.Y + 20, 0), Microsoft.Xna.Framework.Color.Red));
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X - 20, location.Y + 10, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X - 40, location.Y, 0), Microsoft.Xna.Framework.Color.Red));
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X - 20, location.Y - 10, 0), Microsoft.Xna.Framework.Color.Red));

        this.vertex.Add(new VertexPositionColor(new Vector3(location.X - 20, location.Y - 20, 0), Microsoft.Xna.Framework.Color.Red));
        this.vertex.Add(new VertexPositionColor(new Vector3(location.X - 10, location.Y - 20, 0), Microsoft.Xna.Framework.Color.Red));

        for (short i = 16; i > 0; i--)
        {
            this.index.Add((short)(this.vertex.Count - i));
            if (i - 1 > 0)
            {
                this.index.Add((short)(this.vertex.Count - i + 1));
            }
            else
            {
                this.index.Add((short)(this.vertex.Count - 16));
            }
        }

        this.ib = new IndexBuffer(this.GraphicsDevice, typeof(short), this.index.Count, BufferUsage.None);
        this.ib.SetData<short>(this.index.ToArray());
        this.GraphicsDevice.Indices = ib;
    }

2 个答案:

答案 0 :(得分:0)

这是有问题的一行:

this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 
    0, 0, this.vertex.Count, 0, vertex.Count * 2);

您指定的行数是顶点的两倍。但你只有同样多。在缓冲区之后可能存在一些未初始化的垃圾,它被解释为原点。

应该是:

this.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.LineList, 
    0, 0, this.vertex.Count, 0, vertex.Count);

答案 1 :(得分:0)

您应该查看MSDN上的PrimitiveBatch sample。它有点旧,但会给你一个关于如何处理原语的一般概念。我自己一直在将这个用于我目前正在开发的游戏中,我喜欢它的结果。

您可以使用它来绘制线条并使用它们制作更复杂的形状。代码也应该更清晰。