Qt and Ogre, Run Ogre in a independent thread

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.
Post Reply
User avatar
ja0335
Gremlin
Posts: 154
Joined: Sun Jun 07, 2009 9:04 pm
Location: Bogotá, Colombia
x 4
Contact:

Qt and Ogre, Run Ogre in a independent thread

Post 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
Juan Camilo Acosta Arango
Pulas
Halfling
Posts: 61
Joined: Sat Oct 29, 2011 9:39 am

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

Post 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?
TheSHEEEP
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 972
Joined: Mon Jun 02, 2008 6:52 pm
Location: Berlin
x 65

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

Post 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.
My site! - Have a look :)
Also on Twitter - extra fluffy
Pulas
Halfling
Posts: 61
Joined: Sat Oct 29, 2011 9:39 am

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

Post 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?
TheSHEEEP
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 972
Joined: Mon Jun 02, 2008 6:52 pm
Location: Berlin
x 65

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

Post 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 ;)
My site! - Have a look :)
Also on Twitter - extra fluffy
Pulas
Halfling
Posts: 61
Joined: Sat Oct 29, 2011 9:39 am

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

Post 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.
User avatar
ja0335
Gremlin
Posts: 154
Joined: Sun Jun 07, 2009 9:04 pm
Location: Bogotá, Colombia
x 4
Contact:

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

Post 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
Juan Camilo Acosta Arango
Post Reply