Hi. I have a question about glReadPixels()…
I have a huge window with an image that takes up a relatively small amount of area. I need to get color information about the image after I draw it. The image is read in from a file.
Instead of reading information from the whole screen, I’m trying to read only information from the rectangle containing the image. So if the rectagle was, say, in the lower left corner of the screen and the corner of the image was at, say, (a,b), I might say something like
glReadPixels(a,b,image.length, image.height,…etc,…, anArray);
Where on the screen the image will appear is variable though, so I can’t just hardcode in where to read from.
I am not sure about a few things.
Will the glReadPixels() command stick the information in the upperleft corner of anArray (it’s a 2-D array)?
It seems like the glReadPixels() is not putting the correct information in anArray.
anArray is of a pre-defined size, so not all of anArray should contain information. But I cant seem to find where the glReadPixels() is putting the information in anArray.
I believe one solution would be to create a 2-D dynamic array, so that the array would be the correct size. But I’m not sure how to create a 2-d dynamic array.
Can someone help me out with this? Any suggestions would be greatly appreciated.
It will store it in the array, and will assume your array has a width, height, pixel size and variable type you specify.
You array may be “2d” but in memory it’s
just a long string of bytes.
If you posted some code I might be able to help more.
A few things to add. If anArray is a defined as a double pointer (eg BYTE **anArray), it won’t work. If it really is a 2D array, you have to put the height first in the array declaration (eg BYTE anArray[HEIGHT][WIDTH * bytesPerPixel]) in order to read directly into it from glReadPixels
Secondly, if you’re reading 24 bit information, I believe each line is rounded up to be an even multiple of 4 bytes long (at least in Windows). This would change the above array width to be (WIDTH * 3 + 3) / 4 * 4, in order to round up to the nearest 4 bytes.
Finally, if anArray represents an area that is a different width than the area you are reading, you can’t do it this way. You have to read the data out into another buffer and then copy it in one line at a time. If it is simply a different height, you need to make sure to give roundUp(width * bytesPerPixel) * y bytes of offset, where y is the y location you want the data to end up in anArray, and the roundUp(int x) function does return (x + 3) / 4 * 4.
Also, don’t forget that the data you get back has the bottom line first, which is upside-down in comparison to what most people expect.