2011-12-10 9 views
0

私は最終的に私のminecraftエンジンのものにインデックスサポートを追加することにしましたが、私がそれらを追加したらすぐに何も表示されませんでした...私はそれは非常に記述的ではないが、私のコードは私が何か間違っているかどうかを確認する;)ありがとう。インデックスがレンダリングされていない - XNA

public void BuildFaceVertices(Block block, Vector3 pos, BlockFaceDirection blockFaceDirection) 
    { 
     Vector3 topLeftFront = new Vector3(0f, 1f, 0f) + pos; 
     Vector3 bottomLeftFront = new Vector3(0f, 0f, 0f) + pos; 
     Vector3 topRightFront = new Vector3(1f, 1f, 0f) + pos; 
     Vector3 bottomRightFront = new Vector3(1f, 0f, 0f) + pos; 
     Vector3 topLeftBack = new Vector3(0f, 1f, -1f) + pos; 
     Vector3 topRightBack = new Vector3(1f, 1f, -1f) + pos; 
     Vector3 bottomLeftBack = new Vector3(0f, 0f, -1f) + pos; 
     Vector3 bottomRightBack = new Vector3(1f, 0f, -1f) + pos; 

     int row = (int)block.BlockType/Variables.TILE_ALAIS.NumberOfColumns; 
     int column = (int)block.BlockType - row * Variables.TILE_ALAIS.NumberOfColumns; 

     float unit = 1.0f/(float)Variables.TILE_ALAIS.NumberOfColumns; 

     float x = column * unit; 
     float y = row * unit; 

     Vector2 topLeft = new Vector2(x, y); 
     Vector2 topRight = new Vector2(x + unit, y); 
     Vector2 bottomLeft = new Vector2(x, y + unit); 
     Vector2 bottomRight = new Vector2(x + unit, y + unit); 



     switch (blockFaceDirection) 
     { 
      case BlockFaceDirection.ZIncreasing: 
       SolidVertices.Add(new VertexPositionTexture(topRightFront, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightFront, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftFront, topLeft)); 
       break; 
      case BlockFaceDirection.ZDecreasing: 
       // Clockwise 
       SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topRight)); 
       break; 
      case BlockFaceDirection.YIncreasing: 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(topRightFront, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topLeft));     
       break; 
      case BlockFaceDirection.YDecreasing: 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomLeft)); 

       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomLeft)); 
       break; 
      case BlockFaceDirection.XIncreasing: 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightFront, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, bottomLeft)); 
       break; 
      case BlockFaceDirection.XDecreasing: 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftFront, topRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomLeft)); 
       break; 

     } 
    } 

任意のアイデア:

