Question

Well, I am creating a minecraft like terrain thing which - like minecraft is seperated into regions. my problem simply is that some faces are not showing, and the top and right part of 6x6 regions are not showing.

I was wondering if someone could check my code to see what I am doing wrong, here is the function:

    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;

        }
    }'

This code is showing: s1100.photobucket.com/albums/g420/darestium/ the first image is of the faces that are not being draw and the latter is of the sides of the regions which are not being drawn.

Thanks in advance, Darestium

Was it helpful?

Solution

Your issue has to do with what is called Backface Culling. I have recently created something similar to what you are doing and ran into the same issues. The solution is simple, but can be tricky depending on how you build your vertices. The solution: you must build your indices in clockwise order. What this means is, if you are looking at the face from the side you want to see it from, the indices of that face must be in clockwise order in your array. You don't seem to have included your code for building indices, only vertices, but I will make the suggestion that you build your vertices in clockwise order and then simply list your indices as 0, 1, 2 (etc, in order). From a cursory look over your switch statement, it seems that the "ZIncreasing" direction's vertices are in clockwise order, but the "ZDecreasing" goes from TopRight to TopLeft to BottomRight. If you are viewing the "ZDecreasing" face from the back as I imagine you would, then I think this is counterclockwise order, and will not show up (or, rather, the inside face of that part of the cube will be drawn, which isn't visible to you).

If it's hard to think of in your head, just make a little paper cube and label the corners. As you create the vertices, trace out the corners and make sure they are in clockwise order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top