0

好吧,我决定最终为我的 Minecraft 引擎添加索引支持,但问题是,一旦我添加了它们,什么都没有出现……我知道这不是很具描述性,但有人可以查看我的代码,看看我是否在做出了点问题;)谢谢。

    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());
    }

当我只使用一个普通的顶点缓冲区时,它工作得很好,这里是它的代码:

   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;

        }
    }

有任何想法吗?

谢谢,问候, Darestium

4

1 回答 1

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());
于 2011-12-10T21:45:50.443 回答