【发布时间】: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 吗?它已经停产了。
-
我必须在学校学习这个,他们在我学习后的第二年改变了它:\