CG Practical-1 - Computer Graphics Software-Opengl
CG Practical-1 - Computer Graphics Software-Opengl
1 Coordinate Representations
2 Graphics Functions
3 Software Standards
4 Other Graphics Packages
5 Introduction to OpenGL
6 Summary
From Chapter 3 of Computer Graphics with OpenGL®, Fourth Edition, Donald Hearn, M. Pauline Baker, Warren R. Carithers.
Copyright © 2011 by Pearson Education, Inc. Published by Pearson Prentice Hall. All rights reserved.
29
Computer Graphics Software
GL (Graphics Library), OpenGL, VRML (Virtual-Reality Modeling Language), Java 2D, and
Java 3D. A set of graphics functions is often called a computer-graphics application
programming interface (CG API) because the library provides a software interface
between a programming language (such as C++) and the hardware. So when we write
an application program in C++, the graphics routines allow us to construct and display
a picture on an output device.
1 Coordinate Representations
To generate a picture using a programming package, we first need to give the
geometric descriptions of the objects that are to be displayed. These descriptions
determine the locations and shapes of the objects. For example, a box is specified
by the positions of its corners (vertices), and a sphere is defined by its center posi-
tion and radius. With few exceptions, general graphics packages require geomet-
ric descriptions to be specified in a standard, right-handed, Cartesian-coordinate
reference frame. If coordinate values for a picture are given in some other ref-
erence frame (spherical, hyperbolic, etc.), they must be converted to Cartesian
coordinates before they can be input to the graphics package. Some packages
that are designed for specialized applications may allow use of other coordi-
nate frames that are appropriate for those applications.
In general, several different Cartesian reference frames are used in the process
of constructing and displaying a scene. First, we can define the shapes of individ-
ual objects, such as trees or furniture, within a separate reference frame for each
object. These reference frames are called modeling coordinates, or sometimes
local coordinates or master coordinates. Once the individual object shapes have
been specified, we can construct (“model”) a scene by placing the objects into
appropriate locations within a scene reference frame called world coordinates.
This step involves the transformation of the individual modeling-coordinate
frames to specified positions and orientations within the world-coordinate frame.
As an example, we could construct a bicycle by defining each of its parts
(wheels, frame, seat, handlebars, gears, chain, pedals) in a separate modeling-
coordinate frame. Then, the component parts are fitted together in world coor-
dinates. If both bicycle wheels are the same size, we need to describe only one
wheel in a local-coordinate frame. Then the wheel description is fitted into the
world-coordinate bicycle description in two places. For scenes that are not too
complicated, object components can be set up directly within the overall world-
coordinate object structure, bypassing the modeling-coordinate and modeling-
transformation steps. Geometric descriptions in modeling coordinates and world
coordinates can be given in any convenient floating-point or integer values, with-
out regard for the constraints of a particular output device. For some scenes, we
might want to specify object geometries in fractions of a foot, while for other
applications we might want to use millimeters, or kilometers, or light-years.
After all parts of a scene have been specified, the overall world-coordinate
description is processed through various routines onto one or more output-device
reference frames for display. This process is called the viewing pipeline. World-
coordinate positions are first converted to viewing coordinates corresponding to the
view we want of a scene, based on the position and orientation of a hypothetical
camera. Then object locations are transformed to a two-dimensional (2D) projec-
tion of the scene, which corresponds to what we will see on the output device.
The scene is then stored in normalized coordinates, where each coordinate value
is in the range from −1 to 1 or in the range from 0 to 1, depending on the system.
30
Computer Graphics Software
Viewing and
Projection Coordinates
1
1 Video Monitor
Modeling
Coordinates
Normalized 1 Plotter
World Coordinates
Coordinates
Other Output
Device
Coordinates
FIGURE 1
The transformation sequence from modeling coordinates to device coordinates for a three-dimensional scene.
Object shapes can be individually defined in modeling-coordinate reference systems. Then the shapes are positioned
within the world-coordinate scene. Next, world-coordinate specifications are transformed through the viewing
pipeline to viewing and projection coordinates and then to normalized coordinates. At the final step, individual
device drivers transfer the normalized-coordinate representation of the scene to the output devices for display.
2 Graphics Functions
A general-purpose graphics package provides users with a variety of functions
for creating and manipulating pictures. These routines can be broadly classified
31
Computer Graphics Software
according to whether they deal with graphics output, input, attributes, transfor-
mations, viewing, subdividing pictures, or general control.
The basic building blocks for pictures are referred to as graphics output
primitives. They include character strings and geometric entities, such as points,
straight lines, curved lines, filled color areas (usually polygons), and shapes
defined with arrays of color points. In addition, some graphics packages pro-
vide functions for displaying more complex shapes such as spheres, cones, and
cylinders. Routines for generating output primitives provide the basic tools for
constructing pictures.
Attributes are properties of the output primitives; that is, an attribute
describes how a particular primitive is to be displayed. This includes color spec-
ifications, line styles, text styles, and area-filling patterns.
We can change the size, position, or orientation of an object within a scene
using geometric transformations. Some graphics packages provide an additional
set of functions for performing modeling transformations, which are used to con-
struct a scene where individual object descriptions are given in local coordinates.
Such packages usually provide a mechanism for describing complex objects (such
as an electrical circuit or a bicycle) with a tree (hierarchical) structure. Other pack-
ages simply provide the geometric-transformation routines and leave modeling
details to the programmer.
After a scene has been constructed, using the routines for specifying the object
shapes and their attributes, a graphics package projects a view of the picture onto
an output device. Viewing transformations are used to select a view of the scene,
the type of projection to be used, and the location on a video monitor where the
view is to be displayed. Other routines are available for managing the screen
display area by specifying its position, size, and structure. For three-dimensional
scenes, visible objects are identified and the lighting conditions are applied.
Interactive graphics applications use various kinds of input devices, including
a mouse, a tablet, and a joystick. Input functions are used to control and process
the data flow from these interactive devices.
Some graphics packages also provide routines for subdividing a picture
description into a named set of component parts. And other routines may be
available for manipulating these picture components in various ways.
Finally, a graphics package contains a number of housekeeping tasks, such as
clearing a screen display area to a selected color and initializing parameters. We
can lump the functions for carrying out these chores under the heading control
operations.
3 Software Standards
The primary goal of standardized graphics software is portability. When packages
are designed with standard graphics functions, software can be moved easily
from one hardware system to another and used in different implementations and
applications. Without standards, programs designed for one hardware system
often cannot be transferred to another system without extensive rewriting of the
programs.
International and national standards-planning organizations in many coun-
tries have cooperated in an effort to develop a generally accepted standard for
computer graphics. After considerable effort, this work on standards led to the
development of the Graphical Kernel System (GKS) in 1984. This system was
adopted as the first graphics software standard by the International Standards
Organization (ISO) and by various national standards organizations, including
32
Computer Graphics Software
the American National Standards Institute (ANSI). Although GKS was origi-
nally designed as a two-dimensional graphics package, a three-dimensional GKS
extension was soon developed. The second software standard to be developed
and approved by the standards organizations was Programmer’s Hierarchical
Interactive Graphics System (PHIGS), which is an extension of GKS. Increased
capabilities for hierarchical object modeling, color specifications, surface render-
ing, and picture manipulations are provided in PHIGS. Subsequently, an extension
of PHIGS, called PHIGS+, was developed to provide three-dimensional surface-
rendering capabilities not available in PHIGS.
As the GKS and PHIGS packages were being developed, the graphics work-
stations from Silicon Graphics, Inc. (SGI), became increasingly popular. These
workstations came with a set of routines called GL (Graphics Library), which
very soon became a widely used package in the graphics community. Thus,
GL became a de facto graphics standard. The GL routines were designed for
fast, real-time rendering, and soon this package was being extended to other
hardware systems. As a result, OpenGL was developed as a hardware-
independent version of GL in the early 1990s. This graphics package is
now maintained and updated by the OpenGL Architecture Review Board,
which is a consortium of representatives from many graphics companies and
organizations. The OpenGL library is specifically designed for efficient process-
ing of three-dimensional applications, but it can also handle two-dimensional
scene descriptions as a special case of three dimensions where all the z coordinate
values are 0.
Graphics functions in any package are typically defined as a set of specifica-
tions independent of any programming language. A language binding is then
defined for a particular high-level programming language. This binding gives
the syntax for accessing the various graphics functions from that language. Each
language binding is defined to make best use of the corresponding language capa-
bilities and to handle various syntax issues, such as data types, parameter passing,
and errors. Specifications for implementing a graphics package in a particular lan-
guage are set by the ISO. The OpenGL bindings for the C and C++ languages are
the same. Other OpenGL bindings are also available, such as those for Java and
Python.
Later in this book, we use the C/C++ binding for OpenGL as a framework
for discussing basic graphics concepts and the design and application of graphics
packages. Example programs in C++ illustrate applications of OpenGL and the
general algorithms for implementing graphics functions.
33
Computer Graphics Software
using a variety of lighting models. Finally, graphics libraries are often provided
in other types of systems, such as Mathematica, MatLab, and Maple.
5 Introduction to OpenGL
A basic library of functions is provided in OpenGL for specifying graphics prim-
itives, attributes, geometric transformations, viewing transformations, and many
other operations. As we noted in the last section, OpenGL is designed to be hard-
ware independent, so many operations, such as input and output routines, are
not included in the basic library. However, input and output routines and many
additional functions are available in auxiliary libraries that have been developed
for OpenGL programs.
Certain functions require that one (or more) of their arguments be assigned
a symbolic constant specifying, for instance, a parameter name, a value for a
parameter, or a particular mode. All such constants begin with the uppercase
letters GL. In addition, component words within a constant name are written in
capital letters, and the underscore ( ) is used as a separator between all component
words in the name. The following are a few examples of the several hundred
symbolic constants available for use with OpenGL functions:
The OpenGL functions also expect specific data types. For example, an
OpenGL function parameter might expect a value that is specified as a 32-bit inte-
ger. But the size of an integer specification can be different on different machines.
To indicate a specific data type, OpenGL uses special built-in, data-type names,
such as
Each data-type name begins with the capital letters GL, and the remainder of the
name is a standard data-type designation written in lowercase letters.
Some arguments of OpenGL functions can be assigned values using an array
that lists a set of data values. This is an option for specifying a list of values as a
pointer to an array, rather than specifying each element of the list explicitly as a
parameter argument. A typical example of the use of this option is in specifying
xyz coordinate values.
Related Libraries
In addition to the OpenGL basic (core) library, there are a number of associ-
ated libraries for handling special operations. The OpenGL Utility (GLU) pro-
vides routines for setting up viewing and projection matrices, describing complex
objects with line and polygon approximations, displaying quadrics and B-splines
34
Computer Graphics Software
Header Files
In all of our graphics programs, we will need to include the header file for the
OpenGL core library. For most applications we will also need GLU, and on many
systems we will need to include the header file for the window system. For
instance, with Microsoft Windows, the header file that accesses the WGL rou-
tines is windows.h. This header file must be listed before the OpenGL and GLU
header files because it contains macros needed by the Microsoft Windows version
of the OpenGL libraries. So the source file in this case would begin with
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
(We could include gl.h and glu.h as well, but doing so would be redundant and
could affect program portability.) On some systems, the header files for OpenGL
and GLUT routines are found in different places in the filesystem. For instance,
on Apple OS X systems, the header file inclusion statement would be
#include <GLUT/glut.h>
35
Computer Graphics Software
In addition, we will often need to include header files that are required by the
C++ code. For example,
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
With the ISO/ANSI standard for C++, these header files are called cstdio, cst-
dlib, and cmath.
Next, we can state that a display window is to be created on the screen with
a given caption for the title bar. This is accomplished with the function
where the single argument for this function can be any character string that we
want to use for the display-window title.
Then we need to specify what the display window is to contain. For this,
we create a picture using OpenGL functions and pass the picture definition to
the GLUT routine glutDisplayFunc, which assigns our picture to the display
window. As an example, suppose we have the OpenGL code for describing a line
segment in a procedure called lineSegment. Then the following function call
passes the line-segment description to the display window:
glutDisplayFunc (lineSegment);
But the display window is not yet on the screen. We need one more GLUT
function to complete the window-processing operations. After execution of the
following statement, all display windows that we have created, including their
graphic content, are now activated:
glutMainLoop ( );
This function must be the last one in our program. It displays the initial graphics
and puts the program into an infinite loop that checks for input from devices such
as a mouse or keyboard. Our first example will not be interactive, so the program
will just continue to display our picture until we close the display window. In
later chapters, we consider how we can modify our OpenGL programs to handle
interactive input.
Although the display window that we created will be in some default location
and size, we can set these parameters using additional GLUT functions. We use the
glutInitWindowPosition function to give an initial location for the upper-
left corner of the display window. This position is specified in integer screen
36
Computer Graphics Software
Vid
eo s
100 cree
50 n
An E
xam
ple O
penG
Display L Pr
Window ogra
m
300
400
FIGURE 2
A 400 by 300 display window at position (50,
100) relative to the top-left corner of the video
display.
coordinates, whose origin is at the upper-left corner of the screen. For instance,
the following statement specifies that the upper-left corner of the display window
should be placed 50 pixels to the right of the left edge of the screen and 100 pixels
down from the top edge of the screen:
After the display window is on the screen, we can reposition and resize it.
We can also set a number of other options for the display window, such as
buffering and a choice of color modes, with the glutInitDisplayMode func-
tion. Arguments for this routine are assigned symbolic GLUT constants. For exam-
ple, the following command specifies that a single refresh buffer is to be used for
the display window and that we want to use the color mode which uses red,
green, and blue (RGB) components to select color values:
The values of the constants passed to this function are combined using a logical or
operation. Actually, single buffering and RGB color mode are the default options.
But we will use the function now as a reminder that these are the options that
are set for our display. Later, we discuss color modes in more detail, as well as
other display options, such as double buffering for animation applications and
selecting parameters for viewing three-dimensional scenes.
37
Computer Graphics Software
Using RGB color values, we set the background color for the display window
to be white, as in Figure 2, with the OpenGL function:
The first three arguments in this function set the red, green, and blue component
colors to the value 1.0, giving us a white background color for the display window.
If, instead of 1.0, we set each of the component colors to 0.0, we would get a
black background. And if all three of these components were set to the same
intermediate value between 0.0 and 1.0, we would get some shade of gray. The
fourth parameter in the glClearColor function is called the alpha value for the
specified color. One use for the alpha value is as a “blending” parameter. When we
activate the OpenGL blending operations, alpha values can be used to determine
the resulting color for two overlapping objects. An alpha value of 0.0 indicates a
totally transparent object, and an alpha value of 1.0 indicates an opaque object.
Blending operations will not be used for a while, so the value of alpha is irrelevant
to our early example programs. For now, we will simply set alpha to 0.0.
Although the glClearColor command assigns a color to the display win-
dow, it does not put the display window on the screen. To get the assigned window
color displayed, we need to invoke the following OpenGL function:
glClear (GL_COLOR_BUFFER_BIT);
The suffix 3f on the glColor function indicates that we are specifying the three
RGB color components using floating-point (f) values. This function requires that
the values be in the range from 0.0 to 1.0, and we have set red = 0.0, green = 0.4,
and blue = 0.2.
For our first program, we simply display a two-dimensional line segment.
To do this, we need to tell OpenGL how we want to “project” our picture onto
the display window because generating a two-dimensional picture is treated by
OpenGL as a special case of three-dimensional viewing. So, although we only
want to produce a very simple two-dimensional line, OpenGL processes our
picture through the full three-dimensional viewing operations. We can set the
projection type (mode) and other viewing parameters that we need with the fol-
lowing two functions:
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
38
Computer Graphics Software
within this world-coordinate rectangle will be shown within the display win-
dow. Anything outside this coordinate range will not be displayed. Therefore,
the GLU function gluOrtho2D defines the coordinate reference frame within the
display window to be (0.0, 0.0) at the lower-left corner of the display window and
(200.0, 150.0) at the upper-right window corner. Since we are only describing
a two-dimensional object, the orthogonal projection has no other effect than to
“paste” our picture into the display window that we defined earlier. For now, we
will use a world-coordinate rectangle with the same aspect ratio as the display
window, so that there is no distortion of our picture. Later, we will consider how
we can maintain an aspect ratio that does not depend upon the display-window
specification.
Finally, we need to call the appropriate OpenGL routines to create our line seg-
ment. The following code defines a two-dimensional, straight-line segment with
integer, Cartesian endpoint coordinates (180, 15) and (10, 145).
glBegin (GL_LINES);
glVertex2i (180, 15);
glVertex2i (10, 145);
glEnd ( );
Now we are ready to put all the pieces together. The following OpenGL
program is organized into three functions. We place all initializations and related
one-time parameter settings in function init. Our geometric description of the
“picture” that we want to display is in function lineSegment, which is the
function that will be referenced by the GLUT function glutDisplayFunc. And
the main function contains the GLUT functions for setting up the display window
and getting our line segment onto the screen. Figure 3 shows the display window
and line segment generated by this program.
FIGURE 3
The display window and line segment
produced by the example program.
39
Computer Graphics Software
40
Computer Graphics Software
T A B L E 1
OpenGL Error Codes
GLenum code;
This call returns the current error code and clears the internal error flag. If the
returned value is equal to the OpenGL symbolic constant GL NO ERROR, every-
thing is fine. Any other return value indicates that a problem has occurred.
The base OpenGL library defines a number of symbolic constants which
represent different error conditions; the ones which occur most often are listed
in Table 1. The GLU library also defines a number of error codes, but
most of them have almost meaningless names such as GLU NURBS ERROR1,
GLU NURBS ERROR2, and so on. (These are not actually meaningless names, but
their meaning won’t become clear until we discuss more advanced concepts in
later chapters.)
These symbolic constants are helpful, but printing them out directly is not
particularly informative. Fortunately, the GLU library contains a function that
returns a descriptive string for each of the GLU and GL errors. To use it, we first
retrieve the current error code, and then pass it as a parameter to this function.
The return value can be printed out using, for example, the C standard library
fprintf function:
#include <stdio.h>
GLenum code;
41