📄 chapter08.html
字号:
}
void myinit(void)
{
glShadeModel (GL_FLAT);
makeRasterFont();
}
void printString(char *s)
{
glPushAttrib (GL_LIST_BIT);
glListBase(fontOffset);
glCallLists(strlen(s), GL_UNSIGNED_BYTE, (GLubyte *) s);
glPopAttrib ();
}
void display(void)
{
GLfloat white[3] = { 1.0, 1.0, 1.0 };
int i, j;
char teststring[33];
glClear(GL_COLOR_BUFFER_BIT);
glColor3fv(white);
for (i = 32; i < 127; i += 32) {
glRasterPos2i(20, 200 - 18*i/32);
for (j = 0; j < 32; j++)
teststring[j] = (char) (i+j);
teststring[32] = 0;
printString(teststring);
}
glRasterPos2i(20, 100);
printString("The quick brown fox jumps");
glRasterPos2i(20, 82);
printString("over a lazy dog.");
glFlush ();
}
void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0.0, w, 0.0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, 500, 500);
auxInitWindow (argv[0]);
myinit();
auxReshapeFunc (myReshape);
auxMainLoop(display);
}</PRE>
<HR>
<H2>
Images</H2>
An image is similar to a bitmap, but instead of containing only a single
bit for each pixel in a rectangular region of the screen, an image can
contain much more information. For example, an image can contain the complete
(R, G, B, A) quadruple stored at each pixel. Images can come from several
sources, such as:
<UL>A photograph that's digitized with a scanner
<BR>
<P>An image that was first generated on the screen by a graphics program
using the graphics hardware and then read back, pixel by pixel
<BR>
<P>A software program that generated the image in memory pixel by pixel</UL>
The images you normally think of as pictures come from the color buffers.
However, you can read or write rectangular regions of pixel data from or
to the depth buffer or the stencil buffer. See Chapter 10 for an explanation
of these other buffers.
<P>In addition to simply being displayed on the screen, images can be used
for texture maps, in which case they're essentially pasted onto polygons
that are rendered on the screen in the normal way. See Chapter 9 for more
information about this technique.
<H3>
Reading, Writing, and Copying Pixel Data</H3>
OpenGL provides three basic commands that manipulate image data:
<UL><B>glReadPixels()</B> - Reads a rectangular array of pixels from the
framebuffer and stores the data in processor memory.
<BR>
<P><B>glDrawPixels()</B> - Writes a rectangular array of pixels into the
framebuffer from data kept in processor memory.
<BR>
<P><B>glCopyPixels()</B> - Copies a rectangular array of pixels from one
part of the framebuffer to another. This command behaves something like
a call to <B>glReadPixels()</B> followed by a call to <B>glDrawPixels()</B>,
but the data is never written into processor memory.</UL>
The basic ideas behind these commands are simple, but complexity arises
because there are many kinds of framebuffer data, many ways to store pixel
information in computer memory, and various data conversions that can be
performed during the reading, writing, and copying operations. All these
possibilities translate to many different modes of operation. If all your
program does is copy images on the screen, or read them into memory temporarily
so that they can be copied out later, you can ignore most of these modes.
However, if you want your program to modify the data while it's in memory
- for example, if you have an image stored in one format but the window
requires a different format - or if you want to save image data to a file
for future restoration in another session or on another kind of machine
with significantly different graphical capabilities, you have to understand
the various modes.
<P>The rest of this section describes the basic commands in detail. "Storing,
Transforming, and Mapping Pixels" discusses the details of pixel-storage
modes, pixel-transfer operations, and pixel-mapping operations. void <B>glReadPixels</B>(GLint
<B>x</B>, GLint <B>y</B>, GLsizei <B>width</B>, GLsizei <B>height</B>,
GLenum <B>format</B>, GLenum <B>type</B>, GLvoid *<B>pixels</B>);
<P>Reads pixel data from the framebuffer rectangle whose lower left corner
is at (<B>x, y</B>) and whose dimensions are <B>width</B> and <B>height</B>,
and stores it in the array pointed to by <B>pixels</B>. <B>format</B> indicates
the kind of pixel data elements that are read (an index value or an R,
G, B, or A component value, as listed in Table 8-1 ), and <B>type</B> indicates
the data type of each element (see Table 8-2 ).
<TABLE BORDER CELLPADDING=10 >
<CAPTION ALIGN=TOP><B>Table 8-1 : </B>Pixel Formats for Use with glReadPixels()
or glDrawPixels()</CAPTION>
<TR ALIGN=LEFT VALIGN=TOP>
<TH>Name</TH>
<TH>Kind of Pixel Data </TH>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_COLOR_INDEX</TD>
<TD>A single color index </TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_RGB</TD>
<TD>A red color component, followed by a green color component, followed
by a blue color component</TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_RGBA</TD>
<TD>A red color component, followed by a green color component, followed
by a blue color component, followed by an alpha color component</TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_RED</TD>
<TD>A single red color component</TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_GREEN</TD>
<TD>A single green color component </TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_BLUE</TD>
<TD>A single blue color component</TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_ALPHA</TD>
<TD>A single alpha color component </TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_LUMINANCE</TD>
<TD>A single luminance component </TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_LUMINANCE_ALPHA </TD>
<TD>A luminance component followed by an alpha color component </TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_STENCIL_INDEX </TD>
<TD>A single stencil index </TD>
</TR>
<TR ALIGN=LEFT VALIGN=TOP>
<TD>GL_DEPTH_COMPONENT </TD>
<TD>A single depth component </TD>
</TR>
</TABLE>
<P>void <B>glDrawPixels</B>(GLsizei <B>width</B>, GLsizei <B>height</B>,
GLenum <B>format</B>, GLenum <B>type</B>, const GLvoid *<B>pixels</B>);
<P>Draws a rectangle of pixel data with dimensions <B>width</B> and <B>height</B>.
The pixel rectangle is drawn with its lower left corner at the current
raster position. The <B>format</B> and <B>type</B> parameters have the
same meaning as with <B>glReadPixels()</B>. The array pointed to by <B>pixels</B>
contains the pixel data to be drawn. If the current raster position is
invalid, nothing is drawn, and it remains invalid.
<P>Remember that, depending on the format, anywhere from one to four elements
are read or written. For example, if the format is GL_RGBA, and you're
reading into 32-bit integers (that is, if <B>type</B> = GL_INT), then every
pixel read requires 16 bytes of storage (4 components
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -