multithreading in opengl

Hi,

I’m trying to make a simple map viewing program in OpenGL. The map is made up of heaps of faces that have textures that only load when they need to be viewed. The problem is that when a texture is loaded it locks up my program for a bit. What I want to do is use another thread to load the textures.

I tried this using the win32 CreateThread() function. When the texture needs to load now, a thread is spawned and the program runs slightly slower as the texture is loaded (good sign). My problem is the texture never loads sucessfully. I believe this is because opengl doesnt know that my new thread is associated with the original opengl context. I tried using
wglMakeCurrent() in the new thread but that didnt work. Has anyone done
anything like this before?

Thanks,
Robert.

To make things easy, you should avoid calling gl/wgl functions etc from different threads (synchronization etc can be very difficult). My suggestion is that your secondary thread loads the texture, then signals the primary thread (i.e. say “the texture is ready in memory area blah blah” etc). The first thread does its thing (e.g. upload texture to texture memroy), and if necessary signals the secondary thread when its done (e.g. if the secondary thread needs to free up resources).

I would like to take the opportunity to recommend GLFW (an OpenGL toolkit), which has a complete (and portable) threading interface, plus a nice Users Guide that has a decent introduction to multi threading (threads, data sharing & synchronization).