OpenGL_Programming_Guide.pdf

(1165 KB) Pobierz
104398377 UNPDF
Chapter 13, "Now That You Know," describes how to use OpenGL in several clever and
unexpected ways to produce interesting results. These techniques are drawn from years of
experience with the technological precursor to OpenGL, the Silicon Graphics IRIS Graphics
Library.
In addition, there are several appendices that you will likely find useful:
·
OpenGL Programming Guide
About This Guide
The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for
Graphics Library.) It allows you to create interactive programs that produce color images of moving
three−dimensional objects. With OpenGL, you can control computer−graphics technology to produce
realistic pictures or ones that depart from reality in imaginative ways. This guide explains how to
program with the OpenGL graphics system to deliver the visual effect you want.
Appendix A, "Order of Operations," gives a technical overview of the operations OpenGL
performs, briefly describing them in the order in which they occur as an application executes.
·
Appendix B, "OpenGL State Variables," lists the state variables that OpenGL maintains and
describes how to obtain their values.
·
Appendix C, "The OpenGL Utility Library," briefly describes the routines available in the
OpenGL Utility Library.
·
Appendix D, "The OpenGL Extension to the X Window System," briefly describes the
routines available in the OpenGL extension to the X Window System.
What This Guide Contains
This guide has the ideal number of chapters: 13. The first six chapters present basic information that
you need to understand to be able to draw a properly colored and lit three−dimensional object on the
screen:
·
·
Appendix E, "The OpenGL Programming Guide Auxiliary Library," discusses a small C code
library that was written for this book to make code examples shorter and more comprehensible.
·
Appendix F, "Calculating Normal Vectors," tells you how to calculate normal vectors for
different types of geometric objects.
·
Appendix G, "Homogeneous Coordinates and Transformation Matrices," explains some of
the mathematics behind matrix transformations.
Chapter 1, "Introduction to OpenGL," provides a glimpse into the kinds of things OpenGL can
do. It also presents a simple OpenGL program and explains essential programming details you
need to know for subsequent chapters.
·
Appendix H, "Programming Tips," lists some programming tips based on the intentions of the
designers of OpenGL that you might find useful.
·
·
Chapter 2, "Drawing Geometric Objects," explains how to create a three−dimensional
geometric description of an object that is eventually drawn on the screen.
Appendix I, "OpenGL Invariance," describes the pixel−exact invariance rules that OpenGL
implementations follow.
·
·
Chapter 3, "Viewing," describes how such three−dimensional models are transformed before being
drawn onto a two−dimensional screen. You can control these transformations to show a particular
view of a model.
Appendix J, "Color Plates," contains the color plates that appear in the printed version of this
guide.
Finally, an extensive Glossary defines the key terms used in this guide.
·
Chapter 4, "Display Lists," discusses how to store a series of OpenGL commands for execution at
a later time. You’ll want to use this feature to increase the performance of your OpenGL program.
·
Chapter 5, "Color," describes how to specify the color and shading method used to draw an object.
How to Obtain the Sample Code
This guide contains many sample programs to illustrate the use of particular OpenGL programming
techniques. These programs make use of a small auxiliary library that was written for this guide. The
section "OpenGL−related Libraries" gives more information about this auxiliary library. You can
obtain the source code for both the sample programs and the auxiliary library for free via ftp
(file−transfer protocol) if you have access to the Internet.
First, use ftp to go to the host sgigate.sgi.com, and use anonymous as your user name and your_name @
machine as the password. Then type the following:
cd pub/opengl
binary
get opengl.tar.Z
bye
The file you receive is a compressed tar archive. To restore the files, type:
uncompress opengl.tar
tar xf opengl.tar
Chapter 6, "Lighting," explains how to control the lighting conditions surrounding an object and
how that object responds to light (that is, how it reflects or absorbs light). Lighting is an important
topic, since objects usually don’t look three−dimensional until they’re lit.
The remaining chapters explain how to add sophisticated features to your three−dimensional scene.
You might choose not to take advantage of many of these features until you’re more comfortable with
OpenGL. Particularly advanced topics are noted in the text where they occur.
·
Chapter 7, "Blending, Antialiasing, and Fog," describes techniques essential to creating a
realistic scene
alpha blending (which allows you to create transparent objects), antialiasing, and
atmospheric effects (such as fog or smog).
·
Chapter 8, "Drawing Pixels, Bitmaps, Fonts, and Images," discusses how to work with sets of
two−dimensional data as bitmaps or images. One typical use for bitmaps is to describe characters
in fonts.
·
Chapter 9, "Texture Mapping," explains how to map one− and two−dimensional images called
textures onto three−dimensional objects. Many marvelous effects can be achieved through texture
mapping.
·
Chapter 10, "The Framebuffer," describes all the possible buffers that can exist in an OpenGL
implementation and how you can control them. You can use the buffers for such effects as
hidden−surface elimination, stenciling, masking, motion blur, and depth−of−field focusing.
·
The sample programs and auxiliary library are created as subdirectories from wherever you are in the
file directory structure.
Many implementations of OpenGL might also include the code samples and auxiliary library as part of
the system. This source code is probably the best source for your implementation, because it might have
been optimized for your system. Read your machine−specific OpenGL documentation to see where the
code samples can be found.
Chapter 11, "Evaluators and NURBS," gives an introduction to advanced techniques for
efficiently generating curves or surfaces.
·
Chapter 12, "Selection and Feedback," explains how you can use OpenGL’s selection
mechanism to select an object on the screen. It also explains the feedback mechanism, which allows
you to collect the drawing information OpenGL produces rather than having it be used to draw on
the screen.
1
2
·
·
¾
What You Should Know Before Reading This Guide
This guide assumes only that you know how to program in the C language and that you have some
background in mathematics (geometry, trigonometry, linear algebra, calculus, and differential
geometry). Even if you have little or no experience with computer−graphics technology, you should be
able to follow most of the discussions in this book. Of course, computer graphics is a huge subject, so
you may want to enrich your learning experience with supplemental reading:
·
Computer Graphics: Principles and Practice by James D. Foley, Andries van Dam, Steven K.
Feiner, and John F. Hughes (Reading, Mass.: Addison−Wesley Publishing Co.)
Bill Glazier, Kipp Hickman, Phil Karlton, Mark Segal, Kevin P. Smith, and Wei Yen. The members of
the OpenGL Architecture Review Board naturally need to be counted among the designers of OpenGL:
Dick Coulter and John Dennis of Digital Equipment Corporation; Jim Bushnell and Linas Vepstas of
International Business Machines, Corp.; Murali Sundaresan and Rick Hodgson of Intel; and On Lee
and Chuck Whitmore of Microsoft. Other early contributors to the design of OpenGL include Raymond
Drewry of Gain Technology, Inc., Fred Fisher of Digital Equipment Corporation, and Randi Rost of
Kubota Pacific Computer, Inc. Many other Silicon Graphics employees helped refine the definition and
functionality of OpenGL, including Momi Akeley, Allen Akin, Chris Frazier, Paul Ho, Simon Hui,
Lesley Kalmin, Pierre Tardiff, and Jim Winget.
Many brave souls volunteered to review this book: Kurt Akeley, Gavin Bell, Sam Chen, Andrew
Cherenson, Dan Fink, Beth Fryer, Gretchen Helms, David Marsland, Jeanne Rich, Mark Segal, Kevin
P. Smith, and Josie Wernecke from Silicon Graphics; David Niguidula, Coalition of Essential Schools,
Brown University; John Dennis and Andy Vesper, Digital Equipment Corporation; Chandrasekhar
Narayanaswami and Linas Vepstas, International Business Machines, Corp.; Randi Rost, Kubota
Pacific; On Lee, Microsoft Corp.; Dan Sears; Henry McGilton, Trilithon Software; and Paula Womak.
Assembling the set of colorplates was no mean feat. The sequence of plates based on the cover image (
Figure J−1 through Figure J−9 ) was created by Thad Beier of Pacific Data Images, Seth Katz of
Xaos Tools, Inc., and Mason Woo of Silicon Graphics. Figure J−10 through Figure J−32 are
snapshots of programs created by Mason. Gavin Bell, Kevin Goldsmith, Linda Roy, and Mark Daly (all
of Silicon Graphics) created the fly−through program used for Figure J−34 . The model for Figure
J−35 was created by Barry Brouillette of Silicon Graphics; Doug Voorhies, also of Silicon Graphics,
performed some image processing for the final image. Figure J−36 was created by John Rohlf and
Michael Jones, both of Silicon Graphics. Figure J−37 was created by Carl Korobkin of Silicon
Graphics. Figure J−38 is a snapshot from a program written by Gavin Bell with contributions from
the Inventor team at Silicon Graphics
This book is an
encyclopedic treatment of the subject of computer graphics. It includes a wealth of information but
is probably best read after you have some experience with the subject.
¾
·
3D Computer Graphics: A User’s Guide for Artists and Designers by Andrew S. Glassner (New York:
Design Press)
This book is a nontechnical, gentle introduction to computer graphics. It focuses on
the visual effects that can be achieved rather than on the techniques needed to achieve them.
Once you begin programming with OpenGL, you might want to obtain the OpenGL Reference Manual
by the OpenGL Architecture Review Board (Reading, Mass.: Addison−Wesley Publishing Co., 1993),
which is designed as a companion volume to this guide. The Reference Manual provides a technical
view of how OpenGL operates on data that describes a geometric object or an image to produce an
image on the screen. It also contains full descriptions of each set of related OpenGL commands
¾
the
parameters used by the commands, the default values for those parameters, and what the commands
accomplish.
"OpenGL" is really a hardware−independent specification of a programming interface. You use a
particular implementation of it on a particular kind of hardware. This guide explains how to program
with any OpenGL implementation. However, since implementations may vary slightly
¾
¾
in performance
Alain Dumesny, Dave Immel, David Mott, Howard Look, Paul
Isaacs, Paul Strauss, and Rikk Carey. Figure J−39 and Figure J−40 are snapshots from a visual
simulation program created by the Silicon Graphics IRIS Performer team
¾
you might want to investigate whether
supplementary documentation is available for the particular implementation you’re using. In addition,
you might have OpenGL−related utilities, toolkits, programming and debugging support, widgets,
sample programs, and demos available to you with your system.
¾
¾
Craig Phillips, John Rohlf,
from a database produced for Silicon Graphics by
Paradigm Simulation, Inc. Figure J−41 is a snapshot from skyfly, the precursor to Performer, which
was created by John Rohlf, Sharon Fischler, and Ben Garlick, all of Silicon Graphics.
Several other people played special roles in creating this book. If we were to list other names as authors
on the front of this book, Kurt Akeley and Mark Segal would be there, as honorary yeoman. They
helped define the structure and goals of the book, provided key sections of material for it, reviewed it
when everybody else was too tired of it to do so, and supplied that all−important humor and support
throughout the process. Kay Maitz provided invaluable production and design assistance. Kathy
Gochenour very generously created many of the illustrations for this book. Tanya Kucak copyedited the
manuscript, in her usual thorough and professional style.
And now, each of the authors would like to take the 15 minutes that have been allotted to them by
Andy Warhol to say thank you.
I’d like to thank my managers at Silicon Graphics
¾
Style Conventions
These style conventions are used in this guide:
·
Bold
¾
Command and routine names, and matrices
·
Italics ¾Variables, arguments, parameter names, spatial dimensions, and matrix components
Enumerated types and defined constants
Code examples are set off from the text in a monospace font, and command summaries are shaded with
gray boxes.
Topics that are particularly complicated
Regular
¾
¾
and that you can skip if you’re new to OpenGL or computer
¾
Dave Larson and Way Ting
¾
and the members of
are marked with the Advanced icon. This icon can apply to a single paragraph or to an entire
section or chapter.
Advanced
Exercises that are left for the reader are marked with the Try This icon.
Try This
¾
Patricia Creek, Arthur Evans, Beth Fryer, Jed Hartman, Ken Jones, Robert Reimann, Eve
Stratton (aka Margaret−Anne Halse), John Stearns, and Josie Wernecke
¾
for their support during this
lengthy process. Last but surely not least, I want to thank those whose contributions toward this
project are too deep and mysterious to elucidate: Yvonne Leach, Kathleen Lancaster, Caroline Rose,
Cindy Kleinfeld, and my parents, Florence and Ferdinand Neider.
¾
¾
JLN
In addition to my parents, Edward and Irene Davis, I’d like to thank the people who taught me most of
what I know about computers and computer graphics
Acknowledgments
No book comes into being without the help of many people. Probably the largest debt the authors owe is
to the creators of OpenGL itself. The OpenGL team at Silicon Graphics has been led by Kurt Akeley,
¾
Doug Engelbart and Jim Clark.
TRD
I’d like to thank the many past and current members of Silicon Graphics whose accommodation and
3
4
and in providing additional, optional features, for example
Sharon Fischler, Jim Helman, and Michael Jones
·
graphics
my group
¾
enlightenment were essential to my contribution to this book: Gerald Anderson, Wendy Chin, Bert
Fornaciari, Bill Glazier, Jill Huchital, Howard Look, Bill Mannel, David Marsland, Dave Orton, Linda
Roy, Keith Seto, and Dave Shreiner. Very special thanks to Karrin Nicol and Leilani Gayles of SGI for
their guidance throughout my career. I also bestow much gratitude to my teammates on the Stanford B
ice hockey team for periods of glorious distraction throughout the writing of this book. Finally, I’d like
to thank my family, especially my mother, Bo, and my late father, Henry.
¾
points,
lines, and polygons. (A sophisticated library that provides these features could certainly be built on top
of OpenGL
¾
in fact, that’s what Open Inventor is. See "OpenGL−related Libraries" for more
information about Open Inventor.)
Now that you know what OpenGL doesn’t do, here’s what it does do. Take a look at the color plates
¾
¾
MW
they illustrate typical uses of OpenGL. They show the scene on the cover of this book, drawn by a
computer (which is to say, rendered ) in successively more complicated ways. The following paragraphs
describe in general terms how these pictures were made.
·
that is, as if all the objects in
the scene were made of wire. Each line of wire corresponds to an edge of a primitive (typically a
polygon). For example, the surface of the table is constructed from triangular polygons that are
positioned like slices of pie.
Note that you can see portions of objects that would be obscured if the objects were solid rather
than wireframe. For example, you can see the entire model of the hills outside the window even
though most of this model is normally hidden by the wall of the room. The globe appears to be
nearly solid because it’s composed of hundreds of colored blocks, and you see the wireframe lines for
all the edges of all the blocks, even those forming the back side of the globe. The way the globe is
constructed gives you an idea of how complex objects can be created by assembling lower−level
objects.
¾
Chapter 1
Introduction to OpenGL
Chapter Objectives
After reading this chapter, you’ll be able to do the following:
·
Appreciate in general terms what OpenGL offers
·
Identify different levels of rendering complexity
·
Understand the basic structure of an OpenGL program
·
Recognize OpenGL command syntax
·
Understand in general terms how to animate an OpenGL program
This chapter introduces OpenGL. It has the following major sections:
·
·
Figure J−2 shows a depth−cued version of the same wireframe scene. Note that the lines farther
from the eye are dimmer, just as they would be in real life, thereby giving a visual cue ofdepth.
"What Is OpenGL?" explains what OpenGL is, what it does and doesn’t do, and how it works.
·
Figure J−3 shows an antialiased version of the wireframe scene. Antialiasing is a technique for
reducing the jagged effect created when only portions of neighboring pixels properly belong to the
image being drawn. Such jaggies are usually the most visible with near−horizontal or near−vertical
lines.
·
"A Very Simple OpenGL Program" presents a small OpenGL program and briefly discusses it.
This section also defines a few basic computer−graphics terms.
·
"OpenGL Command Syntax" explains some of the conventions and notations used by OpenGL
commands.
·
Figure J−4 shows a flat−shaded version of the scene. The objects in the scene are now shown as
solid objects of a single color. They appear "flat" in the sense that they don’t seem to respond to the
lighting conditions in the room, so they don’t appear smoothly rounded.
·
"OpenGL as a State Machine" describes the use of state variables in OpenGL and the commands
for querying, enabling, and disabling states.
·
"OpenGL−related Libraries" describes sets of OpenGL−related routines, including an auxiliary
library specifically written for this book to simplify programming examples.
·
Figure J−5 shows a lit, smooth−shaded version of the scene. Note how the scene looks much more
realistic and three−dimensional when the objects are shaded to respond to the light sources in the
room; the surfaces of the objects now look smoothly rounded.
·
"Animation" explains in general terms how to create pictures on the screen that move, or animate.
·
Figure J−6 adds shadows and textures to the previous version of the scene. Shadows aren’t an
explicitly defined feature of OpenGL (there is no "shadow command"), but you can create them
yourself using the techniques described in Chapter 13 . Texture mapping allows you to apply a
two−dimensional texture to a three−dimensional object. In this scene, the top on the table surface is
the most vibrant example of texture mapping. The walls, floor, table surface, and top (on top of the
table) are all texture mapped.
What Is OpenGL?
OpenGL is a software interface to graphics hardware. This interface consists of about 120 distinct
commands, which you use to specify the objects and operations needed to produce interactive
three−dimensional applications.
OpenGL is designed to work efficiently even if the computer that displays the graphics you create isn’t
the computer that runs your graphics program. This might be the case if you work in a networked
computer environment where many computers are connected to one another by wires capable of
carrying digital data. In this situation, the computer on which your program runs and issues OpenGL
drawing commands is called the client, and the computer that receives those commands and performs
the drawing is called the server. The format for transmitting OpenGL commands (called the protocol )
from the client to the server is always the same, so OpenGL programs can work across a network even
if the client and server are different kinds of computers. If an OpenGL program isn’t running across a
network, then there’s only one computer, and it is both the client and the server.
OpenGL is designed as a streamlined, hardware−independent interface to be implemented on many
different hardware platforms. To achieve these qualities, no commands for performing windowing tasks
or obtaining user input are included in OpenGL; instead, you must work through whatever windowing
system controls the particular hardware you’re using. Similarly, OpenGL doesn’t provide high−level
commands for describing models of three−dimensional objects. Such commands might allow you to
specify relatively complicated shapes such as automobiles, parts of the body, airplanes, or molecules.
·
Figure J−7 shows a motion−blurred object in the scene. The sphinx (or dog, depending on your
Rorschach tendencies) appears to be captured as it’s moving forward, leaving a blurred trace of its
path of motion.
·
Figure J−8 shows the scene as it’s drawn for the cover of the book from a different viewpoint. This
plate illustrates that the image really is a snapshot of models of three−dimensional objects.
The next two color images illustrate yet more complicated visual effects that can be achieved with
OpenGL:
·
Figure J−9 illustrates the use of atmospheric effects (collectively referred to asfog) to show the
presence of particles in the air.
Figure J−10 shows the depth−of−field effect , which simulates the inability of a camera lens to
maintain all objects in a photographed scene in focus. The camera focuses on a particular spot in
the scene, and objects that are significantly closer or farther than that spot are somewhat blurred.
The color plates give you an idea of the kinds of things you can do with the OpenGL graphics system.
The next several paragraphs briefly describe the order in which OpenGL performs the major graphics
operations necessary to render an image on the screen. Appendix A, "Order of Operations"
describes this order of operations in more detail.
5
6
With OpenGL, you must build up your desired model from a small set of geometric primitive
Figure J−1 shows the entire scene displayed as a wireframe model
·
1. Construct shapes from geometric primitives, thereby creating mathematical descriptions of objects.
(OpenGL considers points, lines, polygons, images, and bitmaps to be primitives.)
2. Arrange the objects in three−dimensional space and select the desired vantage point for viewing the
composed scene.
3. Calculate the color of all the objects. The color might be explicitly assigned by the application,
determined from specified lighting conditions, or obtained by pasting a texture onto the objects.
4. Convert the mathematical description of objects and their associated color information to pixels on
the screen. This process is called rasterization .
During these stages, OpenGL might perform other operations, such as eliminating parts of objects that
are hidden by other objects (the hidden parts won’t be drawn, which might increase performance). In
addition, after the scene is rasterized but just before it’s drawn on the screen, you can manipulate the
pixel data if you want.
A Very Simple OpenGL Program
Because you can do so many things with the OpenGL graphics system, an OpenGL program can be
complicated. However, the basic structure of a useful program can be simple: Its tasks are to initialize
certain states that control how OpenGL renders and to specify objects to be rendered.
Before you look at an OpenGL program, let’s go over a few terms. Rendering, which you’ve already seen
used, is the process by which a computer creates images from models. These models , or objects, are
constructed from geometric primitives
¾
points, lines, and polygons
¾
is
the smallest visible element the display hardware can put on the screen. Information about the pixels
(for instance, what color they’re supposed to be) is organized in system memory into bitplanes. A
bitplane is an area of memory that holds one bit of information for every pixel on the screen; the bit
might indicate how red a particular pixel is supposed to be, for example. The bitplanes are themselves
organized into a framebuffer, which holds all the information that the graphics display needs to control
the intensity of all the pixels on the screen.
Now look at an OpenGL program. Example 1−1 renders a white rectangle on a black background, as
shown in Figure 1−1 .
¾
short for picture element
¾
Figure 1−1 A White Rectangle on a Black Background
Example 1−1 A Simple OpenGL Program
#include <whateverYouNeed.h>
main() {
OpenAWindowPlease();
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glOrtho(−1.0, 1.0, −1.0, 1.0, −1.0, 1.0);
glBegin(GL_POLYGON);
glVertex2f(−0.5, −0.5);
glVertex2f(−0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, −0.5);
glEnd();
glFlush();
KeepTheWindowOnTheScreenForAWhile();
}
The first line of the main() routine opens a window on the screen: The OpenAWindowPlease() routine is
meant as a placeholder for a window system−specific routine. The next two lines are OpenGL
commands that clear the window to black: glClearColor() establishes what color the window will be
cleared to, and glClear() actually clears the window. Once the color to clear to is set, the window is
7
8
that are specified by their vertices.
The final rendered image consists of pixels drawn on the screen; a pixel
104398377.001.png
¾
in this case, the color is white. All objects drawn after this point use this color, until it’s changed with
another call to set the color.
The next OpenGL command used in the program, glOrtho() , specifies the coordinate system OpenGL
assumes as it draws the final image and how the image gets mapped to the screen. The next calls,
which are bracketed by glBegin() and glEnd() , define the object to be drawn
vector and nonvector versions, but some commands accept only individual arguments and others
require that at least some of the arguments be specified as a vector. The following lines show how you
might use a vector and a nonvector version of the command that sets the current color:
glColor3f(1.0, 0.0, 0.0);
in this example, a polygon
with four vertices. The polygon’s "corners" are defined by the glVertex2f() commands. As you might be
able to guess from the arguments, which are ( x, y ) coordinate pairs, the polygon is a rectangle.
Finally, glFlush() ensures that the drawing commands are actually executed, rather than stored in a
buffer awaiting additional OpenGL commands. The KeepTheWindowOnTheScreenForAWhile()
placeholder routine forces the picture to remain on the screen instead of immediately disappearing.
¾
float color_array[] = {1.0, 0.0, 0.0};
glColor3fv(color_array);
In the rest of this guide (except in actual code examples), OpenGL commands are referred to by their
base names only, and an asterisk is included to indicate that there may be more to the command name.
For example, glColor*() stands for all variations of the command you use to set the current color. If we
want to make a specific point about one version of a particular command, we include the suffix
necessary to define that version. For example, glVertex*v() refers to all the vector versions of the
command you use to specify vertices.
Finally, OpenGL defines the constant GLvoid; if you’re programming in C, you can use this instead of
void.
OpenGL Command Syntax
As you might have observed from the simple program in the previous section, OpenGL commands use
the prefix gl and initial capital letters for each word making up the command name (recall
glClearColor() , for example). Similarly, OpenGL defined constants begin with GL_, use all capital
letters, and use underscores to separate words (like GL_COLOR_BUFFER_BIT).
You might also have noticed some seemingly extraneous letters appended to some command names
(the 3f in glColor3f() , for example). It’s true that the Color part of the command name is enough to
define the command as one that sets the current color. However, more than one such command has
been defined so that you can use different types of arguments. In particular, the 3 part of the suffix
indicates that three arguments are given; another version of the Color command takes four arguments.
The f part of the suffix indicates that the arguments are floating−point numbers. Some OpenGL
commands accept as many as eight different data types for their arguments. The letters used as
suffixes to specify these data types for ANSI C implementations of OpenGL are shown in Table 1−1 ,
along with the corresponding OpenGL type definitions. The particular implementation of OpenGL that
you’re using might not follow this scheme exactly; an implementation in C++ or Ada, for example,
wouldn’t need to.
SuffixData Type
Typical Corresponding
C−Language Type
OpenGL Type Definition
OpenGL as a State Machine
OpenGL is a state machine. You put it into various states (or modes) that then remain in effect until
you change them. As you’ve already seen, the current color is a state variable. You can set the current
color to white, red, or any other color, and thereafter every object is drawn with that color until you set
the current color to something else. The current color is only one of many state variables that OpenGL
preserves. Others control such things as the current viewing and projection transformations, line and
polygon stipple patterns, polygon drawing modes, pixel−packing conventions, positions and
characteristics of lights, and material properties of the objects being drawn. Many state variables refer
to modes that are enabled or disabled with the command glEnable() or glDisable() .
Each state variable or mode has a default value, and at any point you can query the system for each
variable’s current value. Typically, you use one of the four following commands to do this:
glGetBooleanv(), glGetDoublev() , glGetFloatv() , or glGetIntegerv() . Which of these commands you select
depends on what data type you want the answer to be given in. Some state variables have a more
specific query command (such as glGetLight*() , glGetError() , or glGetPolygonStipple() ). In addition, you
can save and later restore the values of a collection of state variables on an attribute stack with the
glPushAttrib() and glPopAttrib() commands. Whenever possible, you should use these commands rather
than any of the query commands, since they’re likely to be more efficient.
The complete list of state variables you can query is found in Appendix B . For each variable, the
appendix also lists the glGet*() command that returns the variable’s value, the attribute class to which
it belongs, and the variable’s default value.
b
8−bit integer
signed char
GLbyte
s
16−bit integer
short
GLshort
i
32−bit integer
long
GLint, GLsizei
f
32−bit floating−point
float
GLfloat, GLclampf
d
64−bit floating−point
double
GLdouble, GLclampd
ub 8−bit unsigned integer
unsigned char
GLubyte, GLboolean
us 16−bit unsigned integer
unsigned short
GLushort
ui 32−bit unsigned integer
unsigned long
GLuint, GLenum, GLbitfield
OpenGL−related Libraries
OpenGL provides a powerful but primitive set of rendering commands, and all higher−level drawing
must be done in terms of these commands. Therefore, you might want to write your own library on top
of OpenGL to simplify your programming tasks. Also, you might want to write some routines that allow
an OpenGL program to work easily with your windowing system. In fact, several such libraries and
routines have already been written to provide specialized features, as follows. Note that the first two
libraries are provided with every OpenGL implementation, the third was written for this book and is
available using ftp, and the fourth is a separate product that’s based on OpenGL.
·
Table 1−1 Command Suffixes and Argument Data Types
Thus, the two commands
glVertex2i(1, 3);
glVertex2f(1.0, 3.0);
are equivalent, except that the first specifies the vertex’s coordinates as 32−bit integers and the second
specifies them as single−precision floating−point numbers.
Some OpenGL commands can take a final letter v , which indicates that the command takes a pointer to
a vector (or array) of values rather than a series of individual arguments. Many commands have both
vector and nonvector versions, but some commands accept only individual arguments and others
The OpenGL Utility Library (GLU) contains several routines that use lower−level OpenGL
commands to perform such tasks as setting up matrices for specific viewing orientations and
projections, performing polygon tessellation, and rendering surfaces. This library is provided as
9
10
cleared to, and glClear() actually clears the window. Once the color to clear to is set, the window is
cleared to that color whenever glClear() is called. The clearing color can be changed with another call to
glClearColor() . Similarly, the glColor3f() command establishes what color to use for drawing objects
 
Zgłoś jeśli naruszono regulamin