2012-02-26 86 views
0

好吧,這個人真的會殺死我的腦筋,因爲我已經能夠渲染模型就好了 (其實我必須爲了測試我的相機) 。如何在XNA中用C繪製這個立方體#

但是現在我試圖從一個頂點和索引緩衝區繪製一個立方體,它不會工作。 (我已經能夠繪製三角形等,但從來沒有從他們自己的類)。

我的最終目標是能夠構建64x64x8立方體的區域來創建遊戲世界。 (不是我的世界克隆,實際上是一個RTS--它會有「2D」的感覺,因爲遊戲世界本身只有8立方體深,但我離題了)。

從遍佈整個網絡的各種索引&頂點教程,它看起來像我這樣應該工作。下面是一些代碼.....

game.cs

protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     // TODO: Add your update logic here 
     float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds/1000.0f; 
     cam.Update(timeDifference); 
     base.Update(gameTime); 
    } 

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

     // TODO: Add your drawing code here 

     cube = new CubeShape(Color.Black, new Vector3(0, 0, 0), GraphicsDevice); 


     RasterizerState rasterizerState = new RasterizerState(); 
     rasterizerState.CullMode = CullMode.None; 
     GraphicsDevice.RasterizerState = rasterizerState; 

     cube.Render(cam.viewMatrix,cam.projectionMatrix); 
     base.Draw(gameTime); 
    } 
} 

和我的立方體(這個人是那種長,對不起)

class CubeShape 
{ 
    //Transform later to have static v and i buffers. 
    private VertexBuffer vBuffer; 
    public VertexBuffer VBuffer 
    { get { return vBuffer; } set { vBuffer = value; } } 

    private IndexBuffer iBuffer; 
    public IndexBuffer IBuffer 
    { get { return iBuffer; } set { iBuffer = value; } } 

    private BasicEffect bEffect; 
    public BasicEffect BEffect 
    { get { return bEffect; } set { bEffect = value; } } 

    private Matrix world; 
    public Matrix World 
    { get { return world; } set { world = value; } } 

    private Matrix view; 
    public Matrix View 
    { get { return view; } set { view = value; } } 

    private Matrix projection; 
    private Matrix Projection 
    { get { return projection; } set { projection = value; } } 

    private Color color; 
    public Color Color 
    { get { return color; } set { color = value; } } 

    private Vector3 position; 
    public Vector3 Position 
    { get { return position; } set { position = value; } } 

    //Need to change this eventually to use textures. 
    private VertexPositionColor[] vertices; 
    byte[] indices; 


    private GraphicsDevice device; 
    //constructors! 
    public CubeShape(Color inColor,Vector3 inPosition,GraphicsDevice inDevice) 
    { 
     device = inDevice; 


     this.color = inColor; 
     this.position = inPosition; 
     SetUpVertices(); 
     SetUpIndices(); 
     //world = Matrix.CreateTranslation(position); 
     world = Matrix.CreateTranslation(0, 0, 0); 
     bEffect = new BasicEffect(device); 
     bEffect.World = world; 
     bEffect.VertexColorEnabled = true; 
    } 
    //end constructors! 

