OpenGL Drawing Primitives

OpenGL supports several basic primitive types, including points, lines, quadrilaterals, and geneneral polygons. All of these primitives are specified using a sequence of vertices. The diagram below shows the basic primitive types, where the numbers indicate the order in which the vertices have been specified. Note that for the GL_LINES primitive only every second vertex causes a line segment to be drawn. Similarly, for the GL_TRIANGLES  primitive, every third vertex causes a triangle to be drawn. Note that for the GL_TRIANGLE_STRIP and GL_TRIANGLE_FAN primitives, a new triangle is produced for every additional vertex. All of the closed primitives shown below are solid-filled, with the exception of GL_LINE_LOOP, which only draws lines connecting the vertices.
 

The following code fragment illustrates an example of how the primitive type is specified and how the sequence of  vertices are passed to OpenGL. It assumes that a window has already been opened and that an appropriate 2D coordinate system has already been established.
 

 
The various forms of  glVertex function calls are defined as follows:


The following code fragment specifies a 3D polygon to be drawn, in this case a simple square. Note that in this case the same square could have been drawn using the GL_QUADS and GL_QUAD_STRIP primitives.

Assigning Colours

OpenGL maintains a current drawing colour as part of its state information. The glColor() function calls are used to change the current drawing colour. assigned using the glColor function call. Like glVertex(), this function exists in various instantiations. Colour components are specified in the order of red, green, blue. Colour component values are in the range [0...1], where 1 corresponds to maximum intensity. For unsigned bytes, the range corresponds to [0...255]. All primitives following the fragment of code given below would be drawn in green, assuming no additional glColor() function calls are used.

    GLfloat mycolour[3] = {0,0,1}; // blue
    glColor3fv( mycolour );        // blue using vector of floats
    glColor3f(1.0, 0.0, 0.0);      // red using floats
    glColor3ub(0,255,0);           // green using unsigned bytes

If desired, a polygon can be smoothly shaded to interpolate colours between vertices. This is accomplished by using the GL_SMOOTH shading mode (the OpenGL default) and by assigning a desired colour to each vertex, as shown in the following example.

A fourth value called alpha is often appended to the colour vector. This can be used assign a desired level of transparency to a primitive and finds uses in compositing multiple images together. An alpha value of 0.0 defines an opaque colour, while an alpha value of 1.0 corresponds to complete transparency.

The screen can be cleared to a particular colour as follows:

    glClearcolor(1.0, 1.0, 1.0, 0.0);   // sets the clear colour to white and opaque
    glClear( GL_COLOR_BUFFER_BIT);      // clears the colour frame buffer