I need to shared data (textures, vertex-buffers,... ) across all OpenGL widgets in a application.
The following code isn't working:
I've found some solutions that have one main QGLWidget
and other are constructed using this main widget. Unfortunately, I can't use this approach, because all my QGLWidgets are equal and almost certainly the first(main) created QGLWidget will be destroyed before others are.
Possible approach:
- single shared OpenGL context between all QGLWidgets
- not working: just one QGLWidget gets rendered correctly, others behave as they weren't rendered, corrupted/random data
error for each
QGLWidget
construction except first one:QGLWidget::setContext: Context must refer to this widget
Another approach:
- main OpenGL context and create sub-context for each QGLWidget
- not working:
context->isSharing()
returnsfalse
code that I use for context creation,
context1
andcontext2
are later passed to constructors of QGLWidgets:QGLContext *mainContext = new QGLContext(format), *context1, *context2; mainContext->create(); context1 = new QGLContext(format); context1->create(mainContext); context2 = new QGLContext(format); context2->create(mainContext); cout << mainContext->isSharing() << " " << context1->isSharing() << endl;
With regards to the first approach, you are not setting up sharing but trying to force the same context to be used with different QGLWidgets. As pointed out above, this is wrong and will not work.
Instead, create the QGLWidgets normally and pass the first QGLWidget in the shareWidget parameter when creating the others. This way you will get a separate context for each QGLWidget but they will all share with the context of the first one (and thus with each other). See http://qt-project.org/doc/qt-4.8/qglwidget.html#QGLWidget
Destroying the first widget before the others should not be an issue since the shared objects will be around until any of the sharing contexts are alive.