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

📄 chapter01.html

📁 OpenGl红宝书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
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.
<TABLE BORDER CELLPADDING=10 >
<CAPTION ALIGN=TOP><B>Table 1-1 : </B>Command Suffixes and Argument Data
Types</CAPTION>

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

<TH>Data Type</TH>

<TH>Typical Corresponding C-Language Type</TH>

<TH>OpenGL Type Definition</TH>
</TR>

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

<TD>8-bit integer</TD>

<TD>signed char</TD>

<TD>GLbyte</TD>
</TR>

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

<TD>16-bit integer</TD>

<TD>short</TD>

<TD>GLshort</TD>
</TR>

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

<TD>32-bit integer</TD>

<TD>long</TD>

<TD>GLint, GLsizei</TD>
</TR>

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

<TD>32-bit floating-point</TD>

<TD>float</TD>

<TD>GLfloat, GLclampf</TD>
</TR>

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

<TD>64-bit floating-point</TD>

<TD>double</TD>

<TD>GLdouble, GLclampd</TD>
</TR>

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

<TD>8-bit unsigned integer</TD>

<TD>unsigned char</TD>

<TD>GLubyte, GLboolean</TD>
</TR>

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

<TD>16-bit unsigned integer</TD>

<TD>unsigned short</TD>

<TD>GLushort</TD>
</TR>

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

<TD>32-bit unsigned integer</TD>

<TD>unsigned long</TD>

<TD>GLuint, GLenum, GLbitfield</TD>
</TR>
</TABLE>

<BR>&nbsp;
<P>Thus, the two commands
<PRE>glVertex2i(1, 3);
glVertex2f(1.0, 3.0);</PRE>
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.
<P>Some OpenGL commands can take a final letter <B>v</B>, 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 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:
<PRE>glColor3f(1.0, 0.0, 0.0);

float color_array[] = {1.0, 0.0, 0.0};
glColor3fv(color_array);</PRE>
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, <B>glColor*()</B>
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, <B>glVertex*v()</B>
refers to all the vector versions of the command you use to specify vertices.
<P>Finally, OpenGL defines the constant GLvoid; if you're programming in
C, you can use this instead of void.
<P>
<HR>
<H2>
OpenGL as a State Machine</H2>
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 <B>glEnable()</B> or <B>glDisable()</B>.
<P>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: <B>glGetBooleanv(),
glGetDoublev()</B>, <B>glGetFloatv()</B>, or <B>glGetIntegerv()</B>. 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 <B>glGetLight*()</B>, <B>glGetError()</B>, or <B>glGetPolygonStipple()</B>).
In addition, you can save and later restore the values of a collection
of state variables on an attribute stack with the <B>glPushAttrib()</B>
and <B>glPopAttrib()</B> commands. Whenever possible, you should use these
commands rather than any of the query commands, since they're likely to
be more efficient.
<P>The complete list of state variables you can query is found in Appendix
B . For each variable, the appendix also lists the <B>glGet*()</B> command
that returns the variable's value, the attribute class to which it belongs,
and the variable's default value.
<P>
<HR>
<H2>
OpenGL-related Libraries</H2>
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.
<UL>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 part of your OpenGL
implementation. It's described in more detail in Appendix C and in the
<I>OpenGL
Reference Manual</I>. The more useful GLU routines are described in the
chapters in this guide, where they're relevant to the topic being discussed.
GLU routines use the prefix <B>glu</B>.
<BR>&nbsp;
<P>The OpenGL Extension to the X Window System (GLX) provides a means of
creating an OpenGL context and associating it with a drawable window on
a machine that uses the X Window System. GLX is provided as an adjunct
to OpenGL. It's described in more detail in both Appendix D and the <I>OpenGL
Reference Manual</I>. One of the GLX routines (for swapping framebuffers)
is described in "Animation." GLX routines use the prefix <B>glX</B>.
<BR>&nbsp;
<P>The <I>OpenGL Programming Guide</I> Auxiliary Library was written specifically
for this book to make programming examples simpler and yet more complete.
It's the subject of the next section, and it's described in more detail
in Appendix E . Auxiliary library routines use the prefix <B>aux</B>. "How
to Obtain the Sample Code" describes how to obtain the source code for
the auxiliary library.
<BR>&nbsp;
<P>Open Inventor is an object-oriented toolkit based on OpenGL that provides
objects and methods for creating interactive three-dimensional graphics
applications. Available from Silicon Graphics and written in C++, Open
Inventor provides pre-built objects and a built-in event model for user
interaction, high-level application components for creating and editing
three-dimensional scenes, and the ability to print objects and exchange
data in other graphics formats.</UL>

<H3>
The OpenGL Programming Guide Auxiliary Library</H3>
As you know, OpenGL contains rendering commands but is designed to be independent
of any window system or operating system. Consequently, it contains no
commands for opening windows or reading events from the keyboard or mouse.
Unfortunately, it's impossible to write a complete graphics program without
at least opening a window, and most interesting programs require a bit
of user input or other services from the operating system or window system.
In many cases, complete programs make the most interesting examples, so
this book uses a small auxiliary library to simplify opening windows, detecting
input, and so on.
<P>In addition, since OpenGL's drawing commands are limited to those that
generate simple geometric primitives (points, lines, and polygons), the
auxiliary library includes several routines that create more complicated
three-dimensional objects such as a sphere, a torus, and a teapot. This
way, snapshots of program output can be interesting to look at. If you
have an implementation of OpenGL and this auxiliary library on your system,
the examples in this book should run without change when linked with them.
<P>The auxiliary library is intentionally simple, and it would be difficult
to build a large application on top of it. It's intended solely to support
the examples in this book, but you may find it a useful starting point
to begin building real applications. The rest of this section briefly describes
the auxiliary library routines so that you can follow the programming examples
in the rest of this book. Turn to Appendix E for more details about these
routines.
<H4>
Window Management</H4>
Three routines perform tasks necessary to initialize and open a window:
<UL><B>auxInitWindow()</B> opens a window on the screen. It enables the
Escape key to be used to exit the program, and it sets the background color
for the window to black.
<BR>&nbsp;
<P><B>auxInitPosition()</B> tells <B>auxInitWindow()</B> where to position
a window on the screen.
<BR>&nbsp;
<P><B>auxInitDisplayMode()</B> tells <B>auxInitWindow()</B> whether to
create an RGBA or color-index window. You can also specify a single- or
double-buffered window. (If you're working in color-index mode, you'll
want to load certain colors into the color map; use <B>auxSetOneColor()</B>
to do this.) Finally, you can use this routine to indicate that you want
the window to have an associated depth, stencil, and/or accumulation buffer.</UL>

<H4>
Handling Input Events</H4>
You can use these routines to register callback commands that are invoked
when specified events occur.
<UL><B>auxReshapeFunc()</B> indicates what action should be taken when
the window is resized, moved, or exposed.
<BR>&nbsp;
<P><B>auxKeyFunc()</B> and <B>auxMouseFunc()</B> allow you to link a keyboard
key or a mouse button with a routine that's invoked when the key or mouse
button is pressed or released.</UL>

<H4>
Drawing 3-D Objects</H4>
The auxiliary library includes several routines for drawing these three-dimensional
objects:
<P>sphere octahedron

⌨️ 快捷键说明

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