Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct usages of QOpenGLFunctions

Tags:

c++

opengl

qt5

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

  • It wraps OpenGL for Desktop and OpenGL ES, making sure I am using the OpenGL API in a 'portable' way.
  • I Don't have to worry about including OpenGL headers, Qt does it for me.

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 :

  • If my class was expected to inherit from an other class before, I have to make multi-inheritance. Something I am not kind of. Even when such cases are safe anyways, it's aestethics...
  • Every glXXXX wrapping classes are non-const. I would force all methods using OpenGL to be non-const. That does not make a lot of sens. Yes OpenGLFunctions class can legitimately be non-const when I do glClear(...), but why my method DrawableShape::render(...) would be ?

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...

like image 468
Oragon Efreet Avatar asked Jun 09 '14 14:06

Oragon Efreet


1 Answers

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){ ... } 

};
like image 59
Pierluigi Avatar answered Oct 17 '22 01:10

Pierluigi