c++openglglfwmsaa

Is there a way to change the number of MSAA samples of an existing GLFW window?


I am currently writing a game using C++, OpenGL and GLFW. I would like to allow users to change the number of samples the game uses for antialiasing, since users with old systems might want to disable antialising altogether for performance reasons.

The problem is that GLFW_SAMPLES is a window-creation hint, which means that it's applied when a window is created:

// Use 4 samples for antialiasing
glfwWindowHint(GLFW_SAMPLES, 4);

// The hint above is applied to the window that's created below
GLFWwindow* myWindow = glfwCreateWindow(widthInPix, heightInPix, title.c_str(), glfwGetPrimaryMonitor(), nullptr);

// Disable antialiasing
// This hint is not applied to the previously created window
glfwWindowHint(GLFW_SAMPLES, 4);

The GLFW documentation doesn't contain any information about how to change the number of samples of an existing window. Has anyone faced this problem in the past?


Solution

  • No, you must create a new window and destroy the old one. Preferably sharing the two contexts, so that non-container objects won't be lost in the shuffle.

    Alternatively, you can create multisampled textures or renderbuffers, render to an FBO, and then blit the rendered data to a non-multisampled window. That way, you have complete control over the number of samples, and you can easily destroy and recreate such images at your leisure.