Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. initializeQOpenGLFunctions - when do I actually use it?

initializeQOpenGLFunctions - when do I actually use it?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dariusz
    wrote on 20 Jan 2019, 17:43 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 20 Jan 2019, 18:13 last edited by
      #2

      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);
      }
      
      1 Reply Last reply
      4
      • D Offline
        D Offline
        Dariusz
        wrote on 20 Jan 2019, 18:54 last edited by
        #3

        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);
        

        as my main context(as I can have more than 1 render - I take that is the correct worflow?), and then I added the geometry to that render sceneManager()

        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 ? :- (

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 20 Jan 2019, 19:11 last edited by
          #4

          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.

          1 Reply Last reply
          3
          • D Offline
            D Offline
            Dariusz
            wrote on 21 Jan 2019, 19:26 last edited by Dariusz
            #5

            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?

            1 Reply Last reply
            0

            1/5

            20 Jan 2019, 17:43

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved