Chapter 2 – Graphics Programming

 

We now begin a programming-based approach to computer graphics.  To do this, we consider a minimal API (Application Programmer’s Interface) that has been selected to familiarize the student with basic OpenGL programming.

 

It should be obvious that we shall not present the entire OpenGL API, but restrict ourselves to a number of methods and properties that are sufficient to do a simple basic application.

 

Basics of the OpenGL API

Graphical systems are built around two primitive objects: points and lines between those points.  In the Pen-Plotter Model, points are considered to be two-dimensional objects, with coordinates typically denoted by (X, Y).  In OpenGL and similar systems, all points are three-dimensional objects, with coordinates typically denoted (X, Y, Z).

 

In OpenGL, we use the term “vertex”, (plural “vertices”), rather than point.  A vertex is a location in three-dimensional space, although we shall find use for a four-dimensional representation of a point.  OpenGL contains a family of methods, denoted by “glVertex*”, used to specify vertices.  There are a number of options for use of these methods.
      1)   Whether we view the vertex in 2, 3, or 4 dimensions.
            In this course, we focus on either 3 or 4 dimensional representations.

      2)   Whether we use integers, floating point numbers, or double precision floating
            point numbers to specify the coordinates of the vertices.
      3)   Whether, for an N-dimensional point, we use N numbers or an array with
            N elements to specify the coordinates of the vertices.

 

For the moment, we focus on three-dimensional vertices specified with floating point numbers.  Suppose an array appropriately declared with p = {0.3, 0.4, 0.5}.  We may then specify the vertex in two different ways:

 

            glVertex3f   ( 0.3, 0.4, 0.5)  or

            glVertex3fv  (p).

 

The basic OpenGL primitives are specified via a list of vertices in space, this list being delimited by a pair of delimiters glBegin(option) and glEnd().  The option argument, called “type” in the book, selects how the vertex coordinates are to be interpreted.  For example, a
3-4-5 triangle in the XY plane might be described by

 

            glBegin ( GL_LINE_LOOP) ;

                        glVertex3f (0.0, 0.0, 0.0) ;

                        glVertex3f (3.0, 0.0, 0.0) ;

                        glVertex3f (0.0, 4.0, 0.0) ;
            glEnd() ;

 


Line primitives available in OpenGL include facilities to declare
      a)   A sequence of unconnected points
      b)   A sequence of points, pair-wise connected as lines
      c)   A polyline, which is a sequence of points connected as a
            single line that is not closed.
      d)   A line loop, which is a sequence of points connected as a
            single closed line.  Line loops appear similar to polygons.

 

In OpenGL, a polygon is considered to be a line loop that has an interior and can be represented as a filled area.  Line loops are just lines.  We compare a four point polyline, four point line loop, and four point polygon in the figure below.

 

There are a number of interesting polygon options allowing for definition of multiple quadrilaterals and multiple triangles with the same statement.

 

A simple polygon is a two-dimensional structure such that no two edges cross each other.  A convex polygon is a simple polygon such that any line between any two points in the polygon is completely contained within the polygon.  It can be proved that all triangles are convex.  While all rectangles are convex, one can show four-sided polygons that are not convex.  The following figures illustrate these terms.

 

 

Color

We could spend some time discussing color theory, but it is preferable to focus on RGB color to the exclusion of other aspects.  In RGB color, one specifies a color by the intensity of three components, denoted Red, Green, and Blue.  There are a number of color display methods in use, including 8-bit color and 24-bit color.  In OpenGL, we shall specify the color by a call to glColor3f ( fR, fG, fB), where each of fR, fG, and fB is a real number in the range [0.0, 1.0] used to specify the intensity of Red, Green, and Blue respectively.

 

Later we shall add the alpha channel to be considered as a “fourth color”.

 

Fonts

The book devotes a few pages to discussion of fonts.  We mention fonts only in passing.  Stroke fonts are constructed from other graphic primitives and tend to represent fonts used in common printing.  Raster fonts are more adapted to pixel-based display, but scale up badly.  Most fonts available today are stroke fonts.  The figure below illustrates two stroke fonts and one raster font: 48-point Arial, 48-point Times New Roman, and a raster font.

Note that the raster font “A” looks rather jagged in comparison to the stroke font versions.  This is due to the definition in terms of pixels, rather than graphical primitives.  Most word processors use stroke fonts.

 

Two-Dimensional Viewing

This is a course in three-dimensional graphics.  Despite this fact, we live with the fact that most, if not all, graphic displays are two-dimensional.  We have a number of ways of projecting the three-dimensional objects onto the two-dimensional plane used to represent the view.  Two are the orthographic view and the perspective view.

 

Consider a camera at the origin of coordinates, looking along the +Z axis.  We consider the film to be in the X,Y-plane, so that the image is viewed as existing in the X,Y-plane.  We consider a point (X, Y, Z), with Z > 0, and ask where the image of this point will appear on the image plane.  In the orthographic view, the answer is (X, Y, 0).  While this is simple to compute, it distorts the effects of perspective , which state that points at an equal distance seem to be closer together as they recede into the distance.  The image of parallel railroad tracks appearing to disappear at a point on the horizon illustrates this effect.

 

Aspect Ratio

The aspect ratio of a rectangle is the ratio of the rectangle’s width to its height.  Displays having an aspect ratio of less that 1.0 (higher than they are wide) are uncommon, but occasionally seen in word-processors, where they mimic the shape of typing paper.

 

The aspect ration of a VGA display is 640 / 480 = 4 / 3.  The aspect ratio of a high resolution display is 1280 / 1024 = 5 / 4.  One of the major issues in graphics is handling displays in which the aspect ratio is not the same as that of the original image.  In that case, filling the display will distort the objects.  We often see this effect when a movie is adapted from the “silver screen” to a TV screen with a significantly different aspect ratio.

 

We see the original image with an aspect ratio of approximately 8-to-3 projected onto an approximately square display with an aspect ratio of 1-to-1.  The undistorted view does not fill the entire view.  The screen-filling view shows considerable distortion in the image.