SenselessCoder
SenselessCoder

Reputation: 1159

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

Upvotes: 1

Views: 260

Answers (2)

Rabbid76
Rabbid76

Reputation: 210878

See glEnable:

glEnable — enable or disable server-side GL capabilities

void glEnable(GLenum cap);

cap Specifies a symbolic constant indicating a GL capability.

This means the paramter of glEnable is a constant and not a set of bits and GL_ALPHA_TEST, GL_DEPTH_TEST, GL_CULL_FACE are symbolic constats and not bits of a bit set.

Change your code like this:

glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);


See OpenGL Specifiction - 17.3.4 Depth Buffer Test, p. 500:

17.3.4 Depth Buffer Test

The depth buffer test discards the incoming fragment if a depth comparison fails. The comparison is enabled or disabled with the generic Enable and Disable commands using target DEPTH_TEST.

See OpenGL Specifiction - 14.6.1 Basic Polygon Rasterization, p. 473:

Culling is enabled or disabled by calling Enable or Disable with target CULL_FACE.

Upvotes: 1

qbranchmaster
qbranchmaster

Reputation: 119

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.

Upvotes: 2

Related Questions