I created two windows using GLFW. The first window has an OpenGL context and the second one doesn't. What I want to do is render the same scene to both windows using a single OpenGL context. Something like this.
glBindVertexArray(vaoId);
// ... tell OpenGL to draw on first window
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(...);
// ... swap first window buffers
// ... tell OpenGL to draw on second window
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(...);
// ... swap second window buffers
glBindVertexArray(0);
The problem is I don't know how to tell OpenGL to draw on a specific window. And I also don't know how to swap buffers for a specific window. If it's necessary, I can use Win32 API.
- 
 3You don't tell OpenGL to draw to a window. When a context is made current, you provide an OS-dependent render target. That's what makes the association between a context and its window. GLFW prefers to own all this stuff, so as far as I can tell from a cursory examination of the docs, it doesn't let you do this. And if GLFW's NO_API window was not created correctly (with the right pixel format and such), then you can't attach an OpenGL context to its device context.Nicol Bolas– Nicol Bolas2018年05月20日 15:46:28 +00:00Commented May 20, 2018 at 15:46
- 
 I've found a way to make this work. I create both windows with an OpenGL context. Then I use wglMakeCurrent(GetDC(glfwGetWin32Window(targetWindow)), glfwGetWGLContext(windowWithContext)) before glDrawArrays(...) and swap buffers normally with glfwSwapBuffers(targetWindow). Unfortunately, this method creates an additional unused OpenGL context.Tudor Lechintan– Tudor Lechintan2018年05月20日 15:58:29 +00:00Commented May 20, 2018 at 15:58
- 
 @TudorLechintan: Alternatively, you could set up both context as shared (GLFW explicitely supports that in it's API). In that case, you will have all the "heavy" GL objects like Buffers and Textures available in both contexts. Just the light-weight state containers like VAOs and FBOs are per-context, as are the "global" GL states.derhass– derhass2018年05月20日 16:10:35 +00:00Commented May 20, 2018 at 16:10
1 Answer 1
As far as I'm aware, GLFW does not directly support that in it's API. It generally considers a Window and a GL context as a unit. However, with the native APIs, you can do what you want. For windows 32 in partiuclar, have a look at wglMakeCurrent(). In GLFW, you can get the required context and window handles via GLFW's native access API. Note that you will only get a HWND that way, you will have to manually use GetDC() to get the device context of the window.
Be aware that switching contexts will imply flushing the GL command queue, which can have negative effects on the performance. See GL_KHR_context_flush_control for more details.