public void BuildVertexBuffers() 
    { 
     // TODO: Implyment index 
     SolidVertices = new List<VertexPositionTexture>(); 
     IndexList = new List<short>(); 

     short i = 0; 

     for (short x = 0; x < Variables.REGION_SIZE_X; x++) 
     { 
      for (short y = 0; y < Variables.REGION_SIZE_Y; y++) 
      { 
       for (short z = 0; z < Variables.REGION_SIZE_Z; z++) 
       { 
        int X = (int)(Index.X * Variables.REGION_SIZE_X + x); 
        int Y = (int)(Index.Y * Variables.REGION_SIZE_Y + y); 
        int Z = (int)(Index.Z * Variables.REGION_SIZE_Z + z); 

        bool above = world.Exists(X, Y + 1, Z); 
        bool below = world.Exists(X, Y - 1, Z); 
        bool left = world.Exists(X - 1, Y, Z); 
        bool right = world.Exists(X + 1, Y, Z); 
        bool front = world.Exists(X, Y, Z + 1); 
        bool back = world.Exists(X, Y, Z - 1); 

        bool notVisible = above && below && left && right && front && back; 

        if (notVisible) 
         continue; 
        if (world.GetBlock(X, Y, Z).BlockType == BlockType.none) 
         continue; 

        i++; 

        if (!back) 
         BuildIndexBuffer(world.GetBlock(X, Y, Z), new Vector3(X, Y, Z), BlockFaceDirection.ZDecreasing); 
        if (!front) 
         BuildIndexBuffer(world.GetBlock(X, Y, Z), new Vector3(X, Y, Z), BlockFaceDirection.ZIncreasing); 
        if (!above) 
         BuildIndexBuffer(world.GetBlock(X, Y, Z), new Vector3(X, Y, Z), BlockFaceDirection.YIncreasing); 
        if (!below) 
         BuildIndexBuffer(world.GetBlock(X, Y, Z), new Vector3(X, Y, Z), BlockFaceDirection.YDecreasing); 
        if (!right) 
         BuildIndexBuffer(world.GetBlock(X, Y, Z), new Vector3(X, Y, Z), BlockFaceDirection.XIncreasing); 
        if (!left) 
         BuildIndexBuffer(world.GetBlock(X, Y, Z), new Vector3(X, Y, Z), BlockFaceDirection.XDecreasing); 
       } 
      } 
     } 

     CopyToBuffers(); 
     Dirty = false; 
    } 

    public void BuildIndexBuffer(Block block, Vector3 pos, BlockFaceDirection blockFaceDirection) 
    { 
     Vector3 topLeftFront = new Vector3(0f, 1f, 0f) + pos; 
     Vector3 bottomLeftFront = new Vector3(0f, 0f, 0f) + pos; 
     Vector3 topRightFront = new Vector3(1f, 1f, 0f) + pos; 
     Vector3 bottomRightFront = new Vector3(1f, 0f, 0f) + pos; 
     Vector3 topLeftBack = new Vector3(0f, 1f, -1f) + pos; 
     Vector3 topRightBack = new Vector3(1f, 1f, -1f) + pos; 
     Vector3 bottomLeftBack = new Vector3(0f, 0f, -1f) + pos; 
     Vector3 bottomRightBack = new Vector3(1f, 0f, -1f) + pos; 

     int row = (int)block.BlockType/Variables.TILE_ALAIS.NumberOfColumns; 
     int column = (int)block.BlockType - row * Variables.TILE_ALAIS.NumberOfColumns; 

     float unit = 1.0f/(float)Variables.TILE_ALAIS.NumberOfColumns; 

     float x = column * unit; 
     float y = row * unit; 

     Vector2 topLeft = new Vector2(x, y); 
     Vector2 topRight = new Vector2(x + unit, y); 
     Vector2 bottomLeft = new Vector2(x, y + unit); 
     Vector2 bottomRight = new Vector2(x + unit, y + unit); 

     switch (blockFaceDirection) 
     { 
      case BlockFaceDirection.ZIncreasing: 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftFront, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightFront, topRight)); 
       AddIndices(0, 2, 3, 3, 1, 0); 
       break; 
      case BlockFaceDirection.ZDecreasing: 
       SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topRight)); 
       AddIndices(0, 2, 3, 3, 1, 0); 
       break; 
      case BlockFaceDirection.YIncreasing: 
       SolidVertices.Add(new VertexPositionTexture(topLeftFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightFront, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topRight)); 
       AddIndices(0, 2, 3, 3, 1, 0); 
       break; 
      case BlockFaceDirection.YDecreasing: 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, topRight)); 
       AddIndices(0, 2, 3, 3, 1, 0); 
       break; 
      case BlockFaceDirection.XIncreasing: 
       SolidVertices.Add(new VertexPositionTexture(bottomRightFront, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(topRightFront, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topRightBack, topRight)); 
       AddIndices(0, 2, 3, 3, 1, 0); 
       break; 
      case BlockFaceDirection.XDecreasing: 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomLeft)); 
       SolidVertices.Add(new VertexPositionTexture(bottomLeftFront, bottomRight)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftBack, topLeft)); 
       SolidVertices.Add(new VertexPositionTexture(topLeftFront, topRight)); 
       AddIndices(0, 2, 3, 3, 1, 0); 
       break; 
     } 


    } 

    public void AddIndices(short i0, short i1, short i2, short i3, short i4, short i5) 
    { 
     IndexList.Add((short)(i0 + VertexCount)); 
     IndexList.Add((short)(i1 + VertexCount)); 
     IndexList.Add((short)(i2 + VertexCount)); 
     IndexList.Add((short)(i3 + VertexCount)); 
     IndexList.Add((short)(i4 + VertexCount)); 
     IndexList.Add((short)(i5 + VertexCount)); 

     VertexCount += 4; 
    } 

    private void CopyToBuffers() 
    { 
     SolidVertexBuffer = new VertexBuffer(Variables.GRAPHICS_DEVICE, VertexPositionTexture.VertexDeclaration, SolidVertices.Count, BufferUsage.WriteOnly); 
     SolidVertexBuffer.SetData(SolidVertices.ToArray()); 
     SolidIndices = new IndexBuffer(Variables.GRAPHICS_DEVICE, typeof(int), IndexList.Count, BufferUsage.WriteOnly); 
     SolidIndices.SetData(IndexList.ToArray()); 
    } 

それはここで完全に働いたvertexbuffer私はちょうど平野を使用し、そのためのコードですか?

おかげで、よろしく、Darestium

+0

あなた自身でデバッグする方が良いと思います。実際にレンダリングされているものを確認するには、DirectX SDKのPIXを試してみてください。 –

答えて

0

私が代わりに

SolidIndices = new IndexBuffer(Variables.GRAPHICS_DEVICE, typeof(int), IndexList.Count, BufferUsage.WriteOnly); 
SolidIndices.SetData(IndexList.ToArray()); 

(私はインデックスを保持しているリストに使用)の代わりに短いの整数を使用して、あまりにも多くのメモリを割り当てられたバッファを作成するとき

SolidIndices = new IndexBuffer(Variables.GRAPHICS_DEVICE, typeof(short), IndexList.Count, BufferUsage.WriteOnly); 
SolidIndices.SetData(IndexList.ToArray()); 
関連する問題