Multithreaded OpenGL ?

Hello !
This is my first post here, so please, be gentle …

I am using OGL under WindowsNT, with VC++ 5.0, via GLUT.
It looks like I cannot use OpenGL outside the main thread, and it would be great, since the main thread renders on screen, while a secondary one pre-fetches from disk and prepares Display Lists.
How should I do this ?
I cannot use ANY OGL function from the secondary thread !

Threads are created with _beginthread().

BTW, I will quit GLUT as soon as I enter the production stage, but it serves me well at this moment

Thank you !


You only have one OpenGL rendering context, so you can’t call GL functions from two different threads. If you do, the order of operations will be all screwed up!
You can still perform prefetching and rendering in separate threads, but if you insist on building display lists on the fly, you’ll have to do it in the same thread that does your rendering.

While we’re on the subject, has anyone ever tried to have two threads render simultaneously to separate rendering contexts?

Ok. Thank you. That’s what I wanted to know.
I suppose I’ll have to read the meshdata from the secondary thread and make it available to the main one, which will create the lists.
Unfortunately, this takes a bit, and the navigation pauses


Consider having the second thread assemble vertex arrays instead of display lists. They can be almost just as fast, and you can fill them up in a separate thread before you hand them to the GL.

Good luck,

Ok. Thank you.
I already read Vertex Arrays from disk.
Anyway, I found what is pausing the navigation : Uploading the textures to the 3D board. Unfortunately, that’s an abvious unthreadable process !
I’ll try uploading the textures a little bit at a time while I still don’t need them and hope they’ll be fully loaded by the time I need them.
Any other Ideas ?


Originally posted by Emiliano:
Ok. Thank you. That’s what I wanted to know.

Unfortunately, this takes a bit, and the navigation pauses :frowning:

I’m not sure about GLUT, but I would’ve thought you could use multi-threaded techniques to eliminate (or greatly reduce) the navigation freezing.

For example, do as much processing as possible outside of the main thread and just use the main thread for relatively simple stuff (responding to menus, redrawing the display etc).

:slight_smile: ?


additionally, some toolkits (Cosmo3D comes to mind) make a big deal about providing thread classes.

Presumably this is for cases when you have many contexts/windows and want to use a different thread to render each one?

There is also a demo called glThread.exe (I did a search for the filename) that shows simple rendering using two threads into the same context. Note that only one thread goes at one time… still, may be useful…


How are you guys multi-threading with glut? Ive tried for a while now but I cant get my screen to refresh. I tried a simple example of having the thread update a spheres position:

RenderMain() is my glutDisplayFunc callback. Tx & Ty are globals used in glTranslatef().

void Menu(int index)
if (!busy)
cout << “Starting thread” << endl;
busy = true;
_beginthread(Interpolate, 0, NULL);

VOID Interpolate(PVOID pVoid)
GLfloat r = 5;
for (int i=0; i<10; i++)
for (GLfloat a=0; a<360; a++)
Tx = r * cos(D2R(a));
Ty = r * sin(D2R(a));
cout << “Exiting thread” << endl;
busy = false;

I’ve not been coding multi-threaded for very long, but I have had good results with MFC and the WIN32 API. With MFC Id pass a pointer to my CWnd class so I could call its On_Paint(), from within the thread. With the API Id pass a pointer to a struct containing the main windows handle, again, so I could post a WM_PAINT from within the thread. How does this work with Glut?


There is a way to build display lists in one thread and use them in another. You have to creat two rendering contexts, one in each thread and call the wglShareList method on one of them.

Ok. Thank you all guys.
The problem at this moment is that I need to continuously upload the textures at the same time I am navigating. The hardware is using some textures while I am updating some other ones. Any idea to avoid navigation freezes ?


I have developped a texture cache system which maintains a list of textures and the slot they are loaded in. I also have an expire counter, making the texture slot available if the texture isn’t used for a specified time. Maybe you can do a similar system which also can “anticipate” the use of textures and precache them into main memory instead of loading them from the disk.
Mine is a C++ class.