【问题标题】:XNA DrawPrimitives unintended line to 0,0XNA DrawPrimitives 意外行到 0,0
【发布时间】:2016-06-15 02:32:11
【问题描述】:

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

截图

绘制代码

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

【问题讨论】:

  • 还是 XNA 吗?它已经停产了。
  • 我必须在学校学习这个,他们在我学习后的第二年改变了它:\

标签: c# xna


【解决方案1】:

这是有问题的行:

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

【讨论】:

    【解决方案2】:

    您应该查看 MSDN 上的 PrimitiveBatch sample。它有点旧,但会让您大致了解如何处理原语。我自己一直在将它用于我目前正在开发的游戏中,我喜欢它的结果。

    您可以使用它来绘制线条并用它们制作更复杂的形状。在代码方面也应该更简洁。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2016-06-03
      • 2018-10-24
      • 2015-05-11
      • 1970-01-01
      • 2023-03-19
      • 2019-11-16
      相关资源
      最近更新 更多