OpenGL question

I want my OpenGL updates to happen regularly. Using the JUCE Timer does not work, because as soon as there’s some work going on in the main thread, the animation is not regular anymore.

So what I did is to make my OpenGLComponent inherit from Thread too, so run() takes over the timing and the Thread runs at rather high priority 7:

void MyOpenGLComponent::run() { while (!threadShouldExit()) { Thread::sleep(20); // sleep 20 milliseconds if (contextWasCreated) // set to true at the very end newOpenGLContextCreated() renderAndSwapBuffers(); // render! } }

In my first (huge) application, this works perfectly.

In my second (simpler) application, it does not work.
Is it even ok to do the animation timing like that or is it just “luck” that it worked in the first application and not the second application?

Any help appreciated! Thanks!

Basically it seems to be the function OpenGLComponent::makeCurrentContextActive() that fails because wglContextActive() fails with GetLastError=170 which means: “The requested resource is in use.”

Ok, and here’s the solution to the problem:

  • you have to call makeCurrentContextInactive() at the end of renderOpenGL(), because this tells OpenGL that the context is no longer used (active) by the actual Thread, so another Thread can use it.

And you have to make sure that you only access the context from one thread at a time.

In Linux, you really need to create everything - including a Display connection, on the thread. Let me know if you want an example.