QOpenGLContext 类

QOpenGLContext class represents a native OpenGL context, enabling OpenGL rendering on a QSurface . 更多...

头: #include <QOpenGLContext>
qmake: QT += gui
Since: Qt 5.0
继承: QObject

公共类型

enum OpenGLModuleType { LibGL, LibGLES }

公共函数

QOpenGLContext (QObject * parent = nullptr)
virtual ~QOpenGLContext ()
bool create ()
GLuint defaultFramebufferObject () const
void doneCurrent ()
QSet<QByteArray> extensions () const
QOpenGLExtraFunctions * extraFunctions () const
QSurfaceFormat format () const
QOpenGLFunctions * 函数 () const
QFunctionPointer getProcAddress (const QByteArray & procName ) const
QFunctionPointer getProcAddress (const char * procName ) const
bool hasExtension (const QByteArray & extension ) const
bool isOpenGLES () const
bool isValid () const
bool makeCurrent (QSurface * surface )
QVariant nativeHandle () const
QScreen * screen () const
void setFormat (const QSurfaceFormat & format )
void setNativeHandle (const QVariant & handle )
void setScreen (QScreen * screen )
void setShareContext (QOpenGLContext * shareContext )
QOpenGLContext * shareContext () const
QOpenGLContextGroup * shareGroup () const
QSurface * surface () const
void swapBuffers (QSurface * surface )
QAbstractOpenGLFunctions * versionFunctions (const QOpenGLVersionProfile & versionProfile = QOpenGLVersionProfile()) const
TYPE * versionFunctions () const

信号

void aboutToBeDestroyed ()

静态公共成员

bool areSharing (QOpenGLContext * first , QOpenGLContext * second )
QOpenGLContext * currentContext ()
QOpenGLContext * globalShareContext ()
void * openGLModuleHandle ()
QOpenGLContext::OpenGLModuleType openGLModuleType ()
const QMetaObject staticMetaObject
bool supportsThreadedOpenGL ()

额外继承成员

详细描述

QOpenGLContext class represents a native OpenGL context, enabling OpenGL rendering on a QSurface .

QOpenGLContext represents the OpenGL state of an underlying OpenGL context. To set up a context, set its screen and format such that they match those of the surface or surfaces with which the context is meant to be used, if necessary make it share resources with other contexts with setShareContext (), and finally call create (). Use the return value or isValid () to check if the context was successfully initialized.

A context can be made current against a given surface by calling makeCurrent (). When OpenGL rendering is done, call swapBuffers () to swap the front and back buffers of the surface, so that the newly rendered content becomes visible. To be able to support certain platforms, QOpenGLContext requires that you call makeCurrent () again before starting rendering a new frame, after calling swapBuffers ().

If the context is temporarily not needed, such as when the application is not rendering, it can be useful to delete it in order to free resources. You can connect to the aboutToBeDestroyed () signal to clean up any resources that have been allocated with different ownership from the QOpenGLContext 本身。

一旦 QOpenGLContext has been made current, you can render to it in a platform independent way by using Qt's OpenGL enablers such as QOpenGLFunctions , QOpenGLBuffer , QOpenGLShaderProgram ,和 QOpenGLFramebufferObject . It is also possible to use the platform's OpenGL API directly, without using the Qt enablers, although potentially at the cost of portability. The latter is necessary when wanting to use OpenGL 1.x or OpenGL ES 1.x.

For more information about the OpenGL API, refer to the official OpenGL documentation .

For an example of how to use QOpenGLContext see the OpenGL Window 范例。

线程倾向性

QOpenGLContext can be moved to a different thread with moveToThread (). Do not call makeCurrent () from a different thread than the one to which the QOpenGLContext object belongs. A context can only be current in one thread and against one surface at a time, and a thread only has one context current at a time.

上下文资源共享

Resources such as textures and vertex buffer objects can be shared between contexts. Use setShareContext () before calling create () to specify that the contexts should share these resources. QOpenGLContext internally keeps track of a QOpenGLContextGroup object which can be accessed with shareGroup (), and which can be used to find all the contexts in a given share group. A share group consists of all contexts that have been successfully initialized and are sharing with an existing context in the share group. A non-sharing context has a share group consisting of a single context.

