I am trying to multi-thread my OpenGL code but I have been having trouble. At first, I thought that using opengl inside an alternate thread was impossible, so I just did loading operations and passed the data to opengl in the main thread (I knew once I started passing too much data the main thread would lockup, and noticeably freeze the execution of the program while the data was being moved). I found some forum threads discussing opengl and multi-threading but they don't discuss the details that I'm looking for. namely, how and where do I use wglMakeCurrent() and wglShareLists(). I have been trying to create a display list in the alternate thread with the instructions outlined in the aforementioned forum posts but it crashes every time. I can load the model data, I can even load shaders, but loading a model (of any size) crashes the program. I will show some code snippets but there is really too much to put in one post:
//This is the code initializing the 2 Render Contexts
if (!(app.hDC=GetDC(app.hWnd)) || // Get A Device Context?
!(PixelFormat=ChoosePixelFormat(app.hDC,&pfd)) || // Did Windows Find A Matching Pixel Format?
!SetPixelFormat(app.hDC,PixelFormat,&pfd) || // Able To Set The Pixel Format?
!(app.hRC=wglCreateContext(app.hDC)) || // Able To Get A Rendering Context?
!(app.hRC_thd=wglCreateContext(app.hDC)) || // Get the second context (for loading in parallel thread)
!(wglShareLists(app.hRC, app.hRC_thd)) ||
!wglMakeCurrent(app.hDC,app.hRC)) // Try To Activate The Rendering Context
{
KillGLWindow();
return false;
}
This next bit is all the initialization and render using opengl
GLuint o; // display list handle
vector<v3> v,n; // verts, norms
vector<v2> u; // texcoords (UVs)
vector<face> f; // polygons (faces)
bool done = false;
void load_thread(void* null) // alternate thread entry point
{
wglMakeCurrent(app.hDC, app.hRC_thd); // works fine (seemingly)
load_rs_shdr(); // works fine
load_obj("cell.obj", v,n,u,f); // works fine
gen_display_list(o, v, n, u, f); // crashes my program
done = true;
return;
}
bool init()
{
init_med(v3(0,0,0)); //function to initialize opengl (enable depth, lighting, etc...)