Can I obtain window display buffer data?

I am trying to use OpenGL to not only display 3D objects in a window on my computer but also, after displaying the image in a window, I would like to be able to create an image file of the contents of the window. I know how to create the desired display of my 3D objects in a window using OpenGL, and I know how to create bitmap or JPEG files from an image in memory (for non-OpenGL programs), but I do not know how to access the actual display bytes of an OpenGL window so that I can copy them into memory and write them out to an image file. Is this possible?

fortunately there’s an opengl function, so the solution is independent of the window system.

int w = 1024, h = 768;
char buf[3wh];

glReadPixels(0, 0, wh, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid)buf);

…and the above is an example that there’s no code so short that you can’t put an error into it. replace “w*h” with “w, h”.

glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)buf);

Thanks for your help. However, after I saw your responses posted, I tried using glReadPixels(), with appropriate input parameters, and could not get it to work. Specifically, it reads only ZEROs into the buffer, even though I can definitely see objects I created in the window. I tried zeroing my buffer before calling glReadPixels and the buffer is not changed. However, I also tried setting the entire buffer contents to 0xff before calling glReadPixels, and the entire buffer gets completely reset to zero by the call. I definitely allocated enough memory. I used glGetError() to see if I could identify the error, but it just returns zero. What else can I be doing wrong?

very strange. since you get only zeros, your clear color seems to be black. have you tried a different clear color and checked if the values change?

generally, glReadPixels should be called after glFlush or after SwapBuffers in a double-buffer configuration. with glReadBuffer you can specify which buffer shall be read. try

 glReadBuffer(GL_FRONT);

or

 glReadBuffer(GL_BACK);

Thanks again for your help. I have solved my problem. I used glReadBuffer() to select the front buffer or back buffer to copy from when using glReadBytes() and it still did not work properly. My code for copying the bytes was located in the portion of my code that is responsible for drawing the image. I moved this portion of code to a callback routine that I assigned using glutIdleFunc(), and I am now able to successfully copy the front and back buffers.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.