Using OpenGL in a Thread

Moderator
Posts: 370
Joined: 2006.08
Post: #1
So, I've been working on making my program use pthreads. What I want to do is make a thread that handles the graphics, and then a seperate one for gameplay. However, I'm not quite sure how to do this. I'm using GLUT for my windowing, and this needs to work on Windows too.
I could have the graphics thread contain a struct that has a buffer for OpenGL to render to (I assume it is possible to have OpenGL render to a buffer of your choosing? Any links to information on that would be great), and then when draw() is called by GLUT, it goes and gets the information in the buffer (if the drawing thread is done drawing), and displays it, otherwise it sleeps until the information is ready, and then displays it. The graphics struct would contain a queue of pointers to objects and animations that needed to be rendered, and the game thread would then tell the graphics thread to add pointers and get rid of certain pointers.
However, I'm not quite sure how to do this, and I think my idea is inherently flawed because it would require a lot of blocking and waiting between threads. I'd also like to move my physics code to another thread, which would mean that if the physics thread was moving objects and the graphics thread was rendering them in a different order (so the same object wasn't being accessed at the same time), you're going to end up with some weird looking frames as one object has responded to physics half-way through the rendering and others haven't.

Any ideas on how to improve my current model would be appreciated Smile
Thanks,
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
With GLUT, you have no choice but to render on the main thread (in the display() callback no less); all your GL calls must be made there.

If you write your own windowing code you can get a bit more flexibility, but there are still a lot of very strict rules to adhere to.

You can see an example of multithreaded physics (graphics and physics framerates completely independent) at http://onesadcookie.com/svn/ThreadedNewton
Quote this message in a reply
Moderator
Posts: 370
Joined: 2006.08
Post: #3
I'll definitely take a look at that Smile
Is there any way for my graphics thread to render everything into a buffer and then just display the contents of that buffer when display() from glut is called? Is so, will that cause problems with my picking code, which uses glRenderMode(GL_SELECT), if its called at the same time that the graphics thread is trying to render?
Thanks for you help Smile
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
I have no idea what you're asking really, but as I said, with GLUT, you can only make GL calls on the main thread, from the appropriate callback. If you write your own windowing code, you can get a bit more flexibility.
Quote this message in a reply
Hog
Member
Posts: 151
Joined: 2002.09
Post: #5
OpenGL already does buffering so no need to do that (just don't separate your OpenGL calls into different threads, it wouldn't be safe anyways probably).
You don't need to redraw if nothing has changed.
What you probably could do is to double buffer your scene and redraw a copy of it while updating your physics, but I don't think that's necessary unless you need to explicitly handle parallel processing.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL ES 2.0 Problems - Senior Project Thread Zammy 3 4,320 Mar 16, 2010 12:32 PM
Last Post: arekkusu