PDA

View Full Version : multiple opengl context?



Vermula
03-21-2001, 07:54 AM
What is the basic idea when I'm having multiple windows and each window has an opengl context.

Should I before every call to gl* functions call wglMakeCurrent to make sure I'm using right context?
How to prevent other threads interrupting when another thread is for example rendering?

BwB
03-21-2001, 09:00 AM
You should call wglMakeCurrent() before rendering into each context you dont need to do this for every gl call. Example:




wglMakeCurrent( hDC, Context1 );
DoAllRenderingForContext1();

wglMakeCurrent( hDC, Context2 );
DoAllRenderingForContext2();

etc..


To prevent threads from interrupting other threads you need to setup some kind of Semaphore. A Semaphore will block execution until the Semaphore becomes free. There are Semaphores in the Windows API (I think they are called something else). But you could do this with your own code. Example:




bool gs_bIAmRendering = false;

Context1Thread()
{
while( !done )
{
// this allows thread to keep running and doing other stuff until rendering is allowed
if( gs_bIAmRendering == false )
{
gs_bIAmRendering = true;
DoRender();
gs_bIAmRendering = false;
}
}
}

Context2Thread()
{
while( !done )
{
// this prevents thread from rendering until rendering is allowed
while( gs_bIAmRendering )
{ /* wait, you can do tasks here too */ }

gs_bIAmRendering = true;
DoRender();
gs_bIAmRendering = false;
}
}



This method isnt perfect but it should work for most simple tasks. The problem that you might encounter is that it doesnt keep a priority base on which thread gets the to render next, so you could potentially end up with one thread continuously grabbing the rendering while the other thread is busy...

Hope this helps!

[This message has been edited by BwB (edited 03-21-2001).]