Page 1 of 1

Qt and Ogre, Run Ogre in a independent thread

Posted: Fri Nov 09, 2012 4:19 pm
by ja0335
Hi..

I am developing a program with a user interface based on Qt by Nokia, this user interface manipulates configurations over the current ogre running instance. The problem comes when i for example open a dialog to do some hard calculation stuff, this action makes the ogre render look at low frame rates.

I want to know if is possible to run ogre in an independent thread. Thanks

Re: Qt and Ogre, Run Ogre in a independent thread

Posted: Sat Nov 10, 2012 3:26 pm
by Pulas
I also want to know how to put ogre entirely on the second thread, while Qt GUI thread is the main thread.
Can anyone give me some hints?

Re: Qt and Ogre, Run Ogre in a independent thread

Posted: Sun Nov 11, 2012 9:29 pm
by TheSHEEEP
Well, instead of creating the Ogre Root (and using Ogre) in the main thread when you attach it to the window, you could create a new thread inside that "attaching" function - passing the needed window data (handle, etc.) to it - and only use Ogre from that threat.

Of course, that would require some sort of messaging system to communicate between the main app thread and the Ogre thread when you want to change something (very likely, why else would you want to combine Qt + Ogre ;) ).

I am using tinythread++ for most of my threading needs, but you could also do that as easy with C++11 or boost.
tinythread++ comes with some nice examples, though. Took me only little time to get it and I never did anything with threading before.

Re: Qt and Ogre, Run Ogre in a independent thread

Posted: Mon Nov 12, 2012 4:03 am
by Pulas
Thanks a lot for your useful reply.
TheSHEEEP wrote:Of course, that would require some sort of messaging system to communicate between the main app thread and the Ogre thread when you want to change something (very likely, why else would you want to combine Qt + Ogre ;) ).
As you said, that would require some sort of messaging system.
I have no idea about such messaging system.
Is there any similar system I can refer?

Re: Qt and Ogre, Run Ogre in a independent thread

Posted: Mon Nov 12, 2012 12:51 pm
by TheSHEEEP
There are surprisingly few tutorials about doing simple messaging systems in a multithreaded environment.
The problem is that even a very simple solution is not really easy. Thread synchronization never is ;)

The first answer in this thread has a very simple example, maybe that helps.
But I am sure you will have to write something yourself. I am not aware of a ready-to-use-and-simple thread-safe message queue...

What I do, personally, is having one object that is shared across all threads (each thread has a pointer to it, passed at thread creation). That object has a status, and the threads check that status to see if they have to do something. So, my "messages" are actually only one enum with very few "commands".

If you need something more complex, I guess you'll have to dive into boost thread or the c++11 thread stuff (which is the same as boost thread if I got that right). For those, you'll find examples, but none of them are simple, IMO ;)

Re: Qt and Ogre, Run Ogre in a independent thread

Posted: Wed Nov 14, 2012 3:18 am
by Pulas
TheSHEEEP wrote:The first answer in this thread has a very simple example, maybe that helps.
But I am sure you will have to write something yourself. I am not aware of a ready-to-use-and-simple thread-safe message queue...
Yeah, that simple example helps. But I encountered the same problem as below:
http://gamedev.stackexchange.com/questi ... ne-less-cu

After some searching, I found that Qt provide some methods for QThread communicaton:
1. signal/slot;
2. QApplication::sendEvent();
3. QApplication::postEvent();

IMO, the third method is the best choice.

BTW, there is a good example for show how to communicate between threads.
It is in the "$(QTDIR)/examples/threads/mandelbrot" directory.
Hope this can help someone.

Re: Qt and Ogre, Run Ogre in a independent thread

Posted: Wed Nov 14, 2012 2:10 pm
by ja0335
Pulas wrote:
TheSHEEEP wrote:The first answer in this thread has a very simple example, maybe that helps.
But I am sure you will have to write something yourself. I am not aware of a ready-to-use-and-simple thread-safe message queue...
Yeah, that simple example helps. But I encountered the same problem as below:
http://gamedev.stackexchange.com/questi ... ne-less-cu

After some searching, I found that Qt provide some methods for QThread communicaton:
1. signal/slot;
2. QApplication::sendEvent();
3. QApplication::postEvent();

IMO, the third method is the best choice.

BTW, there is a good example for show how to communicate between threads.
It is in the "$(QTDIR)/examples/threads/mandelbrot" directory.
Hope this can help someone.

Thanks :o