OpenGL glReadPixels錯誤

最近在使用glReadPixels時得到的圖像總是黑色的,

unsigned char *pixels = (unsigned char *) malloc (_width * _height * 3);

glReadPixels(0, 0, _width, _height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

非常不解,查了很多資料,最後在官網上看到 使用glReadPixels時,傳入的format和type是有要求的 ,於是乎改成下面的代碼:

unsigned char *pixels = (unsigned char *) malloc (_width * _height * 4);

glReadPixels(0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

果然就行了

另外,注意glReadPixels讀出來的像素值是上下鏡像翻轉過的,所以要得到正確的圖像,需要對數據進行一次垂直鏡像翻轉


format和type的規則參見官方文檔,http://www.khronos.org/opengles/sdk/1.1/docs/man/

附上官網API詳解:

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 type is GL_UNSIGNED_SHORT_5_6_5 and format is not GL_RGB.

GL_INVALID_OPERATION is 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.

GL_INVALID_OPERATION is 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_OES and GL_IMPLEMENTATION_COLOR_READ_TYPE_OES.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章