Backface Culling

Backface culling is a feature that tells OpenGL not to draw the surfaces on the backs of objects - i.e. those facing away from the camera.

When drawing opaque objects, the back faces are hidden by the front faces of an object, so we don't notice if they're drawn.
When drawing transparent objects, however, the back faces will appear, blended in with the front faces, and often look wrong.

To make OpenGL throw out the back faces, call glEnable(GL_CULL_FACE).

glDisable(GL_CULL_FACE);glEnable(GL_CULL_FACE);

Example code: cullface.c

Whether a polygon is front-facing or back-facing is determined by the order of its vertices. A front-facing polygon is defined to be one whose vertices (when projected into the final window) are given in counter-clockwise order. (This is actually another instance of the right-hand-rule.)

Front facingBack facing
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f( 1.0, -1.0, 0.0);
glVertex3f( 1.0,  1.0, 0.0);
glVertex3f(-1.0,  1.0, 0.0);
glVertex3f(-1.0,  1.0, 0.0);
glVertex3f( 1.0,  1.0, 0.0);
glVertex3f( 1.0, -1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);

[With triangle strips, it's slightly more complicated, as the rule has to reverse with each triangle, but if you make sure that the first triangle is facing the way you want, the rest will come out correctly.]

Advanced trick

In some cases, you might actually want the backs of transparent objects drawn, to make them look complete.

However, you then run into the drawing-order problem again - the back faces of the objects must always be drawn before the front faces. And if you use "packaged" objects, such as the GLUT objects, you have no control over the order that individual polygons are drawn.

You can get around this problem using multi-pass rendering. That is, draw your objects multiple times, but on the first pass, use glCullFace(GL_FRONT) to cull the front faces and only draw the back faces; on the second pass, use the normal glCullFace(GL_BACK) to draw only the front faces.

This trick will only work for convex (simple) objects. Something like the teapot will still have problems.

Example code: drawbacks.c



next