    // >.< 
    public void Render(Matrix view,Matrix projection) 
    { 
     bEffect.View = view; 
     bEffect.Projection = projection; 


     device.SetVertexBuffer(vBuffer); 
     device.Indices = IBuffer; 

     foreach(EffectPass pass in bEffect.CurrentTechnique.Passes) 
     { 
      pass.Apply(); 
      device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12); 
     } 
    } 

    /// <summary> 
    /// Sets up the vertices for a cube using 8 unique vertices. 
    /// Build order is front to back, left to up to right to down. 
    /// </summary> 
    private void SetUpVertices() 
    { 
     vertices = new VertexPositionColor[8]; 

     //front left bottom corner 
     vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), color); 
     //front left upper corner 
     vertices[1] = new VertexPositionColor(new Vector3(0, 100, 0), color); 
     //front right upper corner 
     vertices[2] = new VertexPositionColor(new Vector3(100, 100, 0), color); 
     //front lower right corner 
     vertices[3] = new VertexPositionColor(new Vector3(100, 0, 0), color); 
     //back left lower corner 
     vertices[4] = new VertexPositionColor(new Vector3(0, 0, -100), color); 
     //back left upper corner 
     vertices[5] = new VertexPositionColor(new Vector3(0, 100, -100), color); 
     //back right upper corner 
     vertices[6] = new VertexPositionColor(new Vector3(100, 100, -100), color); 
     //back right lower corner 
     vertices[7] = new VertexPositionColor(new Vector3(100, 0, -100), color); 

     vBuffer = new VertexBuffer(device, typeof(VertexPositionColor), 8, BufferUsage.WriteOnly); 
     vBuffer.SetData<VertexPositionColor>(vertices); 
    } 

    /// <summary> 
    /// Sets up the indices for a cube. Has 36 positions that match up 
    /// to the element numbers of the vertices created earlier. 
    /// Valid range is 0-7 for each value. 
    /// </summary> 
    private void SetUpIndices() 
    { 
     indices = new byte[36]; 

     //Front face 
     //bottom right triangle 
     indices[0] = 0; 
     indices[1] = 3; 
     indices[2] = 2; 
     //top left triangle 
     indices[3] = 2; 
     indices[4] = 1; 
     indices[5] = 0; 
     //back face 
     //bottom right triangle 
     indices[6] = 4; 
     indices[7] = 7; 
     indices[8] = 6; 
     //top left triangle 
     indices[9] = 6; 
     indices[10] = 5; 
     indices[11] = 4; 
     //Top face 
     //bottom right triangle 
     indices[12] = 1; 
     indices[13] = 2; 
     indices[14] = 6; 
     //top left triangle 
     indices[15] = 6; 
     indices[16] = 5; 
     indices[17] = 1; 
     //bottom face 
     //bottom right triangle 
     indices[18] = 4; 
     indices[19] = 7; 
     indices[20] = 3; 
     //top left triangle 
     indices[21] = 3; 
     indices[22] = 0; 
     indices[23] = 4; 
     //left face 
     //bottom right triangle 
     indices[24] = 4; 
     indices[25] = 0; 
     indices[26] = 1; 
     //top left triangle 
     indices[27] = 1; 
     indices[28] = 5; 
     indices[29] = 4; 
     //right face 
     //bottom right triangle 
     indices[30] = 3; 
     indices[31] = 7; 
     indices[32] = 6; 
     //top left triangle 
     indices[33] = 6; 
     indices[34] = 2; 
     indices[35] = 3; 

     iBuffer = new IndexBuffer(device, typeof(short), 36, BufferUsage.WriteOnly); 
     iBuffer.SetData(indices); 
    } 
} 

我真的不知道爲什麼這個ISN沒有工作。可能是因爲我在過去的4個小時裏一直在使用代碼。 <

哦,相機從0,0,50開始,面向0,0,0。它還允許我隨着鼠標和鍵盤移動(如任何rts cam應該)並旋轉(按住鼠標中鍵)。爲了確保我的立方體不在我的視野範圍之外,我搜索了所有地方,但我看到的只是藍色>。 <

我很感謝這裏的任何幫助。

p.s.作爲第二個問題,關於如何在多維數據集之間共享緩衝區的提示? 我讀到,因爲一個多維數據集的幾何形狀永遠不會改變,共享緩衝區效率更高,但我不知道如何去做這個......創建一個體積很大的緩衝區,並用來自某種列表類的多維數據集填充它可以容納所有需要渲染的立方體?甚至不知道從哪裏開始尋找。再次,非常感謝任何意見/建議。

回答

0

看起來你有一些打字問題。我把它通過更改下面的代碼工作:

//byte[] indices; 
short[] indices; 

//indices = new byte[36]; 
indices = new short[36]; 

//iBuffer = new IndexBuffer(device, typeof(short), 36, BufferUsage.WriteOnly); 
iBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, sizeof(short) * indices.Length, BufferUsage.WriteOnly); 

如果還是不行,在我大喊大叫,我會檢查,以確保我沒有錯過任何東西。請記住,我不得不使用我自己的相機類之一,因爲你不包括你的。

我不知道如何回答你的第二個問題。你可能想要把它作爲一個單獨的問題。

+0

是的工作>。< 我試圖保持信息儘可能低(字節短於短褲),並沒有意識到短褲是「短」,因爲我可以去。 – 2012-02-26 19:20:01

+0

出於好奇,爲什麼你將索引計數參數乘以短的大小?這不會有效地使它期望16倍你想要的指數數量嗎?我不認爲這個參數關心每個索引的步幅,只關心它們的數量。 – 2012-02-26 20:01:37

+0

我不確定。 [msdn](http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.indexbuffer.aspx)中的代碼使用sizeof(short)顯示它們,但這並不意味着它是最好的事情。我試着沒有它,發現這些指數。長度的作品,但更少的東西沒有。此外,sizeof(短)是2而不是16. – Molinger 2012-02-26 20:50:44