Multiple OpenGL Windows in one application


I would like to know how it would be possible to have multiple OpenGL windows in a single application. From what I can see at this moment is that the open GL commands do not take a specific handle so there does not seem to be a way to distinguish between instances within a single application.



Do you mean multiple windows as in one window for each monitor or single monitor having multiple windows?

Every OpenGL API call operates on the current OpenGL context. Thus, the current OpenGL context is basically a global value (technically a thread-local value, since different threads can have different contexts be current) which all OpenGL calls use. The context contains all of the state for an instance of OpenGL. Contexts are usually created to be associated with a window, which is what their default framebuffer represents.

So if you want to render to two windows, you generally create two separate OpenGL contexts (possibly sharing objects between them), then you switch which context is current to decide which to render to.

However, the function to make a context current is not itself part of OpenGL; it’s part of the interface between OpenGL and the windowing system. Most such interfaces (wglMakeCurrent and glXMakeCurrent at least) take an object that represents a window (on Win32, this is a window’s device context; on X, it’s a Display object).

If that window has been properly set up to use OpenGL (typically by pixel format), then you can use a single context to render to both windows. You simply make the one context current with one window, render to that window, then make it current with the other window, then render to it.

I mean a single main window with two child windows contained inside of the main parent window which has their only OpenGL context. I don’t really want to isolate this into independent threads at this point in time.

I suggest to checkout glViewPort function. You only create one OpenGL context, create virtual windows which all have position, width and height as well as their elements to draw with in that window and just do X amount of draw calls by just switching the glViewPort values in between the draw calls. If you allow overlapping between the windows then try to put them in an array sorted “farthest” to “nearest” and start the drawing from the farthest.

EDIT: If you wanted to create totally separate windows (window in this case is the same as you would have a web browser open) which each are in fullscreen or in windowed mode then you have to create multiple contexts with shared context from the first context since each .