如何共享OpenGL上下文或数据? [英] How to share OpenGL context or data?

查看:758
本文介绍了如何共享OpenGL上下文或数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在应用程序中的所有OpenGL小部件之间共享数据(纹理,顶点缓冲区等).

I need to shared data (textures, vertex-buffers,... ) across all OpenGL widgets in a application.

以下代码不起作用:

我发现一些具有一个主QGLWidget的解决方案,而其他解决方案是使用此主窗口小部件构造的.不幸的是,我不能使用这种方法,因为我所有的QGLWidgets都是相同的,并且几乎可以肯定,第一个(主要)创建的QGLWidgets将在其他QGLWidgets之前被销毁.

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.

可能的方法:

  • 所有QGLWidget之间的单个共享OpenGL上下文
  • 不起作用:只有一个QGLWidget被正确渲染,其他QGLWidget却表现为未渲染,损坏/随机数据
  • 每个QGLWidget构造的
  • 错误,第一个错误除外:

  • 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

另一种方法:

  • 主要OpenGL上下文,并为每个QGLWidget创建子上下文
  • 不起作用:context->isSharing()返回false
  • 我用于上下文创建的
  • 代码context1context2稍后传递给QGLWidgets的构造函数:

  • main OpenGL context and create sub-context for each QGLWidget
  • not working: context->isSharing() returns false
  • code that I use for context creation, context1 and context2 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;

推荐答案

关于第一种方法,您不是在设置共享,而是试图将同一上下文强制用于不同的QGLWidgets.如上所述,这是错误的,将不起作用.

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.

相反,请正常创建QGLWidget,并在创建其他QGLWidget时在shareWidget参数中传递第一个QGLWidget.这样,您将为每个QGLWidget获得一个单独的上下文,但是它们都将与第一个QGLWidget共享(因此彼此共享).参见 http://qt-project.org/doc/qt-4.8/qglwidget.html#QGLWidget

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.

这篇关于如何共享OpenGL上下文或数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