QOpenGLFunctions 的渲染问题

Renderproblem with QOpenGLFunctions

我想在 QML 应用程序中编写自定义 OpenGL 小部件,以使用 MathGL 绘制数据。
为此,我查看了 http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html
的场景图示例 然后我根据我的需要调整代码,然后问题发生了,图像只闪烁了一个渲染周期,之后就不再出现了。 以下是重要的函数和绑定。

class GLRenderEngine : public QObject, public QOpenGLFunctions

void GLRenderEngine::render()
{
    if(!m_bInit)
    {
        initializeOpenGLFunctions();
        m_pGraph = new mglGraph( 1 );
        m_bInit = true;
    }
    glViewport(m_Viewport.left(), m_Viewport.top(), m_Viewport.width(), m_Viewport.height());
    m_pGraph->Clf();
    //Graph stuff ...
    m_pGraph->Finish();
    if(m_pWindow)
        m_pWindow->resetOpenGLState();
}


class GLWidget : public QQuickItem

GLWidget::GLWidget(QQuickItem *parent) : QQuickItem(parent)
{
    m_pRender = 0;
    connect(this, &QQuickItem::windowChanged, this, &GLWidget::handleWindowChanged);
}

void GLWidget::handleWindowChanged(QQuickWindow *win)
{
    if(win)
    {
        connect(win, &QQuickWindow::beforeSynchronizing, this, &GLWidget::sync, Qt::DirectConnection);
        connect(win, &QQuickWindow::sceneGraphInvalidated, this, &GLWidget::cleanup, Qt::DirectConnection);
        win->setClearBeforeRendering(false);
    }
}

void GLWidget::cleanup()
{
    if(m_pRender)
    {
        delete m_pRender;
        m_pRender = 0;
    }
}

void GLWidget::sync()
{
    if(!m_pRender)
    {
        m_pRender = new GLRenderEngine();
        connect(window(), &QQuickWindow::beforeRendering, m_pRender, &GLRenderEngine::render, Qt::DirectConnection);
    }
    m_pRender->setViewportSize(boundingRect());
    m_pRender->setWindow(window());
}

QML-文件

import QtQuick 2.8
import QtQuick.Window 2.2
import GLWidget 1.0

Window {
    visible: true
    width: 320
    height: 480

    GLWidget{
        anchors.fill: parent
        id: glView
    }

    Rectangle {
        color: Qt.rgba(1, 1, 1, 0.7)
        radius: 10
        border.width: 1
        border.color: "white"
        anchors.fill: label
        anchors.margins: -10
    }

    Text {
        id: label
        color: "black"
        wrapMode: Text.WordWrap
        text: "The background here is a squircle rendered with raw OpenGL using the 'beforeRender()' signal in QQuickWindow. This text label and its border is rendered using QML"
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        anchors.margins: 20
    }
}

我还注意到,当我使用 QQuickFramebufferObject 时,图像在调用 update() 或 window 的调整大小事件后消失了,即使正在调用渲染函数,所以我的猜测是缓冲区未更新或 qt 的其他内容已关闭。
在此先感谢您的帮助。

为了解决这个问题,我切换到 QQuickFrameBuffer 实现并删除了我在渲染函数中使用的所有 glClear 命令,同时启用了基础 class QQuickItem 的清除标志。 它现在就像一个魅力。