2012-02-26 2 views
0

모델을 잘 렌더링 할 수 있었기 때문에 Ok,이 말은 문자 그대로 내 마음을 죽이고 있습니다. (사실 저는 카메라를 테스트해야했습니다) .Cube로 XNA를 그릴 때 큐브를 그리는 방법

그러나 이제 정점과 인덱스 버퍼에서 큐브를 그리려고하고 있지만 작동하지 않습니다. (삼각형 등을 그릴 수 있지만 자신의 클래스에서 가져올 수는 없습니다.)

내 목표는 게임 세계를 만들기 위해 64x64x8 큐브의 영역을 만들 수 있다는 것입니다. (미니 크래프트 클론이 아니라 실제로 RTS입니다. 게임 세계 그 자체가 8 큐브에 불과하다는 점에서 "2d"느낌을 갖지만, 나는 빗나간 다.)

웹상의 다양한 인덱스 & 버텍스 튜토리얼을 살펴 보니이 기능이 작동해야하는 것처럼 보입니다. 여기에 몇 가지 코드가 .....

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 시간 동안 코드를 가지고 staredown을 해왔 기 때문일 것입니다. <

아, 카메라가 0,0,50에서 시작하여 0,0,0을 향하고 있습니다. 또한 회전 (가운데 마우스 버튼을 누르고있는 상태)으로 마우스와 키보드로 (rts 캠처럼) 움직일 수 있습니다. 내 큐브가 내 뷰 범위 밖의 어딘가에 있지 않았 음을 확인하기 위해 모든 것을 둘러 보았습니다. 그러나 파란색으로 보는 것이 전부입니다. <

여기에서 도움을 주시면 감사하겠습니다.

p.s. 2 차 질문으로, 큐브간에 버퍼를 공유하는 방법에 대한 힌트가 있습니까? 큐브의 구조가 결코 바뀌지 않기 때문에 버퍼를 공유하는 것이 더 효율적이지만이 방법에 대해서는 잘 모릅니다 ... 커다란 버퍼 하나를 만들고 일종의 목록 클래스의 큐브로 채 웁니다. 어쩌면 렌더링해야하는 모든 큐브를 보유하고 있습니까? 어디서부터 시작해야 할지도 모릅니다. 다시 말하지만, 모든 입력/조언에 대해 감사드립니다.

답변

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

예. < 가능한 한 낮은 정보 (바이트가 반바지보다 짧음)를 보내려고했는데 반바지가 "간결"한 것을 알지 못했습니다. –

+0

호기심에서 벗어난 이유는 정확히 색인 계수 매개 변수에 단시간 크기를 곱한 이유는 무엇입니까? 이것은 효과적으로 당신이 원하는 지수의 16 배를 기대하지 않을까요? 그 매개 변수가 각 색인의 보폭에 관심이 있다고 생각하지 않습니다. 단지 그 수만입니다. –

+0

잘 모르겠습니다. [msdn] (http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.indexbuffer.aspx)의 코드는 sizeof (short)를 사용하여 보여 주지만 그렇다고해서 할 수있는 가장 좋은 방법. 나는 그것없이 실험 해 보았고 그 지표들을 발견했다.길이는 작동하지만 그 이하는 그렇지 않습니다. 또한 sizeof (short)는 16이 아니라 2입니다. – Molinger

관련 문제