c++openglgraphicsculling

OpenGL: Quads seemingly not culled properly


I have built a simple scene like the following:

Scene

The problem is, the blue shape is lower than the red one but somehow bleeds through. It looks proper when I rotate it like the following:

Scene rotated

From what I searched this could be related to the order of vertices being sent, and here is my definition for those:

Shape* Obj1 = new Quad(Vec3(-5.0, 5.0, 0.0), Vec3(5.0, 5.0, 0.0), Vec3(5.0, 5.0, -10.0), Vec3(-5.0, 5.0, -10.0));
Shape* Obj2 = new Quad(Vec3(-5.0, 3.0, 0.0), Vec3(5.0, 3.0, 0.0), Vec3(5.0, 3.0, -10.0), Vec3(-5.0, 3.0, -10.0));

The Vec3 class just holds 3 doubles for x,y,z coordinates. I add these Vec3 classes to a vector, and iterate through them when I want to draw, as such:

glBegin(GL_QUADS);
    for (auto it = vertex_list.begin(); it != vertex_list.end(); ++it)
        glVertex3d(it->get_x(), it->get_y(), it->get_z());
glEnd();

Finally, my settings:

glEnable(GL_ALPHA_TEST | GL_DEPTH_TEST | GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glAlphaFunc(GL_GREATER, 0.0f);
glViewport(0, 0, WINDOW_X, WINDOW_Y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0f, 300.0);
// camera origin xyz, point to look at xyz, camera rot xyz
gluLookAt(10, 10, -20, 2.5, 2.5, -10, 0, 1, 0);

Solution

  • You should enable depth test, face culling and alpha testing separately.

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    

    They are not flags. You cannot use them in that way.