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: |
This class was introduced in Qt 5.0.
Public Types
enum | OpenGLFeature { Multitexture, Shaders, Buffers, Framebuffers, ..., MultipleRenderTargets } |
flags | OpenGLFeatures |
Public Functions
QOpenGLFunctions(QOpenGLContext *context) | |
QOpenGLFunctions() | |
bool | hasOpenGLFeature(QOpenGLFunctions::OpenGLFeature feature) const |
void | initializeOpenGLFunctions() |
QOpenGLFunctions::OpenGLFeatures | openGLFeatures() const |
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: MyGLWindow(QScreen *screen = 0); 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
flags QOpenGLFunctions::OpenGLFeatures
This enum defines OpenGL and OpenGL ES features whose presence may depend on the implementation.
Constant | Value | Description |
---|---|---|
QOpenGLFunctions::Multitexture | 0x0001 | glActiveTexture() function is available. |
QOpenGLFunctions::Shaders | 0x0002 | Shader functions are available. |
QOpenGLFunctions::Buffers | 0x0004 | Vertex and index buffer functions are available. |
QOpenGLFunctions::Framebuffers | 0x0008 | Framebuffer object functions are available. |
QOpenGLFunctions::BlendColor | 0x0010 | glBlendColor() is available. |
QOpenGLFunctions::BlendEquation | 0x0020 | glBlendEquation() is available. |
QOpenGLFunctions::BlendEquationSeparate | 0x0040 | glBlendEquationSeparate() is available. |
QOpenGLFunctions::BlendEquationAdvanced | 0x20000 | Advanced blend equations are available. |
QOpenGLFunctions::BlendFuncSeparate | 0x0080 | glBlendFuncSeparate() is available. |
QOpenGLFunctions::BlendSubtract | 0x0100 | Blend subtract mode is available. |
QOpenGLFunctions::CompressedTextures | 0x0200 | Compressed texture functions are available. |
QOpenGLFunctions::Multisample | 0x0400 | glSampleCoverage() function is available. |
QOpenGLFunctions::StencilSeparate | 0x0800 | Separate stencil functions are available. |
QOpenGLFunctions::NPOTTextures | 0x1000 | Non power of two textures are available. |
QOpenGLFunctions::NPOTTextureRepeat | 0x2000 | Non power of two textures can use GL_REPEAT as wrap parameter. |
QOpenGLFunctions::FixedFunctionPipeline | 0x4000 | The fixed function pipeline is available. |
QOpenGLFunctions::TextureRGFormats | 0x8000 | The GL_RED and GL_RG texture formats are available. |
QOpenGLFunctions::MultipleRenderTargets | 0x10000 | Multiple color attachments to framebuffer objects are available. |
The OpenGLFeatures type is a typedef for QFlags<OpenGLFeature>. It stores an OR combination of OpenGLFeature values.
Member Function Documentation
QOpenGLFunctions::QOpenGLFunctions(QOpenGLContext *context)
Constructs a function resolver for context. If context is nullptr
, then the resolver will be created for the current QOpenGLContext.
The context or another context in the group must be current.
An object constructed in this way can only be used with context and other contexts that share with it. Use initializeOpenGLFunctions() to change the object's context association.
See also initializeOpenGLFunctions().
QOpenGLFunctions::QOpenGLFunctions()
Constructs a default function resolver. The resolver cannot be used until initializeOpenGLFunctions() is called to specify the context.
See also initializeOpenGLFunctions().
bool QOpenGLFunctions::hasOpenGLFeature(QOpenGLFunctions::OpenGLFeature feature) const
Returns true
if feature is present on this system's OpenGL implementation; false otherwise.
It is assumed that the QOpenGLContext associated with this function resolver is current.
See also openGLFeatures().
void QOpenGLFunctions::initializeOpenGLFunctions()
Initializes OpenGL function resolution for the current context.
After calling this function, the QOpenGLFunctions object can only be used with the current context and other contexts that share with it. Call initializeOpenGLFunctions() again to change the object's context association.
QOpenGLFunctions::OpenGLFeatures QOpenGLFunctions::openGLFeatures() const
Returns the set of features that are present on this system's OpenGL implementation.
It is assumed that the QOpenGLContext associated with this function resolver is current.
See also hasOpenGLFeature().