默认帧缓冲

On certain platforms, a framebuffer other than 0 might be the default frame buffer depending on the current surface. Instead of calling glBindFramebuffer(0), it is recommended that you use glBindFramebuffer(ctx-> defaultFramebufferObject ()), to ensure that your application is portable between different platforms. However, if you use QOpenGLFunctions::glBindFramebuffer (), this is done automatically for you.

另请参阅 QOpenGLFunctions , QOpenGLBuffer , QOpenGLShaderProgram ,和 QOpenGLFramebufferObject .

成员类型文档编制

enum QOpenGLContext:: OpenGLModuleType

此枚举定义底层 OpenGL 实现的类型。

常量 描述
QOpenGLContext::LibGL 0 OpenGL
QOpenGLContext::LibGLES 1 OpenGL ES 2.0 或更高版本

该枚举在 Qt 5.3 引入或被修改。

成员函数文档编制

QOpenGLContext:: QOpenGLContext ( QObject * parent = nullptr)

Creates a new OpenGL context instance with parent object parent .

Before it can be used you need to set the proper format and call create ().

另请参阅 create () 和 makeCurrent ().

[virtual] QOpenGLContext:: ~QOpenGLContext ()

销毁 QOpenGLContext 对象。

If this is the current context for the thread, doneCurrent () is also called.

[signal] void QOpenGLContext:: aboutToBeDestroyed ()

This signal is emitted before the underlying native OpenGL context is destroyed, such that users may clean up OpenGL resources that might otherwise be left dangling in the case of shared OpenGL contexts.

If you wish to make the context current in order to do clean-up, make sure to only connect to the signal using a direct connection.

[static] bool QOpenGLContext:: areSharing ( QOpenGLContext * first , QOpenGLContext * second )

返回 true first and second contexts are sharing OpenGL resources.

bool QOpenGLContext:: create ()

Attempts to create the OpenGL context with the current configuration.

The current configuration includes the format, the share context, and the screen.

If the OpenGL implementation on your system does not support the requested version of OpenGL context, then QOpenGLContext will try to create the closest matching version. The actual created context properties can be queried using the QSurfaceFormat returned by the format () function. For example, if you request a context that supports OpenGL 4.3 Core profile but the driver and/or hardware only supports version 3.2 Core profile contexts then you will get a 3.2 Core profile context.

返回 true if the native context was successfully created and is ready to be used with makeCurrent (), swapBuffers (),等。

注意: If the context already exists, this function destroys the existing context first, and then creates a new one.

另请参阅 makeCurrent () 和 format ().

[static] QOpenGLContext *QOpenGLContext:: currentContext ()

Returns the last context which called makeCurrent in the current thread, or 0, if no context is current.

GLuint QOpenGLContext:: defaultFramebufferObject () const

Call this to get the default framebuffer object for the current surface.

On some platforms (for instance, iOS) the default framebuffer object depends on the surface being rendered to, and might be different from 0. Thus, instead of calling glBindFramebuffer(0), you should call glBindFramebuffer(ctx->defaultFramebufferObject()) if you want your application to work across different Qt platforms.

If you use the glBindFramebuffer() in QOpenGLFunctions you do not have to worry about this, as it automatically binds the current context's defaultFramebufferObject() when 0 is passed.

注意: Widgets that render via framebuffer objects, like QOpenGLWidget and QQuickWidget , will override the value returned from this function when painting is active, because at that time the correct "default" framebuffer is the widget's associated backing framebuffer, not the platform-specific one belonging to the top-level window's surface. This ensures the expected behavior for this function and other classes relying on it (for example, QOpenGLFramebufferObject::bindDefault () 或 QOpenGLFramebufferObject::release ()).

另请参阅 QOpenGLFramebufferObject .

void QOpenGLContext:: doneCurrent ()

Convenience function for calling makeCurrent with a 0 surface.

This results in no context being current in the current thread.

另请参阅 makeCurrent () 和 currentContext ().

