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?
unsigned char *pixelBuffer=param->device->pixelBuffer;
cout << "pixelBuffer is unexpectedly NULL."<<endl;
// this does not print so the pixelBuffer must not be NULL.
// GL_FRONT and GL_BACK both behave the same.
// copy all pixel values from OpenGL colour buffer.
for (int y=0;y<h;y++)
for (int x=0;x<w;x++)
// this will show a colour pattern so
// all is working except always getting an
// encodedColour of gray from the line before it.
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?
0 · ·