某些面孔没有出现

时间:2011-11-30 04:15:09

标签: c# xna draw region zbuffer

好吧,我正在创造一个像地形一样的类似地形的东西 - 就像我的世界被分隔成地区一样。我的问题只是有些面孔没有显示,6x6区域的顶部和右边部分没有显示。

我想知道是否有人可以检查我的代码以查看我做错了什么,这是功能:

    public void BuildFaceVertices(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;

        Vector2 topLeft = new Vector2(0.0f, 0.0f);
        Vector2 topRight = new Vector2(1.0f, 0.0f);
        Vector2 bottomLeft = new Vector2(0.0f, 1.0f);
        Vector2 bottomRight = new Vector2(1.0f, 1.0f);

        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:
                SolidVertices.Add(new VertexPositionTexture(topRightBack, topLeft));
                SolidVertices.Add(new VertexPositionTexture(topLeftBack, topRight));
                SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomLeft));

                SolidVertices.Add(new VertexPositionTexture(topLeftBack, topRight));
                SolidVertices.Add(new VertexPositionTexture(bottomLeftBack, bottomRight));
                SolidVertices.Add(new VertexPositionTexture(bottomRightBack, bottomLeft));              
                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;

        }
    }'

此代码显示:s1100.photobucket.com/albums/g420/darestium/第一张图片是未正在绘制的面,而后者是未绘制区域的边。

提前致谢,Darestium

1 个答案:

答案 0 :(得分:1)

您的问题与所谓的背面剔除有关。我最近创建了类似于你正在做的事情并遇到了同样的问题。解决方案很简单,但根据您构建顶点的方式可能会很棘手。解决方案:您必须按顺时针顺序构建索引。这意味着,如果你从侧面看你想要看到它的面,那个面的索引必须在你的数组中按顺时针顺序。您似乎没有包含构建索引的代码,只包含顶点,但我会建议您按顺时针顺序构建顶点,然后只需将索引列为0,1,2 (等,按顺序)。从粗略看一下你的switch语句,似乎“ZIncreasing”方向的顶点按顺时针顺序排列,但“ZDecreasing”从TopRight变为TopLeft变为BottomRight。如果您正在按照我想象的那样查看背面的“ZDecreasing”面,那么我认为这是逆时针顺序,并且不会显示(或者更确切地说,将绘制立方体那部分的内部面,是不可见的。)

如果难以想到你的头脑,只需制作一个小纸盒并标记角落即可。在创建顶点时,跟踪角落并确保它们按顺时针顺序排列。