PDA

View Full Version : About glReadPixels



Rong Yao
07-02-2007, 08:10 AM
I tried to use glReadPixels to read the color(data) information of a rectangle area into a data buffer and in order to create a bitmap file. However, the program crashes. Any one can help me solve the problem? Thank you very much. Here is the code:
gl.m_pBitmapData = new UINT[nWidth*nHeight];
glReadPixels(StartX, StartY, nWidth, nHeight, GL_RGBA, GL_UNSIGNED_INT, gl.m_pBitmapData);

mfort
07-02-2007, 08:31 AM
Multiply by 4!

new UINT[nWidth*nHeight * 4 ];

mfort
07-02-2007, 08:33 AM
or if one byte per channel is sufficient to you, then read unsigned bytes.
glReadPixels(StartX, StartY, nWidth, nHeight, GL_RGBA, GL_UNSIGNED_BYTE, gl.m_pBitmapData);

The type that goes to glReadPixels is for one channel. You have 4 channels for RGBA.

Rong Yao
07-02-2007, 11:42 AM
Thanks. For each pixel, the data is in 4 BYTEs when I use GL_RGBA, right? But my buffer is in UINT, it is already 4 BYTEs. So it should be OK, right?
Buffer size is: nWidth*nHeight(UINTs) = nWidth* nHeight*4(BYTEs)
Data size is: nWidth* nHeight*4(BYTEs)

Rong Yao
07-02-2007, 12:58 PM
The type that goes to glReadPixels is for one channel. You have 4 channels for RGBA. Oh, I see, Thank you!

Joseph Steel
07-03-2007, 01:04 AM
You might need to have to specify the packing parameters:
glPixelStorei(GL_PACK_ROW_LENGTH, nWidth);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
prior to the call to glReadPixels().

Maybe the pack alignment can be 4?

Rong Yao
07-03-2007, 07:55 AM
Originally posted by Joseph Steel:
You might need to have to specify the packing parameters:
glPixelStorei(GL_PACK_ROW_LENGTH, nWidth);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
prior to the call to glReadPixels().

Maybe the pack alignment can be 4? Thank you for your reminding.