problem copying display buffer to bitmap

I want to copy/read from OpenGL's rendered buffer/display into another format. I know how to make a bitmap so that's not an issue. The problem is that I can't find a way that works at reading the data from the colour buffer of the display.

glReadPixels seems to be good for the job according to online documentation but I'm having trouble with it. Every byte in the pixel buffer becomes 0xCD in hex. The OpenGL is rendering to a window and I don't see even one gray pixel in it. It is a 3D environment with textured green and blue polygons.

Below is a code snippet that I'd like to explain.

The project code is quite large and complicated but the important parts related to the problem are given in the following code snippet.

BitmapImage is a class of my own which works very well and is thoroughly tested.

pixelBuffer is definitely not NULL so it must have the required memory available.

The glReadBuffer function was tested with GL_FRONT and GL_BACK. In both cases, the same result happens; a solid gray background created by all r,g,b values being 0xCD.

I assume the glReadBuffer should be using GL_FRONT but not completely sure. I know that SwapBuffers(hDC) is used in my Visual C++ code to make the buffer show in a window. Is GL_FRONT the right choice?

[code]
int numBytes=4;
int w=100,h=100;
BitmapImage img(w,h);
unsigned char *pixelBuffer=param->device->pixelBuffer;

if (pixelBuffer==NULL)
{
cout << "pixelBuffer is unexpectedly NULL."<<endl;
// this does not print so the pixelBuffer must not be NULL.
}
glReadBuffer(GL_FRONT);
// GL_FRONT and GL_BACK both behave the same.

glReadPixels(100,400,w,h,GL_RGBA,GL_UNSIGNED_BYTE,pixelBuffer);

// BitBlt(hDC,0,0,width,height,_hDesktopDC,x,y,SRCCOPY);

// copy all pixel values from OpenGL colour buffer.
int widthStep=w*numBytes;

for (int y=0;y<h;y++)
{
for (int x=0;x<w;x++)
{
int index=y*widthStep+x*numBytes;
int encodedColour=((pixelBuffer[index]&0xff)<<16)|((pixelBuffer[index+1]&0xff)<<8)|(pixelBuffer[index+2]&0xff);
img.setPixel(x,y,encodedColour);
// img.setPixel(x,y,x+y);
// this will show a colour pattern so
// all is working except always getting an
// encodedColour of gray from the line before it.
}
}
[/code]

Is there a way to see if OpenGL detected a problem while executing glReadPixels? It doesn't seem to work with all the gray but I wonder if it detected a specific problem that I'm not seeing.

Am I sending a bad parameter to the glReadPixels function?

Should I be using a different function?

Do you have any ideas of other things to try?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories