Search code examples
c++sfml

Bottom Face of Cube not rendered - Face Cullling OpenGL


I am drawing a cube in OpenGL C++ with SFML in place of GLFW. The Cube is being rendered correctly without face culling but after face culling the bottom face of the cube is not visible.

I wanted the camera - facing sides of the cube to be rendered but the bottom side even when the camera is facing them they are not being rendered. My cube vertex data with texture data :

GLfloat vertices[] = {
        // Front face  //Texture Coordinates
        -1.0f, -1.0f,  1.0f,  0.0f, 1.0f,
        1.0f, -1.0f,  1.0f,  1.0f, 1.0f,
        1.0f,  1.0f,  1.0f,  1.0f, 0.0f,
        -1.0f,  1.0f,  1.0f,  0.0f, 0.0f,
        // Back face
        -1.0f, -1.0f, -1.0f,  1.0f, 1.0f,
        -1.0f,  1.0f, -1.0f,  1.0f, 0.0f,
        1.0f,  1.0f, -1.0f,  0.0f, 0.0f,
        1.0f, -1.0f, -1.0f,  0.0f, 1.0f,
        // Left face
        -1.0f, -1.0f, -1.0f,  0.0f, 0.0f,
        -1.0f, -1.0f,  1.0f,  1.0f, 0.0f,
        -1.0f,  1.0f,  1.0f,  1.0f, 1.0f,
        -1.0f,  1.0f, -1.0f,  0.0f, 1.0f,
        // Right face
        1.0f, -1.0f, -1.0f,  1.0f, 0.0f,
        1.0f,  1.0f, -1.0f,  1.0f, 1.0f,
        1.0f,  1.0f,  1.0f,  0.0f, 1.0f,
        1.0f, -1.0f,  1.0f,  0.0f, 0.0f,
        // Top face
        -1.0f,  1.0f, -1.0f,  0.0f, 1.0f,
        -1.0f,  1.0f,  1.0f,  1.0f, 1.0f,
        1.0f,  1.0f,  1.0f,  1.0f, 0.0f,
        1.0f,  1.0f, -1.0f,  0.0f, 0.0f,
        // Bottom face
        -1.0f, -1.0f, -1.0f, 1.0f, 0.0f,
        -1.0f, -1.0f,  1.0f, 0.0f, 0.0f,
        1.0f, -1.0f,  1.0f, 0.0f, 1.0f,
        1.0f, -1.0f, -1.0f, 1.0f, 1.0f
};

My cube indice data :

GLuint indices[] = {
        // Front face
        0, 1, 2,
        2, 3, 0,
        // Back face
        4, 5, 6,
        6, 7, 4,
        // Left face
        8, 9, 10,
        10, 11, 8,
        // Right face
        12, 13, 14,
        14, 15, 12,
        // Top face
        16, 17, 18,
        18, 19, 16,
        // Bottom face
        20, 21, 22,
        22, 23, 20
};

My Face culling settings

//Face culling
glEnable(GL_CULL_FACE);
//Cull settings
glCullFace(GL_FRONT);
glFrontFace(GL_CW);

IMAGES : Perspective 1 : (3sides visible) Perspective 2 : (bottom side not visible)


Solution

  • Works as expected.

    Top and bottom sides of your box have vertices defined in CCW order.

    If vertices defined in CW order are taken to be front-facing when you see at the top side of the box from above you are looking at the back face whose rendering is enabled that is why you see box's top side.

    When you look at the bottom side of box below the box you cannot see that side because it is treated as front face - rendering of which is disabled.

    Just change the order of indices for box's bottom side to be CW order:

        // Bottom face
        21, 20, 23,
        21, 23, 22