PDA

View Full Version : QGLWidget multithreaded example?



danielperaza
28th April 2011, 16:09
Hi, I'm working with OpenGL and QGLWidget, but I'd like to do some animations on rotation and zoom operations, so I'm trying to find some examples of how to do multithreaded drawings with OpenGL. At first sight, I wasn't able to find any examples on this subject in QtAssistant.

I guess I must share the QGLContext between the GUI and the animation thread, calling makeCurrent() and doneCurrent() at beginning/end of the animation, and waking up the thread each N seconds, where N is the time step between each frame, and swapping buffers. But I wonder how could I define a common interface for all my animations?

Dong Back Kim
5th August 2011, 07:15
Hi, I'm working with OpenGL and QGLWidget, but I'd like to do some animations on rotation and zoom operations, so I'm trying to find some examples of how to do multithreaded drawings with OpenGL. At first sight, I wasn't able to find any examples on this subject in QtAssistant.

I guess I must share the QGLContext between the GUI and the animation thread, calling makeCurrent() and doneCurrent() at beginning/end of the animation, and waking up the thread each N seconds, where N is the time step between each frame, and swapping buffers. But I wonder how could I define a common interface for all my animations?

I was also trying to achieve the same thing a while ago. After bit of digging, I have learned the followings.

"OpenGL machine state is not designed to be multithreaded."

This means a single rendering context is meant to be used by one thread. I attempted to create multiple rendering contexts and let them render different frames (i.e. even number frames in context 1 and odd number frames in context 2). This sounds like "multithreading" but then when you call "makeCurrent()", it means from that moment until doneCurrent() or another makeCurrent(), the OpenGL machine is dedicated to the current rendering context only which means it acts as kind of mutex mechanism.

I also learned that this will be changed in future (even now) so if in your hardware, graphic card has more than one GPU then multithreading might occur but then this may also not be true unless the driver in your OS supports dual GPU stuff.

Regards,
Dong Back Kim