Menu
OpenGL Tutorials

OpenGL

Backface Culling



Tutorials > OpenGL > Backface Culling

View Full Source

Introduction

Backface Culling In tutorial 10, you will have noticed that when the primitives rotate, their backs are also rendered. When creating a 3D object such as the box in the previous tutorial, we do not need the backs of the primitives (polygons) to be displayed.

A technique known as backface culling is available to us to prevent the backfaces of the polygons from being rendered, thus saving precious rendering time.

This tutorial follows on from the previous tutorial.

Contents of main.cpp :


Two boolean variables are created, the first to keep track of whether backface culling is enabled or not and the other to keep track of the polygon winding mode. The latter will be explained further down.

bool culling = true;
bool ccw = true;

To ensure that the settings of the application are kept when switching between fullscreen and windowed modes, we need to modify the init function.

bool init()
{
	.
	.

To enable backface culling, we need to pass GL_CULL_FACE onto the glEnable function. This will cause all backfacing polygons to not be rendered.

	if (culling)
		glEnable(GL_CULL_FACE);
	else
		glDisable(GL_CULL_FACE);

You may be asking how we determine what side is backfacing? When you render various primitives, you specify front facing polygons by specifying vertices in a counter-clockwise direction. If you go back to all previous objects that we have drawn in the tutorials, you will notice that all primitives have been rendered by specifying vertices in this way.

We can change how backfacing polygons are determined by making a call to the glFrontFace function. This function can take one of two flags, GL_CCW and GL_CW. The default value is GL_CCW(counter-clockwise winding). If you pass GL_CW(clockwise) to the glFrontFace function, front facing polygons will be specified by placing vertices in a clockwise direction.

	glFrontFace(ccw ? GL_CCW : GL_CW);

	return true;
}

Another function which may be useful is the glCullFace function. This function specifies which faces should be culled(not rendered). The values that can be passed include GL_FRONT, GL_BACK and GL_FRONT_AND_BACK. The default value is obviously GL_BACK.

To allow you to play around with some backface culling settings, the idle has been modified.

void idle()
{
	.
	.

The code below enables or disables backface culling when the C key is pressed.

	if (opengl->isKeyDown('C'))
	{
		opengl->keyUp('C');

		culling = !culling;

		if (culling)
			glEnable(GL_CULL_FACE);
		else
			glDisable(GL_CULL_FACE);
	}

The code below reverses which faces are culled when pressing the F key.

	if (opengl->isKeyDown('F'))
	{
		opengl->keyUp('F');

		ccw = !ccw;

		glFrontFace(ccw ? GL_CCW : GL_CW);
	}
}

You have learnt an important concept in 3D graphics. This simple technique can greatly increase the speed of your application. You can obviously not see much of a difference in this small demo but you will see a signficant increase in speed when culling backfacing polygons on models of thousands of polygons.

Please let me know of any comments you may have : Contact Me

Win32 Source Files : Visual Studio Dev-C++
GLUT Source Files : Visual Studio Dev-C++ Unix / Linux

Last Updated : 5 December 2005


< Tutorial 13 - Solid Shapes Tutorial 15 - Polygon Offset >

Back to Top


All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer

Links