I want to load some meshes including OpenGL-textures while the screen shows an animated loading screen (rendered with OpenGL).
So I do my loading stuff in a second thread which of course does not work, because I can't access the same GL-context from two threads.
I am using SDL 1.2 and the whole thing has to be platform independent, so I can't use wgl-stuff.
Which possibilities do I have?
Use Pixel Buffer Objects to create address space mappings for image data (glMapBuffer), the mapped OpenGL memory region can be accessed from all threads of the process. Copy the image data into this using the secondary thread, then glUnmapBuffer and glTexImage from the PBO in the main thread. Both unmapping and texture upload happen asynchronous so that you can already process the next image while the previous is still transferred to fast memory.
All you need then is just a way to communicate between the threads which task to perform next and when this task has been finished.
It's surely not the most elegant solution, but you can just draw your loading screen with an empty bar, load some assets and increase the bar and then draw it again. repeat until you have loaded everything.
in pseudo:
loadgraphics(){
int progress=0;
glClear(GL_COLOR_BUFFER_BIT);
//draw you loading bar from 0 to progress
//load something
++progress;
SwapBuffers(hdc);
glClear(GL_COLOR_BUFFER_BIT);
//draw you loading bar from 0 to progress
//load something
++progress;
SwapBuffers(hdc);
glClear(GL_COLOR_BUFFER_BIT);
//draw you loading bar from 0 to progress
//load something
++progress;
SwapBuffers(hdc);
glClear(GL_COLOR_BUFFER_BIT);
//draw you loading bar from 0 to progress
//load something
++progress;
SwapBuffers(hdc);
glClear(GL_COLOR_BUFFER_BIT);
//draw you loading bar from 0 to progress
//load something
++progress;
SwapBuffers(hdc);
}
This really is not the way to go, but it works if you just want a quick n dirty way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With