Single OpenGL Context across several classes



  • Hello forum,

    I need to have access to opengl functions across several classes with only one opengl context. In that case do I need to initialize the function :

    initializeOpenGLFunctions() 
    

    in each and every classes that need to call to the OpenGL functions ?

    Do I have make a singleton instance of the QOpenGLContext ? In that case how to do it while rendering opengl under qml scene ?

    Thanks



  • Hi forum,

    No reply so far. I believe the my question was not complete enough. Let me try again. I am trying to port an existing OpenGL application to Qt5 with qml and OpenGL as underlay. The application was initially developed with OpenGL 4.3 core profile and since the qml rendering engine still uses OpenGL 2, I had to use the compatibility profile to work with OpenGL 4.3.

    To access the profile specific OpenGL functions to class object I had to subclassed some of the classes as follows:

    class GLSLShader : protected QOpenGLFunctions_4_3_Compatibility
    {
    public:
       ...............
    };
    
    class Hilbert2DScene : public AbstractScene, protected QOpenGLFunctions_4_3_Compatibility
    {
        Q_OBJECT
        ....................
    
    };
    

    As you can see that both of the classes subclassed the QOpenGLFunctions_4_3_Compatibility class. I am not sure if this is the right way to do it, because each of the classes has to call the

    initializeOpenGLFunctions()
    

    before calling any of the profile specific OpenGL functions even after I am assuming the fact that I am working with one and only OpenGL context. This is just an assumption and I want t be sure about it. I need your help here. I am configuring the profile as follows:

    Hilbert2DSceneItem::Hilbert2DSceneItem()
        : mHilbert2DScene(0)
    {
        //initialize the hilbert curve scene class
        mHilbert2DScene = new Hilbert2DScene();
    
        connect(this,SIGNAL(windowChanged(QQuickWindow*)),this,SLOT(handleWindowChanged(QQuickWindow*)),Qt::DirectConnection);
    }
    
    void Hilbert2DSceneItem::handleWindowChanged(QQuickWindow *win)
    {
        if(win)
        {
    
            if(!win->isPersistentOpenGLContext())
                win->setPersistentOpenGLContext(true);
    
            QSurfaceFormat f = win->format();
    
            //we are using the OpenGL version 4.3 here
            //which is not mandatory
            f.setMajorVersion(4);
            f.setMinorVersion(3);
            f.setSamples(16);
            f.setStencilBufferSize(8);
            f.setProfile(QSurfaceFormat::CompatibilityProfile);
    
            win->setFormat(f);
    
            //make sure to disable clear before rendering if beforeRendering is used
            win->setClearBeforeRendering(false);
    
    
            connect(win,SIGNAL(beforeSynchronizing()),this,SLOT(sync()),Qt::DirectConnection);
            connect(win,SIGNAL(sceneGraphInvalidated()),this,SLOT(cleanup()),Qt::DirectConnection);
            /*
             * the following signal is used to draw custom opengl content under the qt quick scene
             * The signal is emitted at the start of every frame before the scene graph starts rendering,
             * thus any OpenGL draw calls that are made as a response to this signal, will stack under the
             * Qt items.
             * */
            connect(win,SIGNAL(beforeRendering()),mHilbert2DScene,SLOT(paint()),Qt::DirectConnection);
        }
    }
    
    

    There is where I would like to ask if I have to create an singleton QOpenGLContext and construct the QOpenGLFunctions from the singleton OpenGLContext whenever necessary across several class definitions.

    Some insights are eagerly requested.

    Thanks


Log in to reply
 

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