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

📄 chapter02.html

📁 OpenGl红宝书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
example, the following code specifies the vertices for the polygon shown
in Figure 2-5 :
<PRE>glBegin(GL_POLYGON);
&nbsp;&nbsp; glVertex2f(0.0, 0.0);
&nbsp;&nbsp; glVertex2f(0.0, 3.0);
&nbsp;&nbsp; glVertex2f(3.0, 3.0);
&nbsp;&nbsp; glVertex2f(4.0, 1.5);
&nbsp;&nbsp; glVertex2f(3.0, 0.0);
glEnd();</PRE>
<IMG SRC="figures/fig2-5.gif" ALT="[IMAGE]" NOSAVE HEIGHT=104 WIDTH=243>
<P><B>Figure 2-5 : </B>Drawing a Polygon or a Set of Points
<BR>&nbsp;
<BR>&nbsp;
<P>If you had used GL_POINTS instead of GL_POLYGON, the primitive would
have been simply the five points shown in Figure 2-5 . Table 2-2 in the
following function summary for <B>glBegin()</B> lists the ten possible
arguments and the corresponding type of primitive.void <B>glBegin</B>(GLenum
<I>mode</I>);
<P>Marks the beginning of a vertex list that describes a geometric primitive.
The type of primitive is indicated by <I>mode</I>, which can be any of
the values shown in Table 2-2 .
<TABLE BORDER CELLPADDING=10 >
<CAPTION ALIGN=TOP><B>Table 2-2 : </B>Geometric Primitive Names and Meanings</CAPTION>

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

<TH>Meaning</TH>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_POINTS</TD>

<TD>individual points</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_LINES</TD>

<TD>pairs of vertices interpreted as individual line segments</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_POLYGON</TD>

<TD>boundary of a simple, convex polygon</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_TRIANGLES</TD>

<TD>triples of vertices interpreted as triangles</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_QUADS</TD>

<TD>quadruples of vertices interpreted as four-sided polygons</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_LINE_STRIP</TD>

<TD>series of connected line segments</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_LINE_LOOP</TD>

<TD>same as above, with a segment added between last and first vertices</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_TRIANGLE_STRIP</TD>

<TD>linked strip of triangles</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_TRIANGLE_FAN</TD>

<TD>linked fan of triangles</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_QUAD_STRIP</TD>

<TD>linked strip of quadrilaterals</TD>
</TR>
</TABLE>

<BR>&nbsp;
<P>void <B>glEnd</B>(void);
<P>Marks the end of a vertex list.
<P>Figure 2-6 shows examples of all the geometric primitives listed in
Table 2-2 . The paragraphs that follow the figure give precise descriptions
of the pixels that are drawn for each of the objects. Note that in addition
to points, several types of lines and polygons are defined. Obviously,
you can find many ways to draw the same primitive. The method you choose
depends on your vertex data.
<P><IMG SRC="figures/fig2-6.gif" ALT="[IMAGE]" NOSAVE HEIGHT=502 WIDTH=617>
<P><B>Figure 2-6 : </B>Geometric Primitive Types
<BR>&nbsp;
<BR>&nbsp;
<P>As you read the following descriptions, assume that <I>n</I> vertices
(v0, v1, v2, ... , vn-1) are described between a <B>glBegin()</B> and <B>glEnd()</B>
pair.
<DL>
<DT>
GL_POINTS</DT>

<BR>Draws a point at each of the <I>n</I> vertices.
<BR>GL_LINES
<BR>Draws a series of unconnected line segments. Segments are drawn between
v0 and v1, between v2 and v3, and so on. If <I>n</I> is odd, the last segment
is drawn between vn-3 and vn-2, and vn-1 is ignored.
<BR>GL_POLYGON
<BR>Draws a polygon using the points v0, ... , vn-1 as vertices. <I>n</I>
must be at least 3, or nothing is drawn. In addition, the polygon specified
must not intersect itself and must be convex. If the vertices don't satisfy
these conditions, the results are unpredictable.
<BR>GL_TRIANGLES
<BR>Draws a series of triangles (three-sided polygons) using vertices v0,
v1, v2, then v3, v4, v5, and so on. If <I>n</I> isn't an exact multiple
of 3, the final one or two vertices are ignored.
<BR>GL_LINE_STRIP
<BR>Draws a line segment from v0 to v1, then from v1 to v2, and so on,
finally drawing the segment from vn-2 to vn-1. Thus, a total of <I>n</I>-<I>1</I>
line
segments are drawn. Nothing is drawn unless <I>n</I> is larger than 1.
There are no restrictions on the vertices describing a line strip (or a
line loop); the lines can intersect arbitrarily.
<BR>GL_LINE_LOOP
<BR>Same as GL_LINE_STRIP, except that a final line segment is drawn from
vn-1 to v0, completing a loop.
<BR>GL_QUADS
<BR>Draws a series of quadrilaterals (four-sided polygons) using vertices
v0, v1, v2, v3, then v4, v5, v6, v7, and so on. If <I>n</I> isn't a multiple
of 4, the final one, two, or three vertices are ignored.
<BR>GL_QUAD_STRIP
<BR>Draws a series of quadrilaterals (four-sided polygons) beginning with
v0, v1, v3, v2, then v2, v3, v5, v4, then v4, v5, v7, v6, and so on. See
Figure
2-6 . <I>n</I> must be at least 4 before anything is drawn, and if <I>n</I>
is odd, the final vertex is ignored.
<BR>GL_TRIANGLE_STRIP
<BR>Draws a series of triangles (three-sided polygons) using vertices v0,
v1, v2, then v2, v1, v3 (note the order), then v2, v3, v4, and so on. The
ordering is to ensure that the triangles are all drawn with the same orientation
so that the strip can correctly form part of a surface. Figure 2-6 should
make the reason for the ordering obvious. <I>n</I> must be at least 3 for
anything to be drawn.
<BR>GL_TRIANGLE_FAN
<BR>Same as GL_TRIANGLE_STRIP, except that the vertices are v0, v1, v2,
then v0, v2, v3, then v0, v3, v4, and so on. Look at Figure 2-6 .</DL>

