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
Contact:

Qt and Ogre, Run Ogre in a independent thread

Post by ja0335 » Fri Nov 09, 2012 4:19 pm

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
0 x
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 » Sat Nov 10, 2012 3:26 pm

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?
0 x

TheSHEEEP
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 972
Joined: Mon Jun 02, 2008 6:52 pm
Location: Berlin

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

Post by TheSHEEEP » Sun Nov 11, 2012 9:29 pm

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.
0 x
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 » Mon Nov 12, 2012 4:03 am

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?
0 x

TheSHEEEP
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 972
Joined: Mon Jun 02, 2008 6:52 pm
Location: Berlin

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

Post by TheSHEEEP » Mon Nov 12, 2012 12:51 pm

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 ;)
0 x
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 » Wed Nov 14, 2012 3:18 am

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.
0 x

User avatar
ja0335
Gremlin
Posts: 154
Joined: Sun Jun 07, 2009 9:04 pm
Location: Bogotá, Colombia
Contact:

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

Post by ja0335 » Wed Nov 14, 2012 2:10 pm

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
0 x
Juan Camilo Acosta Arango

Post Reply