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

initializeQOpenGLFunctions - when do I actually use it?



  • Hey

    So I know we use it in openGLwidget initialize(), I also use it in every geometry/material/textre I make. But do I need to? Or all I need to is just extend QOpenGLFunctions ? I'm lost with that function. Where exactly should I use it?


  • Moderators

    I also use it in every geometry/material/textre I make. But do I need to?

    No.
    QOpenGLFunctions is a class that holds pointers to all the OpenGL functions. Initially they point to nothing. initializeOpenGLFunctions actually looks them up and initializes them so that you can call those functions. It should be called exactly once for one instance of QOpenGLFunctions and there's rarely a reason to have more than one instance of that class so usually it should be called only once in your app. For it to work it needs to be called with an OpenGL context made current so that's why it is usually in the OpenGL initialization function.

    To make it more explicit:
    OpenGL is provided in your system as a shared library (a dll on Windows) by either the OS itself or the graphics driver. When you want to use an OpenGL function in your app you need to first load that library and resolve an address to that function. To make it easier Qt has pointers to all the OpenGL functions closed in a class QOpenGLFunctions, but you still need to resolve those pointers. That's what the initializeOpenGLFunctions function does.
    As to how to use it afterwards - there are couple of ways. You can have a usual instance of that class in your own class e.g.

    class MyClass {
       QOpenGLFunctions funcs;
    }
    

    and use it in your classes functions like this:

    void MyClass:someFunction() {
       funcs.glEnable(GL_BLEND);
    }
    

    To avoid having to write that instance name all the time you can instead inherit your class from QOpenGLFunctions:

    class MyClass : public QOpenGLFunctions {
    }
    

    and this way you can simply use it like this:

    void MyClass:someFunction() {
       glEnable(GL_BLEND);
    }
    


  • Hmmmmmmmmmmmmmm fairly lost!

    Mainly because I spend the last 30 mins trying to debug why glGenVertexArray(1,&vao) was causing a crash, even tho I was inheriting the QOpenGLFunctions_4_5_Core on that class. Essentially the workflow was

        auto meshList = loader.Load("path");
        render[0]->make_current()
        render->scene->addMeshes(meshList);
    

    Then, as the geometry needed new materials, it created new material - since render context was active, I assumed everything was still under that context and bound it in.

    But as it turns out, if I went to create VAO with glGenVertexArray(1,&vao) I got an error, Essentially I had to go on to that class and initializeOpenGLFunctions(), not only that, since that class needed texture which was another class, I had to go and do the same thing there too or else that buffer was crashing there. I'm either doing something very very very wrong, or I can't read, because you said :

    @Chris-Kawa said in initializeQOpenGLFunctions - when do I actually use it?:

    I also use it in every geometry/material/textre I make. But do I need to?

    No.

    So what did I mess up ? :- (


  • Moderators

    I said you need exactly one call to that function for each QOpenGLFunctions instance. If you have an instance of it in each of your classes then you need to call it in each, yes, but having it like that is kinda wasteful (you're duplicating a lot of function pointers). Consider having only one QOpenGLFunctions instance and share it between your various classes.



  • Ohhhh now I get it! So I have to make it like static or some "globally" accessible stuff. Like each object should have ptr to it. AHh got it... thanks! now I get it :- )

    so if my shader needs it then I can do something auto *funcs = render->openGLFunction() or something like that and then do func->glBindBuffer() etc etc?


Log in to reply