Tuesday, May 31, 2011

glReadPixels example c c++ objc java

Name

glReadPixels - read a block of pixels from the color buffer

C Specification

void glReadPixels(GLint y,
    GLint y,
    GLsizei width,
    GLsizei height,
    GLenum format,
    GLenum type,
    GLvoid * pixels)

Parameters

x, y
Specify the window coordinates of the first pixel that is read from the color buffer. This location is the lower left corner of a rectangular block of pixels.
width, height
Specify the dimensions of the pixel rectangle. width and height of one correspond to a single pixel. (glReadPixels)
format
Specifies the format of the pixel data. Must be either GL_RGBA or the value of GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES.
type
Specifies the data type of the pixel data. Must be either GL_UNSIGNED_BYTE or the value of GL_IMPLEMENTATION_COLOR_READ_TYPE_OES.
pixels
Returns the pixel data.

Description

glReadPixels returns pixel data from the color buffer, starting with the pixel whose lower left corner is at location (x, y), into client memory starting at location pixels. The processing of the pixel data before it is placed into client memory can be controlled with glPixelStore.
glReadPixels returns values from each pixel with lower left corner at (x + i, y + j) for 0 ≤ i < width and 0 ≤ j < height. This pixel is said to be the ith pixel in the jth row. Pixels are returned in row order from the lowest to the highest row, left to right in each row.
format specifies the format of the returned pixel values. GL_RGBA is always accepted, the value of GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES may allow another format:
GL_RGBA
Each color component is converted to floating point such that zero intensity maps to 0 and full intensity maps to 1.
GL_RGB
Each element is an RGB triple. The GL converts it to floating point and assembles it into an RGBA element by attaching 1 for alpha.
GL_LUMINANCE
Each element is a single luminance value. The GL converts it to floating point and assembles it into an RGBA element by replicating the luminance value three times for red, green and blue and attaching 1 for alpha.
GL_LUMINANCE_ALPHA
Each element is a luminance/alpha pair. The GL converts it to floating point and assembles it into an RGBA element by replicating the luminance value three times for red, green and blue.
GL_ALPHA
Each element is a single alpha component. The GL converts it to floating point and assembles it into an RGBA element by attaching 0 for red, green and blue.
Unneeded data is then discarded. For example, GL_ALPHA discards the red, green, and blue components, while GL_RGB discards only the alpha component. GL_LUMINANCE computes a single-component value as the sum of the red, green, and blue components, and GL_LUMINANCE_ALPHA does the same, while keeping alpha as a second value. The final values are clamped to the range [0, 1]. (glReadPixels)
Finally, the components are converted to the proper, as specified by type where each component is multiplied by 2n - 1, where n is the number of bits per component.
Return values are placed in memory as follows. If format is GL_ALPHA, or GL_LUMINANCE, a single value is returned and the data for the ith pixel in the jth row is placed in location j * width + i. GL_RGB returns three values, GL_RGBA returns four values, and GL_LUMINANCE_ALPHA returns two values for each pixel, with all values corresponding to a single pixel occupying contiguous space in pixels. Storage parameter GL_PACK_ALIGNMENT set by glPixelStore, affects the way that data is written into memory. See glPixelStore for a description. (glReadPixels)

Notes

Values for pixels that lie outside the window connected to the current GL context are undefined.
If an error is generated, no change is made to the contents of pixels.

Errors

GL_INVALID_ENUM is generated if format is not GL_RGBA or the value of GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES.
GL_INVALID_ENUM is generated if type is not GL_UNSIGNED_BYTE or the value of GL_IMPLEMENTATION_COLOR_READ_TYPE_OES.
GL_INVALID_VALUE is generated if either width or height is negative.
GL_INVALID_OPERATION is generated if format and type are neither (GL_RGBA, GL_UNSIGNED_BYTE) nor the values of (GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES, GL_IMPLEMENTATION_COLOR_READ_TYPE_OES).

Associated Gets

glGetInteger with argument GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES
glGetInteger with argument GL_IMPLEMENTATION_COLOR_READ_TYPE_OES

Copyright

Copyright © 2003 Silicon Graphics, Inc.
This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.

See Also

glGetInteger, glPixelStore

Example
-------------------------------------------------------------------------
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realY;
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
realY = viewport[3] - screenY;
GLfloat winZ;
glReadPixels(screenX, realY, 1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&winZ);
return gluUnProject((GLdouble)screenX,(GLdouble)realY, (GLdouble)winZ, mvmatrix,projmatrix,viewport, &wx,&wy,&wz);