在特定上下文中创建资源

时间:2016-05-11 12:58:31

标签: qt opengl openglcontext

我有一个使用scores = [entry.mean_validation_score for entry in grid.grid_scores_] # the shape is according to the alphabetical order of the parameters in the grid scores = np.array(scores).reshape(len(C_range), len(gamma_range)) for c_scores in scores: plt.plot(gamma_range, c_scores, '-') 呈现内容的Qt OpenGL应用程序。在另一个类(让我们称之为QOpenGLWidget)中,我想为这个小部件创建OpenGL资源,如VBO,VAO,着色器程序等。此创建方法不是由Resources调用,而是由外部调用者调用。

由于某种原因,我的应用程序中存在两个OpenGL上下文(一个可能用于GUI内容,另一个用于QOpenGLWidget)。因此,当调用资源创建方法时,我无法确定正确的上下文是否处于活动状态。所以当我打电话时

QOpenGLWidget
在Resources类中,我不能确定这个VAO是在正确的上下文中创建的。 Resources类无权访问该窗口小部件。因此,QOpenGLVertexArrayObject vao; vao.create(); 无法调用(因为我不知道表面)。

是否有直接的方法来指定应在其上创建资源的上下文?将表面存储在Resources文件中(以及上下文)似乎非常不整洁。

1 个答案:

答案 0 :(得分:1)

显然,无法在特定上下文中创建资源。我使用以下结构解决了这个问题:

我创建了一个接口OpenGLContextProvider,这非常简单:

class OpenGLContextProvider
{
public:
    virtual void MakeOpenGLContextCurrent() = 0;
};

OpenGL小部件实现了这个接口:

class GLView : public QOpenGLWidget, public OpenGLContextProvider
{
    //...
};
void GLView::MakeOpenGLContextCurrent()
{
    makeCurrent();
}

因此,OpenGLContextProvider(即OpenGL小部件)被注入到Resource对象的构造函数中。在它需要上下文之前,它调用相应的方法:

void Resources::LoadSomeData()
{
    //Load data...

    //Create OpenGL resources
    ctx->MakeOpenGLContextCurrent(); //ctx is of type OpenGLContextProvider*
    vao.create(); //is now on the correct context
    //etc.
}
相关问题