我想解决我仍然要解决的问题。那就是使用共享着色器程序等在不同的顶层窗口中同时渲染2个QOpenGLWidgets。

为什么我在这里而不是Qt论坛上发布它?我已经做过,但是没有人回应:/

我的第一个尝试是使用一个上下文,没有用。

问题:QOpenGLWidget当前是否有可能?还是我必须去旧的QGLWidget?或使用其他东西?

Qt::AA_ShareOpenGLContexts的testAttribute返回true,因此共享没有问题
甚至QOpenGLContext::areSharing也返回true。所以有一些我想念或不知道的东西。不使用线程。

如果您还需要其他任何东西来调试此问题,请告诉我。

调试输出:



MapView的initializeGL:

void MapView::initializeGL()
{
    this->makeCurrent();

    initializeOpenGLFunctions();

    // Initialize World
    world->initialize(this->context(), size(), worldCoords);

    // Initialize camera shader
    camera->initialize();

    // Enable depth testing
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glDepthFunc(GL_LEQUAL); // just testing new depth func

    glClearColor(0.65f, 0.77f, 1.0f, 1.0f);
}

MapView paintGL:
void MapView::paintGL()
{
    this->makeCurrent();

    glDrawBuffer(GL_FRONT);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    world->draw(...);
}

MapExplorer initializeGL:
void MapExplorer::initializeGL()
{
    this->makeCurrent();

    QOpenGLContext* _context = _mapView->context();
    _context->setShareContext(this->context());
    _context->create();

    this->context()->create();
    this->makeCurrent();

    initializeOpenGLFunctions();

    // Enable depth testing
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glDepthFunc(GL_LEQUAL); // just testing new depth func

    glClearColor(0.65f, 0.77f, 1.0f, 1.0f);
}

MapExplorer paintGL:
void MapExplorer::paintGL()
{
    this->makeCurrent();

    qDebug() << "MapExplorer" << QOpenGLContext::areSharing(this->context(), _mapView->context()) << (QOpenGLContext::currentContext() == this->context());

    QOpenGLShaderProgram* shader = world->getTerrainShader();
    qDebug() << shader->create();
    shader->bind(); // debug error "QOpenGLShaderProgram::bind: program is not valid in the current context."

    // We need the viewport size to calculate tessellation levels and the geometry shader also needs the viewport matrix
    shader->setUniformValue("viewportSize",   viewportSize);
    shader->setUniformValue("viewportMatrix", viewportMatrix);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    qDebug() << "MapExlorer paintGL ends";

    //world->drawExplorerView(...);
}

谢谢。

问候,glararan。

最佳答案

嗨,我已经破解了2天,终于有了一些工作。

主要引用是qt's threadrenderer example

基本上,我有一个带有自己上下文的 QOpenglWidget ,还有一个背景线程,用于从 QOpenglWidget 共享上下文来绘制。后台线程绘制的帧缓冲区可以由 QOpenglWidget 直接使用。

这是使事情正常进行的步骤:

  • 我有 QOpenglWidget RenderEngine后台线程 RenderWorker
    // the worker is a thread
    class RenderWorker : public QThread, protected QOpenGLFunctions
    {
        // the background thread's context and surface
        QOffscreenSurface *surface = nullptr;
        QOpenGLContext *context = nullptr;
    
        RenderWorker::RenderWorker()
        {
            context = new QOpenGLContext();
            surface = new QOffscreenSurface();
        }
    
        ...
    }
    
    // the engine is a QOpenglWidget
    class RenderEngine : public QOpenGLWidget, protected QOpenGLFunctions
    {
    protected:
        // overwrite
        void initializeGL() override;
        void resizeGL(int w, int h) override;
        void paintGL() override;
    
    private:
        // the engine has a background worker
        RenderWorker *m_worker = nullptr;
    
        ...
    }
    
  • QOpenglWidget initializeGL()中创建和设置后台线程
    void RenderEngine::initializeGL()
    {
        initializeOpenGLFunctions();
    
        // done with current (QOpenglWidget's) context
        QOpenGLContext *current = context();
        doneCurrent();
    
        // create the background thread
        m_worker = new RenderWorker();
    
        // the background thread's context is shared from current
        QOpenGLContext *shared = m_worker->context;
        shared->setFormat(current->format());
        shared->setShareContext(current);
        shared->create();
    
        // must move the shared context to the background thread
        shared->moveToThread(m_worker);
    
        // setup the background thread's surface
        // must be created here in the main thread
        QOffscreenSurface *surface = m_worker->surface;
        surface->setFormat(shared->format());
        surface->create();
    
        // worker signal
        connect(m_worker, SIGNAL(started()), m_worker, SLOT(initializeGL()));
    
        // must move the thread to itself
        m_worker->moveToThread(m_worker);
    
        // the worker can finally start
        m_worker->start();
    }
    
  • 后台线程必须在其自己的线程中初始化共享上下文
    void RenderWorker::initializeGL()
    {
        context->makeCurrent(surface);
        initializeOpenGLFunctions();
    }
    
  • 现在,通过 QOpenglWidget 可直接使用(在中绘制的任何帧缓冲区后台线程(作为纹理等),例如在paintGL()函数中。

  • 据我所知,opengl上下文是绑定(bind)到线程的一种。 必须在主线程中创建并设置共享上下文和相应的表面,移至另一个线程,并且在其中初始化了,才能最终使用它。

    10-08 00:23