QSet < QByteArray > QOpenGLContext:: extensions () const

Returns the set of OpenGL extensions supported by this context.

上下文或共享上下文必须是当前的。

另请参阅 hasExtension ().

QOpenGLExtraFunctions *QOpenGLContext:: extraFunctions () const

Get the QOpenGLExtraFunctions instance for this context.

QOpenGLContext offers this as a convenient way to access QOpenGLExtraFunctions without having to manage it manually.

上下文或共享上下文必须是当前的。

返回的 QOpenGLExtraFunctions instance is ready to be used and it does not need initializeOpenGLFunctions() to be called.

注意: QOpenGLExtraFunctions contains functionality that is not guaranteed to be available at runtime. Runtime availability depends on the platform, graphics driver, and the OpenGL version requested by the application.

另请参阅 QOpenGLFunctions and QOpenGLExtraFunctions .

QSurfaceFormat QOpenGLContext:: format () const

Returns the format of the underlying platform context, if create () has been called.

Otherwise, returns the requested format.

The requested and the actual format may differ. Requesting a given OpenGL version does not mean the resulting context will target exactly the requested version. It is only guaranteed that the version/profile/options combination for the created context is compatible with the request, as long as the driver is able to provide such a context.

For example, requesting an OpenGL version 3.x core profile context may result in an OpenGL 4.x core profile context. Similarly, a request for OpenGL 2.1 may result in an OpenGL 3.0 context with deprecated functions enabled. Finally, depending on the driver, unsupported versions may result in either a context creation failure or in a context for the highest supported version.

Similar differences are possible in the buffer sizes, for example, the resulting context may have a larger depth buffer than requested. This is perfectly normal.

另请参阅 setFormat ().

QOpenGLFunctions *QOpenGLContext:: 函数 () const

Get the QOpenGLFunctions instance for this context.

QOpenGLContext offers this as a convenient way to access QOpenGLFunctions without having to manage it manually.

上下文或共享上下文必须是当前的。

返回的 QOpenGLFunctions instance is ready to be used and it does not need initializeOpenGLFunctions() to be called.

QFunctionPointer QOpenGLContext:: getProcAddress (const QByteArray & procName ) const

Resolves the function pointer to an OpenGL extension function, identified by procName

返回 nullptr if no such function can be found.

QFunctionPointer QOpenGLContext:: getProcAddress (const char * procName ) const

这是重载函数。

该函数在 Qt 5.8 引入。

[static] QOpenGLContext *QOpenGLContext:: globalShareContext ()

Returns the application-wide shared OpenGL context, if present. Otherwise, returns a null pointer.

This is useful if you need to upload OpenGL objects (buffers, textures, etc.) before creating or showing a QOpenGLWidget or QQuickWidget .

注意: You must set the Qt::AA_ShareOpenGLContexts flag on QGuiApplication before creating the QGuiApplication object, otherwise Qt may not create a global shared context.

警告: Do not attempt to make the context returned by this function current on any surface. Instead, you can create a new context which shares with the global one, and then make the new context current.

该函数在 Qt 5.5 引入。

另请参阅 Qt::AA_ShareOpenGLContexts , setShareContext (),和 makeCurrent ().

bool QOpenGLContext:: hasExtension (const QByteArray & extension ) const

返回 true 若此 OpenGL 上下文支持指定 OpenGL extension , false 否则。

上下文或共享上下文必须是当前的。

另请参阅 extensions ().

bool QOpenGLContext:: isOpenGLES () const

返回 true 若上下文是 OpenGL ES 上下文。

If the context has not yet been created, the result is based on the requested format set via setFormat ().

该函数在 Qt 5.3 引入。

另请参阅 create (), format (),和 setFormat ().

bool QOpenGLContext:: isValid () const

Returns if this context is valid, i.e. has been successfully created.

On some platforms the return value of false for a context that was successfully created previously indicates that the OpenGL context was lost.

The typical way to handle context loss scenarios in applications is to check via this function whenever makeCurrent () fails and returns false . If this function then returns false , recreate the underlying native OpenGL context by calling create (), call makeCurrent () again and then reinitialize all OpenGL resources.

