2012-06-18 34 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