CCE 131-Lecture 3
CCE 131-Lecture 3
Software
Lec 02
Dr Adel Khaled
Outlines
• Coordinate Representations
• Graphics Functions
• Software Standards
• Other Graphics Packages
• Introduction to OpenGL
• Summary
Introduction
• There are two broad classifications for computer-graphics software: special- purpose packages
and general programming packages.
• Special-purpose packages are designed for nonprogrammers who want to generate pictures,
graphs, or charts in some application area without worrying about the graphics procedures that
might be needed to produce such displays.
• The interface to a special-purpose package is typically a set of menus that allow users to
communicate with the programs in their own terms.
• Examples of such applications include artists' painting programs and various architectural,
business, medical, and engineering CAD systems.
Introduction
Introduction
Introduction
Introduction
• 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.
• Normalized coordinates are also referred to as normalized device coordinates, since using this representation
makes a graphics package independent of the coordinate range for any specific output device.
• We also need to identify visible surfaces and eliminate picture parts outside the bounds for the view we want to
show on the display device.
• Finally, the picture is scan-converted into the refresh buffer of a raster system for display.
• The coordinate systems for display devices are generally called device coordinates, or screen coordinates in
the case of a video monitor.
Coordinate Representations
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 according to whether they deal with graphics output,
input, attributes, transformations, viewing, subdividing pictures, or general control.
➢ 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. Chapter
3.
➢ In addition, some graphics packages provide functions for displaying more complex shapes such as spheres,
cones, and cylinders. Routines for generating output primitives provide the basic tools for constructing pictures.
Graphics Functions
• Attributes are properties of the output primitives; that is, an attribute describes how a particular primitive is to be
displayed. This includes color specifications, line styles, text styles, and area-filling patterns
• Geometric transformations. We can change the size, position, or orientation of an object within a scene using.
• Some graphics packages provide an additional set of functions for performing modeling transformations,
which are used to construct 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 packages simply provide the geometric-transformation routines and leave modeling details to the
programmer.
Graphics Functions
• 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.
• Input functions are used to control and process the data flow from these interactive devices.
• Interactive graphics applications use various kinds of input devices, including a mouse, a tablet, and a
joystick.
• 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.
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.
Software Standards
• 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 the American National Standards Institute
(ANSI).
• Although GKS was originally designed as a two-dimensional graphics package, a three- dimensional GKS
extension was soon developed.
Software Standards
• 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 rendering, 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.
Software Standards
• 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
• The OpenGL library is specifically designed for efficient processing 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 Graphics functions in any package are typically defined as a set of specifications independent of any
programming language.
Software Standards
• Function names in the OpenGL basic library (also called the OpenGL core library) are prefixed with
gl, and each component word within a function name has its first letter capitalized.
• The following examples illustrate this naming convention:
glClear : Bitwise OR of masks that indicate the buffers to be cleared. The three masks are
GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, and GL_STENCIL_BUFFER_BIT.
glPolygonMode: is an OpenGL function that controls how polygons are interpreted and rendered
during rasterization. Rasterization is the process of converting geometric data (like polygons) into
pixels or fragments on the screen.
Introduction to OpenGL
Basic OpenGL Syntax
• 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 integer.
• 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
Introduction to OpenGL
Introduction to OpenGL
ptrbits is the bitdepth of a CPU pointer address. Therefore, these types must be large enough to
store a pointer: sizeof(void*).
Introduction to OpenGL
Related Libraries
• In addition to the OpenGL basic (core) library, there are a number of associated libraries for handling special operations.
• The OpenGL Utility (GLU) provides routines for setting up viewing and projection matrices, describing complex objects
with line and polygon approximations, displaying quadrics and B-splines using linear approximations, processing the
surface-rendering operations, and other complex tasks.
• Built on-top of the core OpenGL to provide important utilities (such as setting camera view and projection).
• Every OpenGL implementation includes the GLU library, and all GLU function names start with the prefix glu.
• The OpenGL Extension to the X Window System (GLX) provides a set of routines that are prefixed with the
letters glX.
• Apple systems can use the Apple GL (AGL) interface for window-management operations.
• Function names for this library are prefixed with agl.
• For Microsoft Windows systems, the WGL routines provide a Windows-to-OpenGL interface.
• These routines are prefixed with the letters wgl.
• The Presentation Manager to OpenGL (PGL) is an interface for the IBM OS/2, which uses the prefix pgl
for the library routines.
• The OpenGL Utility Toolkit (GLUT) provides a library of functions for interacting with any screen-
windowing system.
• The GLUT library functions are prefixed with glut, and this library also contains methods for describing and
rendering quadric curves and surfaces.
Introduction to OpenGL
Related Libraries
• Since GLUT is an interface to other device-specific window systems, we can use it so that our
programs will be device-independent.
• Information regarding the latest version of GLUT and download procedures for the source code are
available at the following web site:.
http://www.opengl.org/resources/libraries/glut/
Introduction to OpenGL
Related Libraries
OpenGL Extension Wrangler Library (GLEW): "GLEW is a cross-platform open-source C/C++ extension
loading library. GLEW provides efficient run-time mechanisms for determining which OpenGL extensions
are supported on the target platform.
• 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/Glut, 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 routines 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>
Introduction to OpenGL
Header Files
However, if we use GLUT to handle the window-managing operations, we do not need to include gl.h and
glu.h because GLUT ensures that these will be included correctly. Thus, we can replace the header files
for OpenGL and GLU with
. For instance, on Apple OS X systems, the header file inclusion statement would be
#include <GLUT/glut.h>
Introduction to OpenGL
Header Files
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, cstdlib, and cmath.
stdio This library uses what are called streams to operate with physical devices such as keyboards,
printers, terminals or with any other type of files supported by the system.
stdlib This header defines several general purpose functions, including dynamic memory management,
random number generation, communication with the environment, integer arithmetics, searching, sorting
and converting.
Introduction to OpenGL
Display-Window Management Using GLUT
• To get started, we can consider a simplified, minimal number of operations for displaying a picture. Since
we are using the OpenGL Utility Toolkit, our first step is to initialize GLUT.
• This initialization function could also process any command- line arguments, but we will not need to use
these parameters for our first example programs.
• We perform the GLUT initialization with the statement
Usage
argcp
A pointer to the program's unmodified argc variable from main. Upon return, the value pointed to by argcp
will be updated, because glutInit extracts any command line options intended for the GLUT library.
argv
The program's unmodified argv variable from main. Like argcp, the data for argv will be updated because
glutInit extracts any command line options understood by the GLUT library.
Introduction to OpenGL
• Description
• glutInit will initialize the GLUT library and negotiate a session with the window system.
• During this process, glutInit may cause the termination of the GLUT program with an error message to the
user if GLUT cannot be properly initialized.
• Examples of this situation include the failure to connect to the window system, the lack of window system
support for OpenGL, and invalid command line options.
• glutInit also processes command line options, but the specific options parse are window system
dependent.
Introduction to OpenGL
X Implementation Notes
-display DISPLAY
Specify the X server to connect to. If not specified, the value of the DISPLAY environment variable is
used.
-geometry W x H + X + Y
Determines where window's should be created on the screen. The parameter following -geometry
should be formatted as a standard X geometry specification. The effect of using this option is to
change the GLUT initial size and initial position the same as if glutInitWindowSize or
glutInitWindowPosition were called directly.
Introduction to OpenGL
-iconic
Requests all top-level windows be created in an iconic state.
-indirect
Force the use of indirect OpenGL rendering contexts.
-direct
Force the use of direct OpenGL rendering contexts (not all GLX implementations support direct rendering
contexts). A fatal error is generated if direct rendering is not supported by the OpenGL implementation.
If neither -indirect or -direct are used to force a particular behavior, GLUT will attempt to use direct rendering if
possible and otherwise fallback to indirect rendering.
-gldebug
After processing callbacks and/or events, check if there are any OpenGL errors by calling glGetError. If an error is
reported, print out a warning by looking up the error code with gluErrorString. Using this option is helpful in
detecting OpenGL run-time errors.
-sync
Enable synchronous X protocol transactions. This option makes it easier to track down potential X protocol
errors.
Introduction to OpenGL
Display-Window Management Using GLUT
where the single argument for this function can be any character string that we want to
use for the display-window title. ("An Example OpenGL Program");
Introduction to OpenGL
Display-Window Management Using GLUT
• 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);
Introduction to OpenGL
Display-Window Management Using GLUT
• 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 ( );
• . For the display window, we can choose a background color. And we need
to construct a procedure that contains the appropriate OpenGL functions for
the picture that we want to display.
•
• Using RGB color values, we set the background color for the display
window to be white, as in Figure 2, with the OpenGL function:
•
• glClearColor (1.0, 1.0, 1.0, 0.0);
Introduction to OpenGL
A Complete OpenGL Program
• 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. معتم
Introduction to OpenGL
A Complete OpenGL Program
Although the glClearColor command assigns a color to the display window, 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 argument GL_ COLOR_ BUFFER _BIT is an OpenGL symbolic constant specifying that it is the bit
values in the color buffer (refresh buffer) that are to be set to the values indicated in the glClearColor
function.
(OpenGL has several different kinds of buffers that can be manipulated.
Introduction to OpenGL
A Complete OpenGL Program
• In addition to setting the background color for the display window, we can choose a variety of color schemes
for the objects we want to display in a scene.
• For our initial programming example, we will simply set the object color to be a dark green:
•
• glColor3f (0.0, 0.4, 0.2);
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.
Introduction to OpenGL
A Complete OpenGL Program
This specifies that an orthogonal projection is to be used to map the contents of a two-dimensional
rectangular area of world coordinates to the screen, and that the x-coordinate values within this
rectangle range from 0.0 to 200.0, with y-coordinate values ranging from 0.0 to 150.0. Whatever
objects we define window.
Introduction to OpenGL
A Complete OpenGL Program
• Anything outside this coordinate range will not be displayed. gluOrtho2D (0.0, 200.0, 0.0, 150.0);
• 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.
Introduction to OpenGL
A Complete OpenGL Program
• Finally, we need to call the appropriate OpenGL routines to create our line
segment. 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 ( );
#include <GL/glut.h> // (or others, depending on the system in use)
void init (void)
{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void lineSegment (void)
{
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
glColor3f (0.0, 0.4, 0.2); // Set line segment (or any other object) color to green.
glBegin (GL_LINES);
glVertex2i (180, 15); // Specify line-segment geometry.
glVertex2i (10, 145);
glEnd ( );
glFlush ( ); // Process all OpenGL routines as quickly as possible.
}
int main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode. (single buffer with RGB)
glutInitWindowPosition (50, 100); // Set top-left display-window position.
glutInitWindowSize (400, 300); // Set display-window width and height.
glutCreateWindow ("An Example OpenGL Program"); // Create display window.
init ( ); // Execute initialization procedure.
glutDisplayFunc (lineSegment); // Send graphics to display window.
glutMainLoop ( ); // Display everything and wait.
}
Introduction to OpenGL
glFlush
• glFlush: This is simply a routine to force execution of our OpenGL functions, which are stored by
computer systems in buffers in different locations, depending on how OpenGL is implemented.
• On a busy network, for example, there could be delays in processing some buffers. But the call to
glFlush forces all such buffers to be emptied and the OpenGL functions to be processed
Introduction to OpenGL
lineSegment
• The procedure lineSegment that we set up to describe our picture is referred to as a display callback
function.
• And this procedure is described as being “registered” by glutDisplayFunc as the routine to invoke
whenever the display window might need to be redisplayed.
• This can occur, for example, if the display window is moved.
• In subsequent chapters, we will look at other types of callback functions and the associated GLUT
routines that we use to register them.
• In general, OpenGL programs are organized as a set of callback functions that are to be invoked when
certain actions occur
Summary
In this chapter, we surveyed the major features of graphics software systems.
Some software systems, such as CAD packages and paint programs, are designed for particular
applications.
Other software systems provide a library of general graphics routines that can be used within a
programming language such as C++ to generate pictures for any application.
Summary
Standard graphics-programming packages developed and approved through ISO and ANSI are GKS, 3D
GKS, PHIGS, and PHIGS+. Other packages that have evolved into standards are GL and OpenGL. Many
other graphics libraries are available for use in a programming language, including Open Inventor, VRML,
RenderMan, Java 2D, and Java 3D. Other systems, such as Mathematica, MatLab, and Maple, often provide a
set of graphics-programming functions. Normally, graphics-programming packages require coordinate
specifications to be given in Cartesian reference frames. Each object for a scene can be defined in a separate
modeling Cartesian-coordinate system, which is then mapped to a world- coordinate location to construct the
scene. From world coordinates, three- dimensional objects are projected to a two-dimensional plane,
converted to normalized device coordinates, and then transformed to the final display device coordinates. The
transformations from modeling coordinates to normalized device coordinates are independent of particular
output devices that might be used in an application. Device drivers are then used to convert normalized
coordinates to integer device coordinates.
Summary
Functions that are available in graphics programming packages can be divided into the following
categories: graphics output primitives, attributes, geometric and modeling transformations, viewing
transformations, input functions, picture-structuring operations, and control operations.
Summary
The OpenGL system consists of a device-independent set of routines (called the core library), the utility library
(GLU), and the utility toolkit (GLUT). In the auxiliary set of routines provided by GLU, functions are
available for generating complex objects, for parameter specifications in two-dimensional viewing applications,
for dealing with surface-rendering operations, and for performing some other supporting tasks. In GLUT, we
have an extensive set of functions for managing display windows, interacting with screen-window systems, and
for generating some three- dimensional shapes. We can use GLUT to interface with any computer system, or
we can use GLX, Apple GL, WGL, or another system specific software package.