简体   繁体   中英

Interpreting glReadPixels()

After more than an hour of looking for an answer (trying stuff in Munshi's "OpenGL ES 2.0 Programming Guide", searching Apple's documentation, searching StackOverflow), I'm still at a loss for getting glReadPixels to work. I've tried so many different ways, and the best I've got is fluctuating (and therefore wrong) results.

I've set up the simple case of a quad being rendered with shaders to the screen, and I've manually assigned gl_FragColor to pure red, so there should be absolutely no fluctuation on the screen. Then I try something like the following code before presentRenderbuffer:

GLubyte *pixels = (GLubyte *)malloc(3);

glReadPixels(100, 100, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixels);

NSLog(@"%d", (int)pixels[0]);

free(pixels);

Basically, trying to read a single pixel at (100, 100) and read the red value of it, which I expect to be either 1 or 255. Instead I get values like 1522775, 3587, and 65536, though the image on the screen never changes. I did something like this on Mac and it worked fine, but for some reason I can't get this to work on iOS. I have the above statement (and have tried a number of variations I've come across on the internet) after the call to glDrawArrays() and before the presentBuffer: call. I've even tried the method from "OpenGL ES 2.0 Programming Guide" that handles all cases of read-types and read-formats by querying glGetIntegerv() for the framebuffer's information.

Any ideas? I'm sure someone will say, "use the search feature," but I've seriously come up dry on it and can't get any further. Thanks for your help!

From the Opengl ES Manual:

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.

So, you better use GL_RGBA instead of GL_RGB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM