Jay
Jay

Reputation: 14471

How do you use QAbstractOpenGLFunctions in c++?

I’m new to OpenGL so this might be a stupid question. It seems like I'm missing something obvious. I can’t see how the new OpenGL classes are supposed to be useful.

There are classes for each version and profile of OpenGL. These classes have explicit methods for all the OpenGL calls supported by each version. Here's a partial list:

I assume it would be something like the following:

So how do I write code using this class? I can't predict which object I will get at run time without knowing explicitly what hardware it will run on. The base class contains no methods since they're different for every derived class. I could write a giant switch but that seems a step backward from using QOpenGLFunctions or just getting the function addresses manually.

Upvotes: 2

Views: 732

Answers (1)

ksimons
ksimons

Reputation: 3826

The point of why these classes are useful is that the previous QOpenGLFunctions class only exposed OpenGL/ES 2.0 functionality. Now they've exposed the full functionality from many versions of OpenGL allowing you to take advantage of the features offered only in those versions.

Of course most developers don't choose a GL version at runtime for most applications. They target a particular version and for that the Qt classes will work very nicely.

If what you're searching for is a way to call the "common" methods between the various QOpenGLFunctions_* classes without knowing which version of OpenGL you're using (while still giving yourself the opportunity to take advantage of specific features of "higher" versions), why not use templating?

For example:

template <class T>
class SomeOpenGLRendering {
public:
    SomeOpenGLRendering(T *openglFunctions) : openglFunctions(openglFunctions) {
        openglFunctions->initializeOpenGLFunctions();
    }

    void renderSomething() {
        openglFunctions->glClear(GL_COLOR_BUFFER_BIT);
    }

private:
    T *openglFunctions;
};

And then based on whatever criteria you'd like (hardware detection as you said, for example), create the proper version as needed:

SomeOpenGLRendering<QOpenGLFunctions_3_2_Core> r(new QOpenGLFunctions_3_2_Core());
r.renderSomething();

Upvotes: 3

Related Questions