Problem using glTexSubImage2D with QOpenGLWidget



  • Hello,
    I'm trying to Display a texture within a QOpenGlWidget in a QMainWindow.
    For that I made a class which derived from QopenGlWidget and my corresponding widget is promoted to use that class.

    class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    ...
    }
    
    

    My plan is to first initlialize the texture once in the initializeGL() method, then use the texture id in the paintGL() method and only update it with glTexSubImage2D().

    GLWidget::initializeGL()
    {
    ...
      glTexImage2D(...);
    ...
    }
    GLWidget::paintGL()
    {
    ...
       glTexSubImage2D(...);
    ...
    }
    

    This works ONLY if I use glTexImage() right before glTexSubImage2D() in the paintGL() method . Otherwise the texture won't be drawn.
    I think there is a problem with the drawing context. But I cannot figure it out.
    Any idea is appreciated.
    Thank you.

    EDIT:
    I set the new texture from the outside with a the method void setTexture(...);
    This method also calls update() to repaint the widget.
    Is it possible that calling this method from an outside thread causes the texture to render in another context?

    EDIT 2:
    I tried to set a shared context in the painGl() method. I used the context which was used in the initializeGL() method. Also, I tried do make the context from initializeGL() the current context while drawing in painGL(). Again same result. Here are my OpenGL functions for creating the texture:

    //Works when used in paintGl().
    glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, m_cvImage.cols, m_cvImage.rows,0, GL_BGR, GL_UNSIGNED_BYTE, m_cvImage.data);
    
    //Not working when used in paintGl() if glTexImage2D is used to create the texture in initializeGl().
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_cvImage.cols, m_cvImage.rows, GL_BGR,, GL_UNSIGNED_BYTE, m_cvImage.data);
    
    //Also working in paintGL()
    glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, m_cvImage.cols, m_cvImage.rows,0, GL_BGR, GL_UNSIGNED_BYTE, NULL);
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_cvImage.cols, m_cvImage.rows, GL_BGR, GL_UNSIGNED_BYTE, m_cvImage.data);
    

    When I try to call makeCurrent() in the paintGL() method (even if it should be called automatically right before) I get an Assertion:

    ASSERT: "context" in file opengl\qopenglfunctions.cpp, line 209
    

    The same applies to the doneCurrent() method (just for testing).
    This tells me that there is no available context for the paintGL() method.
    Yet a check with:

    if (context() == 0)...
    

    Seems to be fine.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.