另请参阅 create ().

bool QOpenGLContext:: makeCurrent ( QSurface * surface )

Makes the context current in the current thread, against the given surface 。返回 true 若成功;否则返回 false . The latter may happen if the surface is not exposed, or the graphics hardware is not available due to e.g. the application being suspended.

surface is 0 this is equivalent to calling doneCurrent ().

Avoid calling this function from a different thread than the one the QOpenGLContext instance lives in. If you wish to use QOpenGLContext from a different thread you should first make sure it's not current in the current thread, by calling doneCurrent () if necessary. Then call moveToThread (otherThread) before using it in the other thread.

By default Qt employs a check that enforces the above condition on the thread affinity. It is still possible to disable this check by setting the Qt::AA_DontCheckOpenGLContextThreadAffinity application attribute. Be sure to understand the consequences of using QObjects from outside the thread they live in, as explained in the QObject thread affinity 文档编制。

另请参阅 函数 (), doneCurrent (),和 Qt::AA_DontCheckOpenGLContextThreadAffinity .

QVariant QOpenGLContext:: nativeHandle () const

Returns the native handle for the context.

This function provides access to the QOpenGLContext 's underlying native context. The returned variant contains a platform-specific value type. These classes can be found in the module QtPlatformHeaders .

On platforms where retrieving the native handle is not supported, or if neither create () nor setNativeHandle () was called, a null variant is returned.

该函数在 Qt 5.4 引入。

另请参阅 setNativeHandle ().

[static] void *QOpenGLContext:: openGLModuleHandle ()

Returns the platform-specific handle for the OpenGL implementation that is currently in use. (for example, a HMODULE on Windows)

On platforms that do not use dynamic GL switch the return value is null.

The library might be GL-only, meaning that windowing system interface functions (for example EGL) may live in another, separate library.

注意: This function requires that the QGuiApplication instance is already created.

该函数在 Qt 5.3 引入。

另请参阅 openGLModuleType ().

[static] QOpenGLContext::OpenGLModuleType QOpenGLContext:: openGLModuleType ()

Returns the underlying OpenGL implementation type.

On platforms where the OpenGL implementation is not dynamically loaded, the return value is determined during compile time and never changes.

注意: A desktop OpenGL implementation may be capable of creating ES-compatible contexts too. Therefore in most cases it is more appropriate to check QSurfaceFormat::renderableType () or use the convenience function isOpenGLES ().

注意: This function requires that the QGuiApplication instance is already created.

该函数在 Qt 5.3 引入。

QScreen *QOpenGLContext:: screen () const

返回为其创建的上下文屏幕。

另请参阅 setScreen ().

void QOpenGLContext:: setFormat (const QSurfaceFormat & format )

设置 format the OpenGL context should be compatible with. You need to call create () before it takes effect.

When the format is not explicitly set via this function, the format returned by QSurfaceFormat::defaultFormat () will be used. This means that when having multiple contexts, individual calls to this function can be replaced by one single call to QSurfaceFormat::setDefaultFormat () before creating the first context.

另请参阅 format ().

void QOpenGLContext:: setNativeHandle (const QVariant & handle )

Set the native handles for this context. When create () is called and a native handle is set, configuration settings, like format (), are ignored since this QOpenGLContext will wrap an already created native context instead of creating a new one from scratch.

On some platforms the native context handle is not sufficient and other related handles (for example, for a window or display) have to be provided in addition. Therefore handle is variant containing a platform-specific value type. These classes can be found in the QtPlatformHeaders 模块。

create () is called with native handles set, QOpenGLContext does not take ownership of the handles, so destroying the QOpenGLContext does not destroy the native context.

注意: Some frameworks track the current context and surfaces internally. Making the adopted QOpenGLContext current via Qt will have no effect on such other frameworks' internal state. Therefore a subsequent makeCurrent done via the other framework may have no effect. It is therefore advisable to make explicit calls to make no context and surface current to reset the other frameworks' internal state after performing OpenGL operations via Qt.

