Backface Culling

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. This can save precious rendering time.

This tutorial follows on from the previous tutorial.

Contents of main.cpp :


A boolean variable is created to keep track of the polygon winding mode. This will be explained further down.

bool acw = true;

The first step we need to take to enable backface culling is to add the code below to our init method. To enablebackface culling, we need to pass GL_CULL_FACE onto the glEnable function. This will cause all backface polygons to not be rendered.

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 an anti-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 an anti-clockwise direction.

	glEnable(GL_CULL_FACE);

The next change we need to make is to our menu function.

Above, we stated that front facing polygons are specified by placing vertices in an anti-clockwise direction. We can change this by making a call to the glFrontFace function. This function can take one of two flags, GL_CCW andGL_CW. The default value is GL_CCW(counter-clockwise winding). If you pass GL_CW(clockwise) to theglFrontFace function, front facing polygons will be specified by placing vertices in a clockwise direction.

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

The code below reverses which faces are culled when selecting the Reverse Culling menu option.

void menu(int entry)
{
	.
	.
	case 2 : 
		acw = !acw;

		glFrontFace(acw ? GL_CCW : GL_CW);
		
		break;

The second change we make is that if you select the Toggle Culling option, backface culling is enabled and disabled.

	case 3 : 
		if (glIsEnabled(GL_CULL_FACE))
			glDisable(GL_CULL_FACE);
		else
			glEnable(GL_CULL_FACE);
		break;
	.
	.
}

When running the program, you can switch polygon winding by pressing 'r' and you can enable and disable backface culling by pressing 'f'.

You should now be able to cull faces that are never seen. This is very useful to speed up your programs. For every unseen polygon that is culled, another one can be rendered to make a different object more detailed.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值