Section 5.2. Reading Pixels


5.2. Reading Pixels

Many applications read back pixel data to support printing images and saving images to a 2D image file. To obtain the contents of the framebuffer, use the glReadPixels() command.


void glReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
  GLenum format, GLenum type, GLvoid* data );


Reads the width x height pixel rectangle with the bottom-left corner at x and y in window coordinates from the framebuffer and stores it at the address specified by data. The format and type parameters are as for glDrawPixels().

If any part of the read region is not owned by OpenGL (for example, part of the region lies outside the window or is obscured by another window), the read result for that area is undefined.

By default, glReadPixels() reads from the back buffer of a double-buffered window. Applications can change this with the glReadBuffer() command; see "glReadBuffer" in OpenGL® Reference Manual for more information.

If the format parameter is GL_RGBA, and the framebuffer doesn't store alpha, OpenGL returns full intensity for the alpha value of each pixel.

OpenGL allows applications to set state values that control how it packs the pixel rectangle into client memory. If your application specifies GL_RGBA for format and GL_UNSIGNED_BYTE for type, the default settings will tightly pack the pixel rectangle into your client memory area. See Chapter 8, "Drawing Pixels, Bitmaps, Fonts, and Images," in OpenGL® Programming Guide for more information on controlling pixel read state.

The PixelRectangles example code available from this book's Web site uses glReadPixels() to support saving the rendered image to a TIFF image file.




OpenGL Distilled
OpenGL Distilled
ISBN: 0321336798
EAN: 2147483647
Year: 2007
Pages: 123
Authors: Paul Martz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net