C++ – a good way to load textures dynamically in OpenGL


Currently I am loading an image in to memory on a 2nd thread, and then during the display loop (if there is a texture load required), load the texture.

I discovered that I could not load the texture on the 2nd thread because OpenGL didn't like that; perhaps this is possible but I did something wrong – so please correct me if this is actually possible.

On the other hand, if my failure was valid – how do I load a texture without disrupting the rendering loop? Currently the textures take around 1 second to load from memory, and although this isn't a major issue, it can be slightly irritating for the user.

Best Solution

You can load a texture from disk to memory on any thread you like, using any tool you wish for reading the files.

However, when you bind it to OpenGL, it's going to need to be handled on the same thread as the rendering for that OpenGL context. That being said, this discussion suggests that using a PBO in a second thread is an option, and can speed up the process.

Related Question