Recreate Window without destroying the Context

2019-04-14 03:34发布

问题:

This question is about a graphics application using OpenGL. (At the time I am using the framework GLFW but I consider changing it.)

My aim is to let the user (as much as possible) continuously switch between fullscreen mode and windowed mode. This procedure should take less than a second and can occur during runtime. For example see the game Minecraft where the user can toggle fullscreen with virtually no delay.

The window recreation process doesn't take so much time. But the problem is, that closing the window destroys my OpenGL context and I have to reload all my shaders and buffers.

How to recreate the window of a graphics application without destroying the OpenGL context? (I would like to use GLFW but I am open minded about alternative frameworks or a own implementation.)

回答1:

Technically the OpenGL context is not tied to any window. What's important is, that the visual format of the window matches that of the visual format the context has been created for. It is perfectly possible to use a single OpenGL context with multiple windows.

The problem you're facing is, that frameworks like GLFW, SDL or GLUT don't expose this kind of functionality. It's a feature that definitely should be added to them some time.

You can however do it, when you create and manage your windows and OpenGL context yourself. The principal process of what you intend is the following:

  1. select the visual format for the windows (PIXELFORMATDESCRIPTOR, Visual, FBConfig, depending on OS and graphics system)

  2. create your first window and set the visual format

  3. create the OpenGL context with respect to the first window as drawable

  4. create further windows and set them to the very same visual format you've already selected.

You can now detach the OpenGL context from its current drawable (=window) and attach it to any other drawable (i.e. window) having a compatible visual format