2012-06-18 31 views
0

我想在XNA中绘制一个正方形,并根据用户输入简单地用键盘移动它。不幸的是,我甚至没有能够让广场显示 - 我很积极,我错过了一些东西,但我无法弄清楚它到底是什么。我打算创建一个我可以在我的游戏中使用的复杂的原始形状层次结构。 Shape类的主要组件被初始化,并且派生类(在这种情况下为Rect)将这些组件用于进一步使用,并对其进行配置。如何在XNA中绘制和移动正方形?

我在这里做错了什么?

代码

Shape.cs

public abstract class Shape 
    { 
     public Vector3 Center; 
     public Color[] Colors; 
     public bool SetColorsOnUpdate; 

     public virtual Rectangle BoundingBox { get; set; } 
     public float Radius { get { return mRadius; } } 

     protected VertexBuffer mVertexBuf; 
     protected IndexBuffer mIndexBuf; 
     protected float mRadius; 
     protected BasicEffect mShader; 

     public Shape(Vector3 center, Color[] colors, int numVertices, float radius) 
     { 
      Colors = colors; 
      Center = center; 
      mVertexBuf = new VertexBuffer(Graphics.MainDevice, typeof(VertexPositionColor), numVertices, BufferUsage.WriteOnly); 
      mIndexBuf = new IndexBuffer(Graphics.MainDevice, IndexElementSize.SixteenBits, numVertices, BufferUsage.WriteOnly); 
      SetColorsOnUpdate = true; 
      mShader = new BasicEffect(Graphics.MainDevice); 
      mRadius = radius; 
     } 

     public abstract void Update(); 
     public abstract void Draw(); 
    } 

Rect.cs

public class Rect : Shape 
{ 
    public override Rectangle BoundingBox 
    { 
     get 
     { 
      int x = (int)Center.X, y = (int)Center.Y; 
      int diameter = (int)mRadius * 2; 

      return new Rectangle(
       x, y, 
       x + diameter, 
       y + diameter 
      ); 
     } 
    } 

    const float TestRectZCoordinate = 0; 
    const int NumVertices = 4; 

    public Rect(Vector3 center, Color[] colors, float radius) 
     : base(center, colors, NumVertices, radius) 
    { 
     if (colors.Length < NumVertices) 
      throw new IndexOutOfRangeException(string.Format("Color array passed to Rect constructor MUST have an element index size of 4. Current length passed is {0}", colors.Length)); 

     mShader.VertexColorEnabled = true; 

     mVertexBuf.SetData<VertexPositionColor>(
       new VertexPositionColor[] 
       { 
        new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[0]), 
        new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[1]), 
        new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[2]), 
        new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[3]) 
       } 
      ); 

     mIndexBuf.SetData<short>(new short[] { 0, 1, 2, 3 }); 
    } 

    public override void Update() 
    { 
     //TODO 
    } 

    public override void Draw() 
    { 
     mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up); 
     mShader.CurrentTechnique.Passes[0].Apply(); 

     Graphics.MainDevice.SetVertexBuffer(mVertexBuf); 
     Graphics.MainDevice.Indices = mIndexBuf; 

     Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, NumVertices, 0, 1); 
    } 
} 

更新

这是最新的Rect.Draw()实现。

public override void Draw() 
    { 
     Viewport viewport = Graphics.MainDevice.Viewport; 

     mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up); 
     mShader.View = Matrix.CreateLookAt(new Vector3(viewport.Width/2, viewport.Height/2, -5f), Center, Vector3.Up); 
     mShader.CurrentTechnique.Passes[0].Apply(); 

     Graphics.MainDevice.SetVertexBuffer(mVertexBuf); 
     Graphics.MainDevice.Indices = mIndexBuf; 

     Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.LineStrip, 0, 0, NumVertices, 0, 2); 
    } 
+1

您是否正确设置了相机,以便可以看到形状? – PaulG

+0

@保罗,我相信如此。我不太确定,如果我做得很好,或者我需要这样做。这是一款2D游戏 - 因此我需要*相机吗? – zeboidlund

+0

是的,这是必要的。请张贴您的投影矩阵/世界矩阵。 – Ani

回答

1

啊 - 我想我至少看到一个问题。您正在绘制一个TriangleList(请参阅PrimitiveType),并且您需要6个索引,而不是4.

明白了。它看起来像你的顶点/索引顺序是错误的。我也没有看到投影矩阵。

下面是对我很好的代码 - 我所做的只是添加投影矩阵,重新排列顶点,更改索引并修复原始计数。

http://pastebin.com/tMKCxBLL

希望这有助于!

+0

另外,如果你正在绘制一个正方形,你会想要2个原始图像而不是1 – Andy

+0

ananthonline:额外的两个索引究竟是多少? – zeboidlund

+0

根据文档,TriangleList表示“数据按三角形的顺序排列;每个三角形由三个新顶点描述,背面剔除受当前的绕组顺序渲染状态的影响”。因此,给定4个顶点,您的索引是{0,1,2,0,2,3}或沿着这些线的东西。六个索引组成两个三角形(4个顶点)。 XNA不能像OpenGL那样渲染四边形。 – Ani