Oragon Efreet
Oragon Efreet

Reputation: 1134

Correct usages of QOpenGLFunctions

I a currently working on using Qt5 gui module to access to OpenGL functions. Then I discover QOpenGLFunctions which is useful because :

Yet I have doubts about a correct way to use it. Following lines only list the three ways I know about using this class. My question is : Is there a good way to use QOpenGLFunctions ?

Inheriting from QOpenGLFunctions

Qt official documentation says 'inherit you class from QOpenGLFunctions and use glXXXX classes like before. But I don't like this way as :

And about inherinting from QOpenGLFunctions. Its constructor may accept an argument : the current OpenGL context. This parameter seems very important to me, but no Qt documentation calls this constructor. Instead they let the compiler to choose the no-parameter constructor.

Having QOpenGLFunctions as member

An other idea should be to have an instance of QOpenGLFunctions as member of any class calling glXXXXX functions, or at least a reference to one instance, and call every OpenGL functions from this instance.

Passing QOpenGLFunctions as parameter

For each function using OpenGL, the caller send QOpenGLFunctions. This way :

void renderRectangle(QOpenGLFunctions& opengl) const;

But how could I be sure this function will need it and this one won't ? I mean the source code will get bigger by the time and I fear the risk of seeing every methods of the classes receiving this parameter...

Upvotes: 10

Views: 5856

Answers (1)

Pierluigi
Pierluigi

Reputation: 2294

Following the same principle of other object oriented wrappers libraries you might consider a small variation of your third options.

Define a class representing your current opengl context that also extends QOpenGLFunctions

class GL : public QOpenGLFunctions{

  QGLContext& context;

  GL(QGLContext& c) : glContext(c){ ... }

};

The rendering thread will initialize an instance of GL providing its current context and pass it to all rendering instances that need to perform opengl operations. In this way you are also sure that you are not mixing multiple contexts when initializing and using opengl structures and buffers.

class Visualizer{

 void glInit(GL& gl){ ... } 

 void glPaintOpaque(GL& gl){ ... } 

 void glPaintTranslucent(GL& gl){ ... } 

};

Upvotes: 1

Related Questions