Triangle Strip - OpenGL Implementation

OpenGL Implementation

OpenGL has built-in support for triangle strips using the glBegin, glVertex*, and glEnd functions. To draw a triangle strip, glBegin must be passed the argument GL_TRIANGLE_STRIP, which notifies OpenGL a triangle strip is about to be drawn. The glVertex* family of functions specify the coordinates for each vertex in the triangle strip. For more information, consult The OpenGL Redbook.

To draw the triangle strip in the diagram, the code is as follows:

//Vertices below are in Clockwise orientation //Default setting for glFrontFace is Counter-clockwise glFrontFace(GL_CW); glBegin(GL_TRIANGLE_STRIP); glVertex3f( 0.0f, 0.0f, 0.0f ); //vertex 1 glVertex3f( 0.0f, 1.0f, 0.0f ); //vertex 2 glVertex3f( 1.0f, 0.0f, 0.0f ); //vertex 3 glVertex3f( 1.5f, 1.0f, 0.0f ); //vertex 4 glEnd;

Note that only one additional vertex is needed to draw the second triangle. In OpenGL, the order in which the vertices are specified is important so that surface normals are consistent.

Quoted directly from the OpenGL Programming Guide:

GL_TRIANGLE_STRIP Draws a series of triangles (three-sided polygons) using vertices v0, v1, v2, then v2, v1, v3 (note the order), then v2, v3, v4, and so on. The ordering is to ensure that the triangles are all drawn with the same orientation so that the strip can correctly form part of a surface.

The above code sample and diagram demonstrate triangles drawn in a clockwise orientation. For those to be considered front-facing, a preceding call to glFrontFace(GL_CW) is necessary, which otherwise has an initial value of GL_CCW (meaning that triangles drawn counter-clockwise are front-facing by default). This is significant if glEnable(GL_CULL_FACE) and glCullFace(GL_BACK) are already active (GL_BACK by default), because back-facing triangles will be culled, so will not be drawn and will not appear on-screen at all.

Read more about this topic:  Triangle Strip