官方文档:https://docs.gl/es2/glReadPixels

从帧缓冲区中读取一个像素块。
read a block of pixels from the frame buffer.

一、C函数

  1. void glReadPixels(GLint x, // 第一个像素的窗口坐标(像素矩形左下角)
  2. GLint y,
  3. GLsizei width, // 像素矩形宽高
  4. GLsizei height,
  5. GLenum format, // 像素矩形数据格式
  6. GLenum type, // 像素矩形数据类型
  7. GLvoid *data); // 像素矩形指针

glReadPixels returns pixel data from the frame buffer, starting with the pixel whose lower left corner is at location (x, y), into client memory starting at location data. The GL_PACK_ALIGNMENT parameter, set with the glPixelStorei command, affects the processing of the pixel data before it is placed into client memory.
glReadPixels从帧缓冲区返回像素数据,从左下角位于(x,y)的像素开始,从位置data开始返回客户端内存。使用glPixelStorei命令设置的GL_PACK_ALIGNMENT参数会影响像素数据在放入客户端内存之前的处理。

glReadPixels returns values from each pixel with lower left corner at (x+i,y+j)x+iy+j for 0<=iglReadPixels返回每个像素的值,左下角为x + i y + j,0 <= i <width,0 <= j <height。 该像素被称为第j行中的第i个像素。 像素按行顺序从最低行返回到最高行,每行从左到右排列。

RGBA color components are read from the color buffer. Each color component is converted to floating point such that zero intensity maps to 0.0 and full intensity maps to 1.0.
从颜色缓冲区读取RGBA颜色分量。 每个颜色分量都转换为浮点。

Unneeded data is then discarded. For example, GL_ALPHA discards the red, green, and blue components, while GL_RGB discards only the alpha component. The final values are clamped to the range [0,1].
丢弃不需要的数据。例如GL_ALPHA丢弃red,green和blue分量,而GL_RGB仅丢弃alpha分量。 最终值被限制在[0, 1]的范围内。

Finally, the components are converted to the proper format, as specified by type. When type is GL_UNSIGNED_BYTE, each component is multiplied by 28−1. When type is GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4, or GL_UNSIGNED_SHORT_5_5_5_1, each component is multiplied by 2N−1, where NN is the number of bits in the bitfield.
最后,组件将转换为由type指定合适的格式。当类型为GL_UNSIGNED_BYTE时,每个组件乘以28-1。 当类型为GL_UNSIGNED_SHORT_5_6_5GL_UNSIGNED_SHORT_4_4_4_4GL_UNSIGNED_SHORT_5_5_5_1时,每个分量乘以2N-1,其中N是位域中的位数。

gIReadPixels从图形硬件中复制数据,通常通过总线传输到系统内存。在这种情况下, 应用程序将被阻塞,直到内存传输完成。此外,如果我们指定一个与图形硬件的本地排列不同的像素布局,那么在数据进行重定格式时将产生额外的性能开销。

参数x、y

Specify the window coordinates of the first pixel that is read from the frame 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.

参数format

Specifies the format of the pixel data. The following symbolic values are accepted:

  • GL_ALPHA
  • GL_RGB
  • GL_RGBA

    参数type

    Specifies the data type of the pixel data. Must be one of

  • GL_UNSIGNED_BYTE

  • GL_UNSIGNED_SHORT_5_6_5
  • GL_UNSIGNED_SHORT_4_4_4_4
  • GL_UNSIGNED_SHORT_5_5_5_1

    参数data

    Returns the pixel data.

    三、注意

    If the currently bound framebuffer is not the default framebuffer object, color components are read from the color image attached to the GL_COLOR_ATTACHMENT0 attachment point.
    如果当前绑定的帧缓冲区不是默认的帧缓冲区对象,则从附加到GL_COLOR_ATTACHMENT0附着点的彩色图像中读取颜色分量。

Only two format/type parameter pairs are accepted. GL_RGBA/GL_UNSIGNED_BYTE is always accepted, and the other acceptable pair can be discovered by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.
只有两个format/type参数对是可接受的。GL_RGBA / GL_UNSIGNED_BYTE是始终都可以接受的,另外的就需要查询了:通过查询GL_IMPLEMENTATION_COLOR_READ_FORMATGL_IMPLEMENTATION_COLOR_READ_TYPE来发现其他可接受的对。

Values for pixels that lie outside the window connected to the current GL context are undefined.
位于连接到当前GL上下文的窗口之外的像素值是未定义的。

If an error is generated, no change is made to the contents of data.
如果发生错误,则不会更改data内容。

四、错误

GL_INVALID_ENUM

generated if format or type is not an accepted value.

GL_INVALID_VALUE

generated if either width or height is negative.

GL_INVALID_OPERATION

generated if type is GL_UNSIGNED_SHORT_5_6_5 and format is not GL_RGB.

generated if type is GL_UNSIGNED_SHORT_4_4_4_4 or GL_UNSIGNED_SHORT_5_5_5_1 and format is not GL_RGBA.

generated if format and type are neither GL_RGBA and GL_UNSIGNED_BYTE, respectively, nor the format/type pair returned by querying GL_IMPLEMENTATION_COLOR_READ_FORMAT and GL_IMPLEMENTATION_COLOR_READ_TYPE.
如果formattype分别既不是GL_RGBA又不是GL_UNSIGNED_BYTE,也不是通过查询GL_IMPLEMENTATION_COLOR_READ_FORMATGL_IMPLEMENTATION_COLOR_READ_TYPE返回的格式/类型对。

GL_INVALID_FRAMEBUFFER_OPERATION

generated if the currently bound framebuffer is not framebuffer complete (i.e. the return value from glCheckFramebufferStatus is not GL_FRAMEBUFFER_COMPLETE).
如果当前绑定的帧缓冲区不是帧缓冲区完成状态(即glCheckFramebufferStatus的返回值不是GL_FRAMEBUFFER_COMPLETE)。