QOpenGLFunctions Class

The QOpenGLFunctions class provides cross-platform access to the OpenGL ES 2.0 API. More...

Header: #include <QOpenGLFunctions>
qmake: QT += gui
Since: Qt 5.0
Inherited By:

QOpenGLExtraFunctions

This class was introduced in Qt 5.0.

Public Types

enum OpenGLFeature { Multitexture, Shaders, Buffers, Framebuffers, BlendColor, …, MultipleRenderTargets }

Detailed Description

OpenGL ES 2.0 defines a subset of the OpenGL specification that is common across many desktop and embedded OpenGL implementations. However, it can be difficult to use the functions from that subset because they need to be resolved manually on desktop systems.

QOpenGLFunctions provides a guaranteed API that is available on all OpenGL systems and takes care of function resolution on systems that need it. The recommended way to use QOpenGLFunctions is by direct inheritance:

     class MyGLWindow : public QWindow, protected QOpenGLFunctions
     {
         Q_OBJECT
     public:
         explicit MyGLWindow(QScreen *screen = nullptr);

     protected:
         void initializeGL();
         void paintGL();

         QOpenGLContext *m_context;
     };

     MyGLWindow(QScreen *screen)
       : QWindow(screen), QOpenGLWidget(parent)
     {
         setSurfaceType(OpenGLSurface);
         create();

         // Create an OpenGL context
         m_context = new QOpenGLContext;
         m_context->create();

         // Setup scene and render it
         initializeGL();
         paintGL();
     }

     void MyGLWindow::initializeGL()
     {
         m_context->makeCurrent(this);
         initializeOpenGLFunctions();
     }

The paintGL() function can then use any of the OpenGL ES 2.0 functions without explicit resolution, such as glActiveTexture() in the following example:

     void MyGLWindow::paintGL()
     {
         m_context->makeCurrent(this);
         glActiveTexture(GL_TEXTURE1);
         glBindTexture(GL_TEXTURE_2D, textureId);
         ...
         m_context->swapBuffers(this);
         m_context->doneCurrent();
     }

QOpenGLFunctions can also be used directly for ad-hoc invocation of OpenGL ES 2.0 functions on all platforms:

     QOpenGLFunctions glFuncs(QOpenGLContext::currentContext());
     glFuncs.glActiveTexture(GL_TEXTURE1);

An alternative approach is to query the context's associated QOpenGLFunctions instance. This is somewhat faster than the previous approach due to avoiding the creation of a new instance, but the difference is fairly small since the internal data structures are shared, and function resolving happens only once for a given context, regardless of the number of QOpenGLFunctions instances initialized for it.

     QOpenGLFunctions *glFuncs = QOpenGLContext::currentContext()->functions();
     glFuncs->glActiveTexture(GL_TEXTURE1);

QOpenGLFunctions provides wrappers for all OpenGL ES 2.0 functions, including the common subset of OpenGL 1.x and ES 2.0. While such functions, for example glClear() or glDrawArrays(), can be called also directly, as long as the application links to the platform-specific OpenGL library, calling them via QOpenGLFunctions enables the possibility of dynamically loading the OpenGL implementation.

The hasOpenGLFeature() and openGLFeatures() functions can be used to determine if the OpenGL implementation has a major OpenGL ES 2.0 feature. For example, the following checks if non power of two textures are available:

     QOpenGLFunctions funcs(QOpenGLContext::currentContext());
     bool npot = funcs.hasOpenGLFeature(QOpenGLFunctions::NPOTTextures);

See also QOpenGLContext and QSurfaceFormat.

Member Type Documentation

enum QOpenGLFunctions::OpenGLFeature

This enum defines OpenGL and OpenGL ES features whose presence may depend on the implementation.

ConstantValueDescription
QOpenGLFunctions::Multitexture0x0001glActiveTexture() function is available.
QOpenGLFunctions::Shaders0x0002Shader functions are available.
QOpenGLFunctions::Buffers0x0004Vertex and index buffer functions are available.
QOpenGLFunctions::Framebuffers0x0008Framebuffer object functions are available.
QOpenGLFunctions::BlendColor0x0010glBlendColor() is available.
QOpenGLFunctions::BlendEquation0x0020glBlendEquation() is available.
QOpenGLFunctions::BlendEquationSeparate0x0040glBlendEquationSeparate() is available.
QOpenGLFunctions::BlendEquationAdvanced0x20000Advanced blend equations are available.
QOpenGLFunctions::BlendFuncSeparate0x0080glBlendFuncSeparate() is available.
QOpenGLFunctions::BlendSubtract0x0100Blend subtract mode is available.
QOpenGLFunctions::CompressedTextures0x0200Compressed texture functions are available.
QOpenGLFunctions::Multisample0x0400glSampleCoverage() function is available.
QOpenGLFunctions::StencilSeparate0x0800Separate stencil functions are available.
QOpenGLFunctions::NPOTTextures0x1000Non power of two textures are available.
QOpenGLFunctions::NPOTTextureRepeat0x2000Non power of two textures can use GL_REPEAT as wrap parameter.
QOpenGLFunctions::FixedFunctionPipeline0x4000The fixed function pipeline is available.
QOpenGLFunctions::TextureRGFormats0x8000The GL_RED and GL_RG texture formats are available.
QOpenGLFunctions::MultipleRenderTargets0x10000Multiple color attachments to framebuffer objects are available.