Loading

OpenGL: Drawing Geometric Primitives

Drawing Geometric Primitives

This OpenGL tutorial builds on the code from Lesson 1. If you have not downloaded and set up GLUT on your machine, you will need to do that.

This tutorial covers the 10 geometric primitives in OpenGL: Points, Lines, Triangles, Triangle Strip, Quad Strip, Line Strip, Line Loop, Quads, Polygon, and Triangle Fan. These primitives are simple shapes that can be rendered quickly in hardware and can be put together to make more complex shapes. To draw these primitives, we call the function glBegin() with one of these constant arguments:

  • GL_POINTS
  • GL_LINES
  • GL_TRIANGLES
  • GL_TRIANGLE_STRIP
  • GL_QUAD_STRIP
  • GL_LINE_STRIP
  • GL_LINE_LOOP
  • GL_QUADS
  • GL_POLYGON
  • GL_TRIANGLE_FAN

Then we add the vertices with successive calls to glVertex3f() and finish with a call to glEnd().

As was mentioned in the video, polygons must be convex and simple; these constraints are given to make it possible to render polygons faster.

There are a few additional minor points to be made about geometric primitives. First, polygons that are not convex and simple will still render without notification. Likewise, primitives such as triangles, which require that the vertices come in multiples of three, will still render, but the extra vertices will be ignored. Generally, you will not want any of these errors silently running through your code.

So, you may want to check that polygons are simple and convex, and that primitives have the correct multiple of vertices. A polygon is simple if no two sides intersect and convex if the polygon equals its convex hull. The number of vertices for each primitive should be divisible by these integers: Points 1, Lines 2, Triangles 3, Triangle Strip 1, Quad Strip 2, Line Strip 1, Line Loop 1, Quads 4, Polygon 1, Triangle Fan 1.

One other point that we will be concerned about is orientation. Each triangle or simple polygon has an orientation: counterclockwise or clockwise. The counterclockwise orientation is also considered the positive orientation or the front face of the polygon. The clockwise orientation is considered negative orientation or the back face of the polygon. The terms front and back have great significance in 3D, and a common optimization is that the back face of a polygon usually does not need to be rendered.