I’m working with glReadPixels for some work, and it’s behaving strangely. In particular, I run the following test:
unsigned char pixels[ 512 * 512 * 4 ];
int mid = 0;
while (mid < 512) {
printf("Test %d : ", mid);
memset(pixels, 0, 512*512*4);
pixels[1] = 0x42;
glReadPixels( 0, 0, mid, 511, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
printf(" %x ", pixels[1]);
if (pixels[0] == 0) {
printf("0\n");
} else {
printf("1\n");
}
mid++;
}
With output:
Test 0 : 42 0
Test 1 : 67 1
Test 2 : 67 1
Test 3 : 67 1
Test 4 : 42 0
Test 5 : 67 1
Test 6 : 67 1
Test 7 : 67 1
Test 8 : 42 0
Test 9 : 67 1
Test 10 : 67 1
Test 11 : 67 1
I have already drawn data to the entire screen (the 0x67 is the correct byte for the color I’m drawing) so I’m not sure what’s going on here.
The main thing I actually want is to get a copy of every pixel currently on the screen inside of an array I can access not on the GPU. I’m not sure if this is the ‘correct’ way about getting it, but it’s the only way I know of. Is there a better way of doing this?
You could use this code to first create an empty texture (use it just once, before your update loop):
Then use the following to copy the current buffer to your texture (use this at the end of every update loop, just before you swap buffers):
And finally use this to retrieve an array with RGBA pixel data (right after the previous bit):
It all depends on what you want to do with this pixel data ofcourse. This may not help you at all, but I hope it will ;).