Display 1 QOpenGLWidget in 2+ places?



  • Hey

    I'm tinkering with a view that would allow user to display 1 qopenGLwidget in multiple "places" in the app.

    Right now one way of doing it is to have as many as I want QLabels with setQPixmap(glWidget->grabViewport()) command to set them just as textures. But thats is very inneficient. I was wondering if any1 has any ideas as to how I could display the openGLwidget directly in different places? I guess it would be cool to have a "scene" based system where user can have 1 scene and add it to multiple views for displaying... Maybe there is a way somehow to do it with glWidget?

    TIA


  • Lifetime Qt Champion

    Hi,

    IIRC, you can share the context between several widgets.



  • Render your scene to a QOpenGLFrameBufferObject, then use a QOpenGLTextureBlitter to display the resulting image in two different QOpenGLWidgets.

    Everything will stay on the GPU, so it's faster than trying to copy the contents to a QPixmap and then copy that onto a QLabel. Most of the time, just rendering the scene twice for the two QOpenGLWidgets is the right way to go. Don't dismiss the simplest solution until you are really really sure that it's wrong. But if you are really sure that you want to render the scene once and display it in two widgets, rendering offscreen to the FBO is what you want.

    And no, displaying 1 widget in 2 places isn't what you want. A widget "is" the way you represent a thing on screen in a particular place. Trying to fight that definition will just drive you crazy chasing your tail as if you were trying to mathematically prove that 2==1. If you want what looks like two widgets in two different places on screen, then use two widgets.



  • @wrosecrans said in Display 1 QOpenGLWidget in 2+ places?:

    Render your scene to a QOpenGLFrameBufferObject, then use a QOpenGLTextureBlitter to display the resulting image in two different QOpenGLWidgets.

    Hey

    This seems to be the right thing to do. I spend few hours on googling now but I was unable to find much more info on it. Few mentions on stackoverflow/etc but nothing helpful...

    Can you maybe direct me in some place where I can read more on it/see examples?

    I'm a tad lost.

    I used QOpenGLFrameBufferObject in past where I draw off screen and display stuff as Pixmap on label. But I have no idea how to merge it to the QOPenGLWIdget::paint() function with that buffer and then share it to another QOpenGLWidsget and display it there again...

    Any help would be great!

    Regards
    Dariusz



  • @Dariusz said in Display 1 QOpenGLWidget in 2+ places?:

    I used QOpenGLFrameBufferObject in past where I draw off screen and display stuff as Pixmap on label. But I have no idea how to merge it to the QOPenGLWIdget::paint() function with that buffer and then share it to another QOpenGLWidsget and display it there again...

    That's what the textureblitter is for. It's the unsung hero. In-theory, drawing a texture to the screen is super simple, but writing it really robustly in a way that works right with both classic fixed function OpenGL and also modern OpenGL in a surprising pain in the rump.

    https://doc.qt.io/qt-5/qopengltextureblitter.html

    The FBO has a texture() method:
    http://doc.qt.io/qt-5/qopenglframebufferobject.html#texture

    The texture blitter taxes a texture ID in the blit method. Just use the blit() method when you are drawing your QOpenGLWidget, and voila, the contents of the FBO get drawn to your widget. Figuring out exactly when and how to draw into the FBO is up to you. As long as you can manage that, drawing the result on screen is the easy part using that blitter class.



  • Hey

    Thanks so much for your help!

    I spend a while longer on the topic, learning more about the buffer&widgetGL and now blit texture. I got as far as this project > https://sourceforge.net/projects/opengl-blit/files/

    But I cant figure out why the blit does not work. I can see that the frameObject has proper texture/render. But when I try to blit it I got nothing.

    Any help would be great!

    TIA.


Log in to reply