2

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.

asked May 20, 2018 at 15:25
3
  • 3
    You 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. Commented 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. Commented 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. Commented May 20, 2018 at 16:10

1 Answer 1

4

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.

answered May 20, 2018 at 15:55
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. Fortunately, flushing the command queue isn't a problem since my app stops rendering on the first window until the second one is closed. I wanted to implement something like a message box.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.