📄 chapter01.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-2">
<META NAME="GENERATOR" CONTENT="Mozilla/4.07 [en] (Win98; I) [Netscape]">
<META NAME="Author" CONTENT="Goran UnreaL Krajnovic">
<TITLE>Chapter 1 - OpenGL Programming Guide (Addison-Wesley Publishing Company)</TITLE>
</HEAD>
<BODY BGCOLOR="#EFEFEF" LINK="#0000FF" VLINK="#551A8B" ALINK="#FF0000">
<DIV ALIGN=right><IMG SRC="figures/SGI_ID.gif" ALT="Silicon Graphics" NOSAVE HEIGHT=43 WIDTH=151 ALIGN=TEXTTOP></DIV>
<HR>
<H1>
Chapter 1<BR>
Introduction to OpenGL</H1>
<B>Chapter Objectives</B>
<P>After reading this chapter, you'll be able to do the following:
<UL>Appreciate in general terms what OpenGL offers
<BR>
<P>Identify different levels of rendering complexity
<BR>
<P>Understand the basic structure of an OpenGL program
<BR>
<P>Recognize OpenGL command syntax
<BR>
<P>Understand in general terms how to animate an OpenGL program</UL>
This chapter introduces OpenGL. It has the following major sections:
<UL>"What Is OpenGL?" explains what OpenGL is, what it does and doesn't
do, and how it works.
<BR>
<P>"A Very Simple OpenGL Program" presents a small OpenGL program and briefly
discusses it. This section also defines a few basic computer-graphics terms.
<BR>
<P>"OpenGL Command Syntax" explains some of the conventions and notations
used by OpenGL commands.
<BR>
<P>"OpenGL as a State Machine" describes the use of state variables in
OpenGL and the commands for querying, enabling, and disabling states.
<BR>
<P>"OpenGL-related Libraries" describes sets of OpenGL-related routines,
including an auxiliary library specifically written for this book to simplify
programming examples.
<BR>
<P>"Animation" explains in general terms how to create pictures on the
screen that move, or animate.</UL>
<HR>
<BR>
<H2>
What Is OpenGL?</H2>
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.
<P>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 <I>protocol</I>)
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.
<P>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.
With OpenGL, you must build up your desired model from a small set of geometric
primitive - 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.)
<P>Now that you know what OpenGL <I>doesn't</I> do, here's what it <I>does</I>
do. Take a look at the color plates - they illustrate typical uses of OpenGL.
They show the scene on the cover of this book, drawn by a computer (which
is to say, <I>rendered</I>) in successively more complicated ways. The
following paragraphs describe in general terms how these pictures were
made.
<UL>Figure J-1 shows the entire scene displayed as a wireframe model -
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.
<P>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.
<BR>
<P>Figure J-2 shows a <I>depth-cued</I> 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 of depth.
<BR>
<P>Figure J-3 shows an <I>antialiased</I> 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.
<BR>
<P>Figure J-4 shows a <I>flat-shaded</I> 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.
<BR>
<P>Figure J-5 shows a <I>lit, smooth-shaded</I> 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.
<BR>
<P>Figure J-6 adds <I>shadows</I> and <I>textures</I> 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.
<BR>
<P>Figure J-7 shows a <I>motion-blurred</I> 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.
<BR>
<P>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.</UL>
The next two color images illustrate yet more complicated visual effects
that can be achieved with OpenGL:
<UL>Figure J-9 illustrates the use of atmospheric effects (collectively
referred to as fog) to show the presence of particles in the air.
<BR>
<P>Figure J-10 shows the <I>depth-of-field effect</I>, 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.</UL>
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.
<OL>Construct shapes from geometric primitives, thereby creating mathematical
descriptions of objects. (OpenGL considers points, lines, polygons, images,
and bitmaps to be primitives.)
<BR>
<P>Arrange the objects in three-dimensional space and select the desired
vantage point for viewing the composed scene.
<BR>
<P>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.
<BR>
<P>Convert the mathematical description of objects and their associated
color information to pixels on the screen. This process is called <I>rasterization</I>.</OL>
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.
<P>
<HR>
<H2>
A Very Simple OpenGL Program</H2>
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.
<P>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 <I>models</I>, or objects, are constructed from
geometric primitives - points, lines, and polygons - that are specified
by their vertices.
<P>The final rendered image consists of pixels drawn on the screen; a pixel
- short for picture element - 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.
<P>Now look at an OpenGL program. Example 1-1 renders a white rectangle
on a black background, as shown in Figure 1-1 .
<P><IMG SRC="figures/fig01-01.gif" ALT="[IMAGE]" NOSAVE HEIGHT=269 WIDTH=251>
<P><B>Figure 1-1 : </B>A White Rectangle on a Black Background
<BR>
<BR>
<P><B>Example 1-1 : </B>A Simple OpenGL Program
<PRE>#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();
}</PRE>
The first line of the <B>main()</B> routine opens a window on the screen:
The <B>OpenAWindowPlease()</B> 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: <B>glClearColor()</B> establishes what
color the window will be cleared to, and <B>glClear()</B> actually clears
the window. Once the color to clear to is set, the window is cleared to
that color whenever <B>glClear()</B> is called. The clearing color can
be changed with another call to <B>glClearColor()</B>. Similarly, the <B>glColor3f()</B>
command establishes what color to use for drawing objects - 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.
<P>The next OpenGL command used in the program, <B>glOrtho()</B>, 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 <B>glBegin()</B> and <B>glEnd()</B>, define the object to be drawn -
in this example, a polygon with four vertices. The polygon's "corners"
are defined by the <B>glVertex2f()</B> commands. As you might be able to
guess from the arguments, which are (<I>x, y</I>) coordinate pairs, the
polygon is a rectangle.
<P>Finally, <B>glFlush()</B> ensures that the drawing commands are actually
executed, rather than stored in a buffer awaiting additional OpenGL commands.
The <B>KeepTheWindowOnTheScreenForAWhile()</B> placeholder routine forces
the picture to remain on the screen instead of immediately disappearing.
<P>
<HR>
<H2>
OpenGL Command Syntax</H2>
As you might have observed from the simple program in the previous section,
OpenGL commands use the prefix <B>gl</B> and initial capital letters for
each word making up the command name (recall <B>glClearColor()</B>, for
example). Similarly, OpenGL defined constants begin with GL_, use all capital
letters, and use underscores to separate words (like GL_COLOR_BUFFER_BIT).
<P>You might also have noticed some seemingly extraneous letters appended
to some command names (the <B>3f</B> in <B>glColor3f()</B>, for example).
It's true that the <B>Color</B> 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 <B>3</B> part of the suffix indicates that three arguments
are given; another version of the <B>Color</B> command takes four arguments.
The <B>f</B> 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -