⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter02.html

📁 OpenGl红宝书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!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 2 - 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 2<BR>
Drawing Geometric Objects</H1>
<B>Chapter Objectives</B>
<P>After reading this chapter, you'll be able to do the following:
<UL>Clear the window to an arbitrary color
<BR>&nbsp;
<P>Draw with any geometric primitive - points, lines, and polygons - in
two or three dimensions
<BR>&nbsp;
<P>Control the display of those primitives - for example, draw dashed lines
or outlined polygons
<BR>&nbsp;
<P>Specify normal vectors at appropriate points on the surface of solid
objects
<BR>&nbsp;
<P>Force any pending drawing to complete</UL>
Although you can draw complex and interesting pictures using OpenGL, they're
all constructed from a small number of primitive graphical items. This
shouldn't be too surprising - look at what Leonardo da Vinci accomplished
with just pencils and paintbrushes.
<P>At the highest level of abstraction, there are three basic drawing operations:
clearing the window, drawing a geometric object, and drawing a raster object.
Raster objects, which include such things as two-dimensional images, bitmaps,
and character fonts, are covered in Chapter 8 . In this chapter, you learn
how to clear the screen and to draw geometric objects, including points,
straight lines, and flat polygons.
<P>You might think to yourself, "Wait a minute. I've seen lots of computer
graphics in movies and on television, and there are plenty of beautifully
shaded curved lines and surfaces. How are those drawn, if all OpenGL can
draw are straight lines and flat polygons?" Even the image on the cover
of this book includes a round table and objects on the table that have
curved surfaces. It turns out that all the curved lines and surfaces you've
seen are approximated by large numbers of little flat polygons or straight
lines, in much the same way that the globe on the cover is constructed
from a large set of rectangular blocks. The globe doesn't appear to have
a smooth surface because the blocks are relatively large compared to the
globe. Later in this chapter, we show you how to construct curved lines
and surfaces from lots of small geometric primitives.
<P>This chapter has the following major sections:
<UL>"A Drawing Survival Kit" explains how to clear the window and force
drawing to be completed. It also gives you basic information about controlling
the color of geometric objects and about hidden-surface removal.
<BR>&nbsp;
<P>"Describing Points, Lines, and Polygons" shows you what the set of primitive
geometric objects is and how to draw them.
<BR>&nbsp;
<P>"Displaying Points, Lines, and Polygons" explains what control you have
over the details of how primitives are drawn - for example, what diameter
points have, whether lines are solid or dashed, and whether polygons are
outlined or filled.
<BR>&nbsp;
<P>"Normal Vectors" discusses how to specify normal vectors for geometric
objects and (briefly) what these vectors are for.
<BR>&nbsp;
<P>"Some Hints for Building Polygonal Models of Surfaces" explores the
issues and techniques involved in constructing polygonal approximations
to surfaces.</UL>
One thing to keep in mind as you read the rest of this chapter is that
with OpenGL, unless you specify otherwise, every time you issue a drawing
command, the specified object is drawn. This might seem obvious, but in
some systems, you first make a list of things to draw, and when it's complete,
you tell the graphics hardware to draw the items in the list. The first
style is called immediate-mode graphics and is OpenGL's default style.
In addition to using immediate mode, you can choose to save some commands
in a list (called a display list) for later drawing. Immediate-mode graphics
is typically easier to program, but display lists are often more efficient.
Chapter
4 tells you how to use display lists and why you might want to use them.
<P>
<HR>
<H2>
A Drawing Survival Kit</H2>
This section explains how to clear the window in preparation for drawing,
set the color of objects that are to be drawn, and force drawing to be
completed. None of these subjects has anything to do with geometric objects
in a direct way, but any program that draws geometric objects has to deal
with these issues. This section also introduces the concept of hidden-surface
removal, a technique that can be used to draw geometric objects easily.
<H3>
Clearing the Window</H3>
Drawing on a computer screen is different from drawing on paper in that
the paper starts out white, and all you have to do is draw the picture.
On a computer, the memory holding the picture is usually filled with the
last picture you drew, so you typically need to clear it to some background
color before you start to draw the new scene. The color you use for the
background depends on the application. For a word processor, you might
clear to white (the color of the paper) before you begin to draw the text.
If you're drawing a view from a spaceship, you clear to the black of space
before beginning to draw the stars, planets, and alien spaceships. Sometimes
you might not need to clear the screen at all; for example, if the image
is the inside of a room, the entire graphics window gets covered as you
draw all the walls.
<P>At this point, you might be wondering why we keep talking about <I>clearing</I>
the window - why not just draw a rectangle of the appropriate color that's
large enough to cover the entire window? First, a special command to clear
a window can be much more efficient than a general-purpose drawing command.
In addition, as you'll see in Chapter 3 , OpenGL allows you to set the
coordinate system, viewing position, and viewing direction arbitrarily,
so it might be difficult to figure out an appropriate size and location
for a window-clearing rectangle. Also, you can have OpenGL use hidden-surface
removal techniques that eliminate objects obscured by others nearer to
the eye; thus, if the window-clearing rectangle is to be a background,
you must make sure that it's behind all the other objects of interest.
With an arbitrary coordinate system and point of view, this might be difficult.
Finally, on many machines, the graphics hardware consists of multiple buffers
in addition to the buffer containing colors of the pixels that are displayed.
These other buffers must be cleared from time to time, and it's convenient
to have a single command that can clear any combination of them. (All the
possible buffers are discussed in Chapter 10 .)
<P>As an example, these lines of code clear the window to black:
<PRE>glClearColor(0.0, 0.0, 0.0, 0.0);&nbsp;
glClear(GL_COLOR_BUFFER_BIT);</PRE>
The first line sets the clearing color to black, and the next command clears
the entire window to the current clearing color. The single parameter to
<B>glClear()</B>
indicates which buffers are to be cleared. In this case, the program clears
only the color buffer, where the image displayed on the screen is kept.
Typically, you set the clearing color once, early in your application,
and then you clear the buffers as often as necessary. OpenGL keeps track
of the current clearing color as a state variable rather than requiring
you to specify it each time a buffer is cleared.
<P>Chapter 5 and Chapter 10 talk about how other buffers are used. For
now, all you need to know is that clearing them is simple. For example,
to clear both the color buffer and the depth buffer, you would use the
following sequence of commands:
<PRE>glClearColor(0.0, 0.0, 0.0, 0.0);&nbsp;
glClearDepth(0.0);&nbsp;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);</PRE>
In this case, the call to <B>glClearColor()</B> is the same as before,
the <B>glClearDepth()</B> command specifies the value to which every pixel
of the depth buffer is to be set, and the parameter to the <B>glClear()</B>
command now consists of the logical OR of all the buffers to be cleared.
The following summary of <B>glClear()</B> includes a table that lists the
buffers that can be cleared, their names, and the chapter where each type
of buffer is discussed.void <B>glClearColor</B>(GLclampf <B>red</B>, GLclampf
<B>green</B>,
GLclampf <B>blue</B>, GLclampf <B>alpha</B>);
<P>Sets the current clearing color for use in clearing color buffers in
RGBA mode. For more information on RGBA mode, see Chapter 5 . The <B>red</B>,
<B>green</B>, <B>blue</B>, and <B>alpha</B> values are clamped if necessary
to the range [0,1]. The default clearing color is (0, 0, 0, 0), which is
black.
<P>void <B>glClear</B>(GLbitfield <B>mask</B>);
<P>Clears the specified buffers to their current clearing values. The <I>mask</I>
argument is a bitwise-ORed combination of the values listed in Table 2-1
.
<TABLE BORDER CELLPADDING=10 >
<CAPTION ALIGN=TOP><B>Table 2-1 : </B>Clearing Buffers</CAPTION>

<TR ALIGN=LEFT VALIGN=TOP>
<TH>Buffer</TH>

<TH>Name</TH>

<TH>Reference</TH>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>Color buffer</TD>

<TD>GL_COLOR_BUFFER_BIT</TD>

<TD>Chapter 5</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>Depth buffer</TD>

<TD>GL_DEPTH_BUFFER_BIT</TD>

<TD>Chapter 10</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>Accumulation buffer</TD>

<TD>GL_ACCUM_BUFFER_BIT</TD>

<TD>Chapter 10</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>Stencil buffer</TD>

<TD>GL_STENCIL_BUFFER_BIT</TD>

<TD>Chapter 10</TD>
</TR>
</TABLE>

<BR>&nbsp;
<P>Before issuing a command to clear multiple buffers, you have to set
the values to which each buffer is to be cleared if you want something
other than the default color, depth value, accumulation color, and stencil
index. In addition to the <B>glClearColor()</B> and <B>glClearDepth()</B>
commands that set the current values for clearing the color and depth buffers,
<B>glClearIndex()</B>,
<B>glClearAccum()</B>, and <B>glClearStencil()</B> specify the color index,
accumulation color, and stencil index used to clear the corresponding buffers.
See Chapter 5 and Chapter 10 for descriptions of these buffers and their
uses.
<P>OpenGL allows you to specify multiple buffers because clearing is generally
a slow operation, since every pixel in the window (possibly millions) is
touched, and some graphics hardware allows sets of buffers to be cleared
simultaneously. Hardware that doesn't support simultaneous clears performs
them sequentially. The difference between
<PRE>glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);</PRE>
and
<PRE>glClear(GL_COLOR_BUFFER_BIT);&nbsp;
glClear(GL_DEPTH_BUFFER_BIT);</PRE>
is that although both have the same final effect, the first example might
run faster on many machines. It certainly won't run more slowly.
<H3>
Specifying a Color</H3>
With OpenGL, the description of the shape of an object being drawn is independent
of the description of its color. Whenever a particular geometric object
is drawn, it's drawn using the currently specified coloring scheme. The
coloring scheme might be as simple as "draw everything in fire-engine red,"
or might be as complicated as "assume the object is made out of blue plastic,
that there's a yellow spotlight pointed in such and such a direction, and
that there's a general low-level reddish-brown light everywhere else."
In general, an OpenGL programmer first sets the color or coloring scheme,
and then draws the objects. Until the color or coloring scheme is changed,
all objects are drawn in that color or using that coloring scheme. This
method helps OpenGL achieve higher drawing performance than would result
if it didn't keep track of the current color.
<P>For example, the pseudocode
<PRE>set_current_color(red);&nbsp;
draw_object(A);&nbsp;
draw_object(B);&nbsp;
set_current_color(green);&nbsp;
set_current_color(blue);&nbsp;
draw_object(C);</PRE>
draws objects A and B in red, and object C in blue. The command on the
fourth line that sets the current color to green is wasted.
<P>Coloring, lighting, and shading are all large topics with entire chapters
or large sections devoted to them. To draw geometric primitives that can
be seen, however, you need some basic knowledge of how to set the current
color; this information is provided in the next paragraphs. For details
on these topics, see Chapter 5 and Chapter 6 .
<P>To set a color, use the command <B>glColor3f()</B>. It takes three parameters,
all of which are floating-point numbers between 0.0 and 1.0. The parameters
are, in order, the red, green, and blue components of the color. You can
think of these three values as specifying a "mix" of colors: 0.0 means
don't use any of that component, and 1.0 means use all you can of that
component. Thus, the code
<PRE>glColor3f(1.0, 0.0, 0.0);</PRE>
makes the brightest red the system can draw, with no green or blue components.
All zeros makes black; in contrast, all ones makes white. Setting all three
components to 0.5 yields gray (halfway between black and white). Here are
eight commands and the colors they would set:
<PRE>glColor3f(0.0, 0.0, 0.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; black&nbsp;
glColor3f(1.0, 0.0, 0.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; red&nbsp;
glColor3f(0.0, 1.0, 0.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; green&nbsp;
glColor3f(1.0, 1.0, 0.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yellow&nbsp;
glColor3f(0.0, 0.0, 1.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; blue&nbsp;
glColor3f(1.0, 0.0, 1.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; magenta&nbsp;
glColor3f(0.0, 1.0, 1.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cyan&nbsp;
glColor3f(1.0, 1.0, 1.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; white</PRE>
You might have noticed earlier that when you're setting the color to clear
the color buffer, <B>glClearColor()</B> takes four parameters, the first
three of which match the parameters for <B>glColor3f()</B>. The fourth
parameter is the alpha value; it's covered in detail in "Blending." For
now, always set the fourth parameter to 0.0.
<H3>
Forcing Completion of Drawing</H3>
Most modern graphics systems can be thought of as an assembly line, sometimes
called a graphics <I>pipeline</I>. The main central processing unit (CPU)
issues a drawing command, perhaps other hardware does geometric transformations,
clipping occurs, then shading or texturing is performed, and finally, the
values are written into the bitplanes for display (see Appendix A for details
on the order of operations). In high-end architectures, each of these operations
is performed by a different piece of hardware that's been designed to perform
its particular task quickly. In such an architecture, there's no need for

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -