title: OpenGL VAO's - Handling VAO's in multiple contexts
body: <p>I have run into an issue that I am afraid is going to be very hard to resolve, at least as far as google searching has show me. </p>
  
  <p>I have an Editor utility using Qt which creates multiple OpenGL contexts for different tools in the editor, their is a 'World' editor which hosts a QGLWidget of my games scene, and a material editor which has a 'preview' QGLWidget which displays the currently built material.</p>
  
  <p>I have been able to get context sharing working fine, I use gDEBugger to see the OpenGL contexts, and they are sharing Textures, VBO's, shaders etc. But one stipulation has me wondering how this will work, you can't share Vertex Array Objects between contexts. As I understand it, Vertex Array Objects are the standard now, and we should really be using them as opposed to using VBO's without VAO's. </p>
  
  <p>I have thought of a 2 ways to get around this but I don't exactly think either are ideal</p>
  
  <ol>
  <li>Generate the VAO's before each render, but this seems to defeat the purpose of the VAO's</li>
  <li>use a std::map to map a GL context to a VAO, if the current context does not have this VAO, then generate one for the said context, this seems bad and may not even work.</li>
  </ol>
  
  <p>What other solutions are there that I am overlooking? I have also considered somehow having everything in one context and using seperate viewports for each required opengl widget, which I hoped would be possible but I am having no luck figuring it out using Qt and it's QGLWidget.</p>
  
  <p><em>Edit</em></p>
  
  <p>Ok, so I have tried to get this working but it is giving me alot of grief, I tried 2 different ways and they are both causing me errors.</p>
  
  <p>1) I create a QGLContext, and then pass it to my QGLWidgets when they are being created.</p>
  
  <pre><code>QGLFormat    fmt = QGLFormat();
  QGLContext*  pContext = new QGLContext(fmt);
  
  QGLWidget*   pWidget1 = new QGLWidget(pContext);
  someLayout-&gt;addWidget(pWidget1);
  
  QGLWidget*   pWidget2 = new QGLWidget(pContext);
  anotherLayout-&gt;addWidget(pWidget2);
  </code></pre>
  
  <p>The error here is that as soon as I add the widget to a layout, or set it as the central widget for a Main Window, it deletes the context, really weird. If I then try to pass in the context from the first widget to the second</p>
  
  <pre><code>QGLFormat    fmt = QGLFormat();
  QGLContext*  pContext = new QGLContext(fmt);
  
  QGLWidget*   pWidget1 = new QGLWidget(pContext);
  someLayout-&gt;addWidget(pWidget1);
  
  pContext = (QGLContext*)pWidget1-&gt;context();
  QGLWidget*   pWidget2 = new QGLWidget(pContext);
  anotherLayout-&gt;addWidget(pWidget2);
  </code></pre>
  
  <p>I Get a Qt error saying QGLWidget::setContext: Context must refer to this widget</p>
  
  <p>2) I create my first widget and use it's context for all the others</p>
  
  <pre><code>QGLWidget* pWidget1 = new QGLWidget();
  
  QGLContext* pContext = (QGLContext*)pWidget-&gt;context();
  QGLWidget* pWidget2 = new QGLWidget(pContext);
  </code></pre>
  
  <p>This gives me the same error that I got from the end of my first method, it says
  QGLWidget::setContext: Context must refer to this widget.</p>
  
  <p>Something is not right here and I feel I am missing something.</p>