4

正如 Qt 用户所知,使用任何 OpenGL 扩展都是相当麻烦的。我使它工作的方式是像这样扩展 QGLFunctions 类:

 class Object3D : protected QGLFunctions{ 
 ... 
 }

为了使 Object3D 能够正确运行,以便它可以调用 glGenBuffer() 等函数,需要调用

initializeGLFunctions(glWidget->context());

在创建 QGLWidget 之后,否则它会在使用任何扩展功能时简单地使应用程序崩溃。虽然我终于可以在 Object3D 存在期间调用“glGenBuffer()”和其他调用,但它似乎在包含“glDeleteBuffer()”调用的 ~Object3D() 调用时崩溃。我确信正是这个调用导致应用程序崩溃。

有谁知道如何解决这个问题?我怀疑这是因为 QGLWidget 在 Object3D 之前首先被删除,所以 QGLWidget 的上下文消失了。如果是这种情况,我如何确保 QGLWidget 最后被删除,因为 QGLWidget 被添加到 QMainWindow 中,它只是按照添加顺序删除其子级?

4

1 回答 1

2

The general rule is, if you can't guarantee that the OpenGL RAII object will be destroyed while the context is still around, then don't wrap OpenGL objects inside RAII C++ classes. Manage the lifetime of OpenGL objects in other ways.

It's your code (which you neglected to show us); only you can decide where or how to manage the destruction of classes. You need some system to manage your objects that will ensure that things are destroyed (and created) in the proper order.

于 2012-11-11T23:57:28.270 回答