OpenGL drawing in another thread OpenGL drawing in another thread multithreading multithreading

OpenGL drawing in another thread


The two basic rules are:

  • Any given OpenGL rendering context can be active at only one thread at a time.
  • And any thread can have only one OpenGL context been made currently active at a time.

However there is no strict association between particular OpenGL contexts and particular windows (Win32 test program for using a single OpenGL context with multiple windows), or particular thread. You can always migrate a OpenGL context from one thread to another.

Two usual approaches are:

  • Create a window in thread A, pass the window handle to thread B and create the OpenGL context then and there.

or

  • Create windows and OpenGL contexts in thread A, make the context(s) inactive in A, pass the handles to thread B and make them active there.


you need OpenGL context per each target window ... and also you need to swap contexts before rendering ... see how wglMakeCurrent is used otherwise all the rendering you are performing is done for lastly selected context.

So on each rendering thread before rendering set wglMakeCurrent(hdc, hrc) for the window you need ... and then render.

There are also other issues to take care of like: