A bit of trouble with offscreen rendering

I have a GUI program that uses OpenGL to draw objects. What I’m trying to do is to leverage those drawing routines in order to add the ability to output png images of those objects to another program.

This second program is very basic, and I’m trying to keep it that way. I know how to use OpenGL extensions to create an FBO and RenderBuffer to do offscreen rendering; but I don’t want to add a dependency on GLEW if I don’t have to. Not to mention that there are still a few graphics cards which don’t support FBOs.

So I’m trying to just create a window, keep it hidden, and do my rendering/readback entirely on the back buffer. Except there seem to be two problems with this:

  1. If I don’t show the window, it seems like nothing gets drawn. The glReadPixels() just grabs whatever was in memory there (usually mishmashes of previously displayed images). Why?

  2. In Windows (this needs to be cross-platform so I have two backends for window creation), it appears that the allocated size of the window (which I’m using for my glViewport and glReadPixels calls) is for the entire window, not just the useful area. Title bar and such too. Is there a way around this, preferably that lets me control the usable size directly?

Why not just use the main system window and render what ever you like to the back buffer (after setting view port and projection matrix). Then you can readback with glReadPixels?

In your main rendering loop, just draw to the backbuffer as described above, then clear the screen and render all the rest of your stuff.
This is far simpler than messing with hidden 2nd window and then having to share displaylists and textures across two rendering contexts.

Because OpenGL wasn’t meant to be used this way?
In any case, if you find a way to do this on one system, it will definitely behave differently on another setup.
Why are you using the back buffer, since the window is not displayed you could theoretically use any buffer.

What are you trying to achieve? Grabbing the window content of a OpenGL rendering?


The AdjustWindowRectEx function calculates the required size of the window rectangle, based on the desired size of the client rectangle. The window rectangle can then be passed to the CreateWindowEx function to create a window whose client area is the desired size.

Why not just use the main system window

I don’t have one. This is a console app.

Because OpenGL wasn’t meant to be used this way?

Heh, it wasn’t meant for GPGPU work either, yet I’ve done that…this should be simple by comparison.

What are you trying to achieve? Grabbing the window content of a OpenGL rendering?

Essentially, yeah.

I think it may be necessary to flash a window on screen for a moment…this could be annoying, but I don’t see a way around it that doesn’t require a GLEW or GLee dependency (or massively annoying manual extension handling).

Thanks for pointing me at that function, I’m sure that will be useful.

The contents of the backbuffer of an invisible window are undefined. You will have to create a FBO or pbuffer.

You don’t need to use GLEW if you don’t want to. Just load the necessary extensions manually.

Edit: or, if you don’t care about hardware acceleration, use Mesa3D which supports offscreen rendering without a window.

Or, depending on the console, it may be like the NVidia GL drivers on Linux, where you just use their includes/libs, and don’t even have to mess with that GetProcAddress stuff.