Индексы не рендеринг - XNA - PullRequest
       48

Индексы не рендеринг - XNA

0 голосов
/ 10 декабря 2011

ну, я решил наконец добавить поддержку индекса к своему движку 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

1 Ответ

0 голосов
/ 11 декабря 2011

Когда я создаю буферы, я назначаю слишком много памяти, используя целое число вместо короткого (которое я использую для списка, который содержит индексы)

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