<H4>
Restrictions on Using glBegin() and glEnd()</H4>
The most important information about vertices is their coordinates, which
are specified by the <B>glVertex*()</B> command. You can also supply additional
vertex-specific data for each vertex - a color, a normal vector, texture
coordinates, or any combination of these - using special commands. In addition,
a few other commands are valid between a <B>glBegin()</B> and <B>glEnd()</B>
pair. Table 2-3 contains a complete list of such valid commands.
<TABLE BORDER CELLPADDING=10 >
<CAPTION ALIGN=TOP><B>Table 2-3 : </B>Valid Commands between glBegin()
and glEnd()</CAPTION>

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

<TH>Purpose of Command</TH>

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

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glVertex*()</TD>

<TD>set vertex coordinates</TD>

<TD>Chapter 2</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glColor*()</TD>

<TD>set current color</TD>

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

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glIndex*()</TD>

<TD>set current color index</TD>

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

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glNormal*()</TD>

<TD>set normal vector coordinates</TD>

<TD>Chapter 2</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glEvalCoord*()</TD>

<TD>generate coordinates</TD>

<TD>Chapter 11</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glCallList(), glCallLists()</TD>

<TD>execute display list(s)</TD>

<TD>Chapter 4</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glTexCoord*()</TD>

<TD>set texture coordinates</TD>

<TD>Chapter 9</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glEdgeFlag*()</TD>

<TD>control drawing of edges</TD>

<TD>Chapter 2</TD>
</TR>

<TR ALIGN=LEFT VALIGN=TOP>
<TD>glMaterial*()</TD>

<TD>set material properties</TD>

<TD>Chapter 6</TD>
</TR>
</TABLE>

<BR>&nbsp;
<P>No other OpenGL commands are valid between a <B>glBegin()</B> and <B>glEnd()</B>
pair, and making any other OpenGL call generates an error. Note, however,
that only OpenGL commands are restricted; you can certainly include other
programming-language constructs. For example, the following code draws
an outlined circle:
<PRE>#define PI 3.1415926535897;&nbsp;
GLint circle_points = 100;&nbsp;
glBegin(GL_LINE_LOOP);&nbsp;
for (i = 0; i &lt; circle_points; i++) {&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp; angle = 2*PI*i/circle_points;&nbsp;
&nbsp;&nbsp; glVertex2f(cos(angle), sin(angle));&nbsp;
}&nbsp;
glEnd();</PRE>
This example isn't the most efficient way to draw a circle, especially
if you intend to do it repeatedly. The graphics commands used are typically
very fast, but this code calculates an angle and calls the <B>sin()</B>
and <B>cos()</B> routines for each vertex; in addition, there's the loop
overhead. If you need to draw lots of circles, calculate the coordinates
of the vertices once and save them in an array, create a display list (see
Chapter
4 ,) or use a GLU routine (see Appendix C .)
<P>Unless they are being compiled into a display list, all <B>glVertex*()</B>
commands should appear between some <B>glBegin()</B> and <B>glEnd()</B>
combination. (If they appear elsewhere, they don't accomplish anything.)
If they appear in a display list, they are executed only if they appear
between a <B>glBegin()</B> and a <B>glEnd()</B>.
<P>Although many commands are allowed between <B>glBegin()</B> and <B>glEnd()</B>,
vertices are generated only when a <B>glVertex*()</B> command is issued.
At the moment <B>glVertex*()</B> is called, OpenGL assigns the resulting
vertex the current color, texture coordinates, normal vector information,
and so on. To see this, look at the following code sequence. The first
point is drawn in red, and the second and third ones in blue, despite the
extra color commands:
<PRE>glBegin(GL_POINTS);&nbsp;
&nbsp;&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; /* green */&nbsp;
&nbsp;&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; /* red */&nbsp;
&nbsp;&nbsp; glVertex(...);&nbsp;
&nbsp;&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; /* yellow */&nbsp;

⌨️ 快捷键说明

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