QOpenGLFunctions draw in the wrong QOpenGLWidget



  • Hi all,

    Thanks for clicking in my post first. I am recently using QOpenGLfuncitons to render some graphics images as my project needs. Basically, right now I have ONE window with an QOpenGLWidget(widget1) child and ANOTHER QOpenGLWidget(widget2) as standalone window (note they are different). They are rendering different graphics content.

    The work flow is a timer send timeOut() signal to two qopenglwidget's update() slots to render image according to the data inside the two qopenglwidgets. On the other hand, I update the data in widget2 (some textures loading by cv::Mat into texture_) and use glBindTexture(TEXTURE_2D, texture_) & glTexImage2D(...) to update the texture (rebind in my paintGL() function). However, the texture is rendering inside widget1 rather than widget2.

    My guess is they are using the same context to render the graphics. Right now I just use default this->context() for each of my widgets. But I haven't tried creating new instance of context yet since it may cost me some time. Therefore I want to post this first to see if anyone could help me out and I would try my guess at the same time.

    Platform: Ubuntu 16.04, Qt 5.6, and the QSurfaceFormat I use for setting is

    QSurfaceFormat format;
        format.setRenderableType(QSurfaceFormat::OpenGL);
        format.setProfile(QSurfaceFormat::CoreProfile);
        format.setVersion(3,3);
    

    for both widgets


  • Lifetime Qt Champion

    Hi,

    You should post the code you are using that is related to OpenGL.



  • Hi thanks for responding. Let me write down the way I am using this. I am also new to OpenGL, but I think it's the right way to bind my texture_ (glBindTexture(TEXTURE_2D, texture_) in paintGL)

    In mywidget2, I want to render an image as texture on the screen. However, the texture_ I want to render on mywidget2 is bound to mywidget1. Which I believe, these two widgets are sharing same QOpenGLContext. Am I correct? If it was, how could I fix that?

    class MyWidget1: QOpenGLWidget, QOpengGLFuncitons
    {
        Q_OBJECT
    public:
        MyWidget1(QWidget *parent): QOpenGLWidget(parent) {
            QSurfaceFormat format;
            format.setRenderableType(QSurfaceFormat::OpenGL);
            format.setProfile(QSurfaceFormat::CoreProfile);
            format.setVersion(3,3);
            this->setFormat(format);
        }
    
        void updateData1(Data data){
            data_ = data;
        }
    
    protected:
        void initializeGL(){
             initializeOpenGLFunctions();
        }
    
        void paintGL(){
            // paint in mywidget1 with data_
        }
    
        void resizeGL(){}
    
    priavte:
        Data data_;
    }
    
    class MyWidget2: QOpenGLWidget, QOpengGLFuncitons
    {
        Q_OBJECT
    public:
        MyWidget2(QWidget *parent): QOpenGLWidget(parent) {
            QSurfaceFormat format;
            format.setRenderableType(QSurfaceFormat::OpenGL);
            format.setProfile(QSurfaceFormat::CoreProfile);
            format.setVersion(3,3);
            this->setFormat(format);
        }
    
        void updateData2(Data data){
            //data_ = data;
    
            cv::Mat mat_in(img.height(), img.width(), CV_8UC1, data);
            cv::cvtColor(mat_in, mat_, CV_BayerBG2RGB);
    
            glBindTexture(GL_TEXTURE_2D, texture_);
            glTexImage2D(GL_TEXTURE_2D, 0, 3, mat_.cols, mat_.rows, 0, GL_RGB,
                         GL_UNSIGNED_BYTE, mat_.data);
            glGenerateMipmap(GL_TEXTURE_2D);
        }
    
    protected:
        void initializeGL(){
            initializeOpenGLFunctions();
            // other VAO, VBO & EBOs
    
            // set up texture_
            glGenTextures(1, &texture_);
            glBindTexture(GL_TEXTURE_2D, texture_);
    
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        }
    
        void paintGL(){
            // paint in mywidget2 with data_
    
           // bind updated texture_
           glBindTexture(GL_TEXTURE_2D, texture_);
           // draw with glDrawElements()...
        }
    
        void resizeGL(){}
    
    private:
        Data data_;
        unsigned int texture_;
    }
    
    class Mainwindow {
    public:
        Mainwindow(){
            connect(timer_, SIGNAL(timeOut()), mywidget1, SLOT(update());
            connect(timer_, SIGNAL(timeOut()), mywidget2, SLOT(update());
            connect(timer_update_, SIGNAL(timeOut()), this, SLOT(updateData());
            // other initialization    
        }
    
        void startTimer(){
            // this is not my implementation, I just write two timer signal to simulate what happened on my real code
             timer_.start(100);
             timer_update_.start(50);
        }
    public slots:
        void updateData(){
            // load data
            mywidget1->updateData1(data1);
            mywidget2->updateData2(data2);
        }
    
    private:
        MyWidget1 *mywidget1;
        MyWidget2 *mywidget2;
        QTimer timer_;
        QTimer timer_update_;
    }
    

    Thanks for your help! I really appreciate. If this code is still confusing, please tell me.


  • Lifetime Qt Champion

    Did you try using QOpenGLTexture ?



  • @SGaist Yea, I did, but I found QImage(unsigned char *data....) is slow compared with openCV. I am trying to implement something like streaming video in mywidget2. Which is to say, in mywidget2, there is only a rectangle with my texture_ on it. And the data is read from producer-consumer pattern queue. Thats why the reading speed could be the bottleneck here.


  • Lifetime Qt Champion

    Streaming video ?

    Where does the video come from ?



  • It's basically a sequence of images, but I want to play it back as "video".


Log in to reply
 

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