Face Culling

From OpenGL.org
(Redirected from Culling)
Jump to: navigation, search

Triangle primitives after all transformation steps have a particular facing. This is defined by the order of the three vertices that make up the triangle, as well as their apparent order on-screen. Triangles can be discarded based on their apparent facing, a process known as Face Culling.

Winding order

Every vertex in every part of the rendering pipeline has a specific order. The user submits a vertex stream in a specific sequence. Even Tessellation does not alter this sequence. Geometry Shaders emit primitives, but even those primitives have vertices in a set, specific order as defined by the GS.

Each vertex in the Primitive Assembly stage is ordered. The order of the vertices in a triangle can be combined with their relative visual orientation to determine whether the triangle is being seen from the "front" or the "back" side.

This is covered by the winding order of the primitive. Given a vertex ordering, a triangle can appear to have a clockwise winding or counter-clockwise winding. Clockwise means that the three vertices, in order, rotate clockwise around the triangle's center. Counter-clockwise means that the three vertices, in order, rotate counter-clockwise around the triangle's center.

File:Winding order.png

Which side is considered the "front" side is controlled by this function:

void glFrontFace(GLenum mode​);

This is global state. mode​ may be GL_CW​ or GL_CCW​, which mean clockwise or counter-clockwise is front, respectively. On a freshly created OpenGL Context, the default front face is GL_CCW​.

The Fragment Shader gets a built-in input value saying whether the fragment was generated by the triangle's front face (this will always be true for non-triangular primitives).

Culling

The primary use of setting the front facing of a triangle is to allow the culling of the front or back facing triangles.

Consider a cube; this is made of 12 triangles, but 6 of them will be facing in the opposite direction from the other 6. Unless the cube is transparent, 6 of the triangles will always be covered up by the other 6. Indeed, depending on the projection, more than 6 triangles could be covered (imagine a cube that is very close to the camera, since the front face is huge).

Face culling allows non-visible triangles of closed surfaces to be culled before expensive Rasterization and Fragment Shader operations.

To activate face culling, GL_CULL_FACE​ must first be enabled with glEnable. By default, face culling is disabled. To select which side will be culled, use the following function:

void glCullFace(GLenum mode​);

mode​ can be set to GL_FRONT​, GL_BACK​, or GL_FRONT_AND_BACK​. The latter will cull all triangles. This is different from glEnable(GL_RASTERIZER_DISCARD), as the latter will shut off all Primitives, while culling both faces will only cull triangles (since only they have faces).

By default, GL_BACK​ is the face to be culled.