QOpenGLContext 類錶示本機 OpenGL 上下文,使 OpenGL 能夠渲染在 QSurface . 更多...
| 頭: |
#include <QOpenGLContext>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
|
| qmake: |
QT += gui
|
| 繼承: | 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 ) |
| QNativeInterface * | nativeInterface () const |
| QScreen * | screen () const |
| void | setFormat (const QSurfaceFormat & format ) |
| void | setScreen (QScreen * screen ) |
| void | setShareContext (QOpenGLContext * shareContext ) |
| QOpenGLContext * | shareContext () const |
| QOpenGLContextGroup * | shareGroup () const |
| QSurface * | surface () const |
| void | swapBuffers (QSurface * surface ) |
| void | aboutToBeDestroyed () |
| bool | areSharing (QOpenGLContext * first , QOpenGLContext * second ) |
| QOpenGLContext * | currentContext () |
| QOpenGLContext * | globalShareContext () |
| QOpenGLContext::OpenGLModuleType | openGLModuleType () |
| bool | supportsThreadedOpenGL () |
QOpenGLContext 錶示底層 OpenGL 上下文的 OpenGL 狀態。要設置上下文,意味著要設置與使用上下文的一個或多個錶麵相匹配的屏幕和格式,若有必要,使之與其它上下文共享資源采用 setShareContext (),最後調用 create ()。使用返迴值或 isValid () 校驗上下文是否已成功初始化。
可以使上下文目前針對給定錶麵,通過調用 makeCurrent ()。當 OpenGL 渲染完成時,調用 swapBuffers () 交換錶麵的前後緩衝,以便新近渲染內容變為可見。為能夠支持某些平颱,QOpenGLContext 要求調用 makeCurrent () 再次在開始渲染新的幀之前,之後調用 swapBuffers ().
若臨時不需要上下文,譬如,當不渲染應用程序時,刪除它以釋放資源可能很有用。可以連接到 aboutToBeDestroyed () 信號以清理與 QOpenGLContext 本身具有不同所有權而分配的任何資源。
一旦使 QOpenGLContext 變為當前,就可以以獨立於平颱的方式渲染它通過使用 Qt 的 OpenGL 使能器,譬如 QOpenGLFunctions , QOpenGLBuffer , QOpenGLShaderProgram ,和 QOpenGLFramebufferObject 。直接使用平颱的 OpenGL API 也是可能的,不使用 Qt 使能器,盡管有可移植性潛在成本。後者有必要,當想要使用 OpenGL 1.x 或 OpenGL ES 1.x 時。
有關 OpenGL API 的更多信息,參考官方 OpenGL 文檔編製 .
如何使用 QOpenGLContext 的範例見 OpenGL Window 範例。
可以將 QOpenGLContext 移到不同綫程采用 moveToThread ()。不要調用 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 () 先於調用 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.
警告: WebAssembly
We recommend that only one QOpenGLContext is made current with a QSurface , for the entire lifetime of the QSurface . Should more than once context be used, it is important to understand that multiple QOpenGLContext instances may be backed by the same native context underneath with the WebAssembly platform. Therefore, calling makeCurrent () with the same QSurface on two QOpenGLContext objects may not switch to a different native context in the second call. As a result, any OpenGL state changes done after the second makeCurrent () may alter the state of the first QOpenGLContext as well, as they are all backed by the same native context.
注意: This means that when targeting WebAssembly with existing OpenGL-based Qt code, some porting may be required to cater to these limitations.
另請參閱 QOpenGLFunctions , QOpenGLBuffer , QOpenGLShaderProgram ,和 QOpenGLFramebufferObject .
此枚舉定義底層 OpenGL 實現的類型。
| 常量 | 值 | 描述 |
|---|---|---|
QOpenGLContext::LibGL
|
0
|
OpenGL |
QOpenGLContext::LibGLES
|
1
|
OpenGL ES 2.0 或更高版本 |
[explicit]
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 noexcept]
QOpenGLContext::
~QOpenGLContext
()
銷毀 QOpenGLContext 對象。
若這是綫程的當前上下文, doneCurrent () 也被調用。
[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.
注意: In Qt for Python, this signal will not be received when emitted from the destructor of QOpenGLWidget or QOpenGLWindow due to the Python instance already being destroyed. We recommend doing cleanups in QWidget::hideEvent () 代替。
[static]
bool
QOpenGLContext::
areSharing
(
QOpenGLContext
*
first
,
QOpenGLContext
*
second
)
返迴
true
若
first
and
second
上下文正共享 OpenGL 資源。
試圖采用當前配置創建 OpenGL 上下文。
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
nullptr
, if no context is current.
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 .
Convenience function for calling makeCurrent 采用 0 錶麵。
This results in no context being current in the current thread.
另請參閱 makeCurrent () 和 currentContext ().
Returns the set of OpenGL extensions supported by this context.
上下文或共享上下文必須是當前的。
另請參閱 hasExtension ().
獲取 QOpenGLExtraFunctions 實例為此上下文。
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 .
Returns the format of the underlying platform context, if create () 被調用。
否則,返迴請求格式。
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 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.
Resolves the function pointer to an OpenGL extension function, identified by procName
返迴
nullptr
若找不到這種功能。
這是重載函數。
[static]
QOpenGLContext
*QOpenGLContext::
globalShareContext
()
Returns the application-wide shared OpenGL context, if present. Otherwise, returns
nullptr
.
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 先於創建 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::AA_ShareOpenGLContexts , setShareContext (),和 makeCurrent ().
返迴
true
若此 OpenGL 上下文支持指定 OpenGL
extension
,
false
否則。
上下文或共享上下文必須是當前的。
另請參閱 extensions ().
返迴 true 若上下文是 OpenGL ES 上下文。
If the context has not yet been created, the result is based on the requested format set via setFormat ().
另請參閱 create (), format (),和 setFormat ().
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.
On some platforms context loss situations is not something that can avoided. On others however, they may need to be opted-in to. This can be done by enabling
ResetNotification
在
QSurfaceFormat
. This will lead to setting
RESET_NOTIFICATION_STRATEGY_EXT
to
LOSE_CONTEXT_ON_RESET_EXT
in the underlying native OpenGL context.
QOpenGLContext
will then monitor the status via
glGetGraphicsResetStatusEXT()
in every
makeCurrent
().
另請參閱 create ().
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
nullptr
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 .
Returns a native interface of the given type for the context.
此函數提供對平颱特定功能的訪問為 QOpenGLContext ,作為定義在 QNativeInterface 名稱空間:
| 在 macOS 的 NSOpenGLContext 的本機接口 | |
| ELX 上下文的本機接口 | |
| GLX 上下文的本機接口 | |
| Windows WGL 上下文的本機接口 |
若請求接口不可用
nullptr
被返迴。
[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 ().
注意: 此函數要求 QGuiApplication instance is already created.
返迴為其創建的上下文屏幕。
另請參閱 setScreen ().
設置 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 ().
設置 screen the OpenGL context should be valid for. You need to call create () before it takes effect.
另請參閱 screen ().
Makes this context share textures, shaders, and other OpenGL resources with shareContext . You need to call create () before it takes effect.
另請參閱 shareContext ().
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 ().
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.
Returns the surface the context has been made current with.
This is the surface passed as an argument to makeCurrent ().
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.