Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QT / OpenGL and Switch to fullscreen crashes



  • Hi,

    I'm currently trying to dig in to the details how to use QT in combination with OpenGL
    In the past I've been using glBegin and glEnd commands to draw my content. I read that this is outdated so I tried updating my code to match up with the rendering pipeline in QT.

    I tried to implement a simple example just drawing a red rectangle in an QTOpenGLWidget and provide the possibility to switch to fullscreen by double clicking on the rendering content.

    This worked in the past with glBegin and glEnd and also worked as long as I created the vertices on the fly in the rendering routine but crashes now when I create the content beforehand and just switch to the vertex Array buffer in the paint routine.

    This is my code so far:

    template<class T>
    void View3D<T>::initializeGL()
    {
        glClearColor(0,0,0,255);
    
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_SMOOTH);
        glEnable(GL_MULTISAMPLE);
    
        m_program = new QOpenGLShaderProgram(this);
        m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vertexShader.glsl");
        m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fragmentShader.glsl");
    
        m_program->link();
        m_posAttr = m_program->attributeLocation("posAttr");
    
        GLfloat vertices[] = {
           +0.0f, +1.0f, 0.0f,
           -1.0f, -1.0f,   0.0f,
           +1.0f, -1.0f,   0.0f
        };
    
        m_vbo.create();
        m_vbo.bind();
        m_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
        m_vbo.allocate(vertices, sizeof(vertices));
    
        m_vao.create();
        m_vao.bind();
    
        glEnableVertexAttribArray(0);
        m_program->setAttributeBuffer(0,GL_FLOAT, 0, 3, 0);
    
        m_vbo.release();
        m_vao.release();
       m_program->release();
    }
    
    
    template<class T>
    void View3D<T>::paintGL()
    {
    
        this->makeCurrent();
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        m_vao.bind();
        m_program->bind();
    
        glDrawArrays(GL_TRIANGLES, 0, 3);
    
        m_program->release();
        m_vao.release();
    }
    
    
    template<class T>
    void View3D<T>::mouseDoubleClickEvent(QMouseEvent *e) {
        if(isFullScreen())
        {
            setWindowFlags(Qt::Widget);
            showNormal();
    
        } else {
            setWindowFlags(Qt::Window);
            showFullScreen();
        }
    }
    

    Forget about the template class, I need this for other reasons. As soon as I switch to fullscreen the app crashes @ glDrawArrays(GL_TRIANGLES, 0, 3); deep down in OpenGL.

    What could be wrong?



  • Hi,
    I do not use makeCurrent in paintGL. I assume it is already done by the Qt framework before it calls paintGL.

    NoneTheless if used you should also call doneCurrent() at the end.
    -Michael.



  • @procrash said in QT / OpenGL and Switch to fullscreen crashes:

    Hi,

    I'm currently trying to dig in to the details how to use QT in combination with OpenGL
    In the past I've been using glBegin and glEnd commands to draw my content. I read that this is outdated so I tried updating my code to match up with the rendering pipeline in QT.

    I tried to implement a simple example just drawing a red rectangle in an QTOpenGLWidget and provide the possibility to switch to fullscreen by double clicking on the rendering content.

    This worked in the past with glBegin and glEnd and also worked as long as I created the vertices on the fly in the rendering routine but crashes now when I create the content beforehand and just switch to the vertex Array buffer in the paint routine.

    This is my code so far:

    template<class T>
    void View3D<T>::initializeGL()
    {
        glClearColor(0,0,0,255);
    
        glEnable(GL_DEPTH_TEST);
        glShadeModel(GL_SMOOTH);
        glEnable(GL_MULTISAMPLE);
    
        m_program = new QOpenGLShaderProgram(this);
        m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vertexShader.glsl");
        m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fragmentShader.glsl");
    
        m_program->link();
        m_posAttr = m_program->attributeLocation("posAttr");
    
        GLfloat vertices[] = {
           +0.0f, +1.0f, 0.0f,
           -1.0f, -1.0f,   0.0f,
           +1.0f, -1.0f,   0.0f
        };
    
        m_vbo.create();
        m_vbo.bind();
        m_vbo.setUsagePattern(QOpenGLBuffer::StaticDraw);
        m_vbo.allocate(vertices, sizeof(vertices));
    
        m_vao.create();
        m_vao.bind();
    
        glEnableVertexAttribArray(0);
        m_program->setAttributeBuffer(0,GL_FLOAT, 0, 3, 0);
    
        m_vbo.release();
        m_vao.release();
       m_program->release();
    }
    
    
    template<class T>
    void View3D<T>::paintGL()
    {
    
        this->makeCurrent();
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        m_vao.bind();
        m_program->bind();
    
        glDrawArrays(GL_TRIANGLES, 0, 3);
    
        m_program->release();
        m_vao.release();
    }
    
    
    template<class T>
    void View3D<T>::mouseDoubleClickEvent(QMouseEvent *e) {
        if(isFullScreen())
        {
            setWindowFlags(Qt::Widget);
            showNormal();
    
        } else {
            setWindowFlags(Qt::Window);
            showFullScreen();
        }
    }
    

    Forget about the template class, I need this for other reasons. As soon as I switch to fullscreen the app crashes @ glDrawArrays(GL_TRIANGLES, 0, 3); deep down in OpenGL.

    What could be wrong?

    Found one strange issue. When I recreate the vbo everything is fine. How can I avoid that QT invalidates the vbo when it is switching to fullscreen? As far as I understood the vertex buffers should not be touched by QT during the switch.



  • Hi,
    I do not to create it every time in paintGL, but only bind (and release) it every time.
    -Michael.


Log in to reply