PDA

View Full Version : OpenGL and multithreading in Win32



TheSpider
02-13-2004, 05:53 PM
Hi,
i was trying to create a multithreaded application that uses openGL, however when it came to text and textured quads i noticed interesting behaviour. I believe this is due to the initalisation of the text and texture data in the second (rear) thread.

If openGL is intialised in Winmain, it appears that the openGL states are not shared across winmain and the rear thread, i have a sample piece of code below




Winmain(...)
{
CBitmapDraw CBitmap;

CpBitmap=&CBitmap;
int iReturn = CBitmap.SetBitmap("NeHe.bmp");
hThread = StartThread();
while(!bTexInitialised)
{};

ReSizeGLScene(800,600);
.
.
.
Windows message loop and whatnot
}


this is followed by a windows message loop using peekmessage. note that bThreadInitalised is a global boolean variable initalised to false.
In the second thread




DWORD WINAPI ThreadProc(LPVOID pvoid)
{
//int iReturn = CpBitmap->SetBitmap("NeHe.bmp");
bTexInitialised = true;
.
.
.
Adds object to a drawing list
}



This draws just fine. however if i uncomment the int iReturn = CpBitmap->SetBitmap("NeHe.bmp"); line in the thread and comment out that line in the winmain, the code no longer displays the bitmap. as far as i can tell both bits of code are identical, unless openGL has 2 state machines running.

Is this the case? if so how can i get around it without having to put all my openGL initialisation code in the front thread?

zeckensack
02-13-2004, 06:10 PM
OpenGL is designed to be thread safe. Each thread has its own current OpenGL context. Also note that any OpenGL context can only be current to one thread. This is what you're seeing. You really should put all your GL code into one thread. It's the safest way to do things.

There's also wglShareLists, but I have my doubts that this is the right approach here.

TheSpider
02-13-2004, 08:12 PM
Thanks heaps, took me long enough to figure out why the texture wouldnt show up http://www.opengl.org/discussion_boards/ubb/smile.gif .

No, wglsharelists isnt quite what im looking for. I think i can rearrange my code to utilise the same thread for the gl code.

Thankyou zeckensack