Spaurh의 느긋한 블로그



Backface Culling Computer Graphic

Introduction

Backface CullingIn 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-clockwisedirection. 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 theglFrontFace 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 aclockwise 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 includeGL_FRONTGL_BACK and GL_FRONT_AND_BACK. The default value is obviouslyGL_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.