注意: Using foreign contexts with Qt windows and Qt contexts with windows and surfaces created by other frameworks may give unexpected results, depending on the platform, due to potential mismatches in context and window pixel formats. To make sure this does not happen, avoid making contexts and surfaces from different frameworks current together. Instead, prefer approaches based on context sharing where OpenGL resources like textures are accessible both from Qt's and the foreign framework's contexts.

该函数在 Qt 5.4 引入。

另请参阅 nativeHandle ().

void QOpenGLContext:: setScreen ( QScreen * screen )

设置 screen the OpenGL context should be valid for. You need to call create () before it takes effect.

另请参阅 screen ().

void QOpenGLContext:: setShareContext ( QOpenGLContext * shareContext )

Makes this context share textures, shaders, and other OpenGL resources with shareContext . You need to call create () before it takes effect.

另请参阅 shareContext ().

QOpenGLContext *QOpenGLContext:: shareContext () const

Returns the share context this context was created with.

If the underlying platform was not able to support the requested sharing, this will return 0.

另请参阅 setShareContext ().

QOpenGLContextGroup *QOpenGLContext:: shareGroup () const

Returns the share group this context belongs to.

[static] bool QOpenGLContext:: supportsThreadedOpenGL ()

返回 true if the platform supports OpenGL rendering outside the main (gui) thread.

The value is controlled by the platform plugin in use and may also depend on the graphics drivers.

该函数在 Qt 5.5 引入。

QSurface *QOpenGLContext:: surface () const

Returns the surface the context has been made current with.

This is the surface passed as an argument to makeCurrent ().

void QOpenGLContext:: swapBuffers ( QSurface * surface )

Swap the back and front buffers of surface .

Call this to finish a frame of OpenGL rendering, and make sure to call makeCurrent () again before issuing any further OpenGL commands, for example as part of a new frame.

QAbstractOpenGLFunctions *QOpenGLContext:: versionFunctions (const QOpenGLVersionProfile & versionProfile = QOpenGLVersionProfile()) const

Returns a pointer to an object that provides access to all functions for the versionProfile of this context. There is no need to call QAbstractOpenGLFunctions::initializeOpenGLFunctions() as long as this context is current. It is also possible to call this function when the context is not current, but in that case it is the caller's responsibility to ensure proper initialization by calling QAbstractOpenGLFunctions::initializeOpenGLFunctions() afterwards.

Usually one would use the template version of this function to automatically have the result cast to the correct type.

TYPE *QOpenGLContext:: versionFunctions () const

This function overloads versionFunctions().

Returns a pointer to an object that provides access to all functions for the version and profile of this context. There is no need to call QAbstractOpenGLFunctions::initializeOpenGLFunctions() as long as this context is current. It is also possible to call this function when the context is not current, but in that case it is the caller's responsibility to ensure proper initialization by calling QAbstractOpenGLFunctions::initializeOpenGLFunctions() afterwards.

Usually one would use the template version of this function to automatically have the result cast to the correct type.

QOpenGLFunctions_3_3_Core* funcs = 0;
funcs = context->versionFunctions<QOpenGLFunctions_3_3_Core>();
if (!funcs) {
    qWarning() << "Could not obtain required OpenGL context version";
    exit(1);
}
					

It is possible to request a functions object for a different version and profile than that for which the context was created. To do this either use the template version of this function specifying the desired functions object type as the template parameter or by passing in a QOpenGLVersionProfile object as an argument to the non-template function.

Note that requests for function objects of other versions or profiles can fail and in doing so will return a null pointer. Situations in which creation of the functions object can fail are if the request cannot be satisfied due to asking for functions that are not in the version or profile of this context. For example:

  • Requesting a 3.3 core profile functions object would succeed.
  • Requesting a 3.3 compatibility profile functions object would fail. We would fail to resolve the deprecated functions.
  • Requesting a 4.3 core profile functions object would fail. We would fail to resolve the new core functions introduced in versions 4.0-4.3.
  • Requesting a 3.1 functions object would succeed. There is nothing in 3.1 that is not also in 3.3 core.

Note that if creating a functions object via this method that the QOpenGLContext retains ownership of the object. This is to allow the object to be cached and shared.