PDA

View Full Version : RGBA values



sumit30
10-23-2001, 06:06 AM
Hi,
I am really a beginner
how do I get the RGBA values for each pixel, after my scene is rendered, and I use glreadpixels?
some code snippets would be useful

GameMaker
10-23-2001, 04:11 PM
Here is some code that reads a rectangle from the screen and puts it into a bitmap. When you read from the screen you really only get RGB values back (I think) because the A (alpha) value is only used for blending when drawing it.


/*
* 'ReadDIBitmap()' - Read the current OpenGL viewport into a
* 24-bit RGB bitmap.
*
* Returns the bitmap pixels if successful and NULL otherwise.
*/

void *
ReadDIBitmap(BITMAPINFO **info) /* O - Bitmap information */
{
long i, j, /* Looping var */
bitsize, /* Total size of bitmap */
width; /* Aligned width of a scanline */
GLint viewport[4]; /* Current viewport */
void *bits; /* RGB bits */
GLubyte *rgb, /* RGB looping var */
temp; /* Temporary var for swapping */

/*
* Grab the current viewport...
*/

glGetIntegerv(GL_VIEWPORT, viewport);

/*
* Allocate memory for the header and bitmap...
*/

if ((*info = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER))) == NULL)
{
/*
* Couldn't allocate memory for bitmap info - return NULL...
*/

return (NULL);
};

width = viewport[2] * 3; /* Real width of scanline */
width = (width + 3) & ~3; /* Aligned to 4 bytes */
bitsize = width * viewport[3]; /* Size of bitmap, aligned */

if ((bits = calloc(bitsize, 1)) == NULL)
{
/*
* Couldn't allocate memory for bitmap pixels - return NULL...
*/

free(*info);
return (NULL);
};

/*
* Read pixels from the framebuffer...
*/

glFinish(); /* Finish all OpenGL commands */
glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
glPixelStorei(GL_PACK_SKIP_ROWS, 0);
glPixelStorei(GL_PACK_SKIP_PIXELS, 0);

glReadPixels(0, 0, viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE,
bits);

/*
* Swap red and blue for the bitmap...
*/

for (i = 0; i < viewport[3]; i ++)
for (j = 0, rgb = ((GLubyte *)bits) + i * width;
j < viewport[2];
j ++, rgb += 3)
{
temp = rgb[0];
rgb[0] = rgb[2];
rgb[2] = temp;
};

/*
* Finally, initialize the bitmap header information...
*/

(*info)->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
(*info)->bmiHeader.biWidth = viewport[2];
(*info)->bmiHeader.biHeight = viewport[3];
(*info)->bmiHeader.biPlanes = 1;
(*info)->bmiHeader.biBitCount = 24;
(*info)->bmiHeader.biCompression = BI_RGB;
(*info)->bmiHeader.biSizeImage = bitsize;
(*info)->bmiHeader.biXPelsPerMeter = 2952; /* 75 DPI */
(*info)->bmiHeader.biYPelsPerMeter = 2952; /* 75 DPI */
(*info)->bmiHeader.biClrUsed = 0;
(*info)->bmiHeader.biClrImportant = 0;

return (bits);
}