Results 1 to 2 of 2

Thread: multiple opengl context?

  1. #1

    multiple opengl context?

    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?

  2. #2
    Senior Member Regular Contributor
    Join Date
    Aug 2000
    Location
    Ocoee, Florida, USA
    Posts
    150

    Re: multiple opengl context?

    You should call wglMakeCurrent() before rendering into each context you dont need to do this for every gl call. Example:

    Code :
    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:

    Code :
    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).]

Similar Threads

  1. openCl Multiple context ! is it possible ?
    By Belkacem in forum OpenCL
    Replies: 1
    Last Post: 02-02-2012, 05:11 PM
  2. Buffer orphaning and multiple context
    By aqnuep in forum OpenGL: Advanced Coding
    Replies: 10
    Last Post: 01-13-2011, 10:13 AM
  3. Replies: 1
    Last Post: 08-20-2010, 12:19 PM
  4. Multiple threads and one context
    By Taicoon in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 05-06-2010, 09:34 AM
  5. Replies: 2
    Last Post: 04-26-2010, 09:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean