QAbstractOpenGLFunctions Class
QAbstractOpenGLFunctions 类是一系列类的基类,这些类公开了每个 OpenGL 版本和配置文件的所有函数。更多
头文件: | #include <QAbstractOpenGLFunctions> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS OpenGL) target_link_libraries(mytarget PRIVATE Qt6::OpenGL) |
qmake: | QT += opengl |
被继承: | 27 类型QOpenGLFunctions_1_0,QOpenGLFunctions_1_1,QOpenGLFunctions_1_2,QOpenGLFunctions_1_3,QOpenGLFunctions_1_4,QOpenGLFunctions_1_5,QOpenGLFunctions_2_0,QOpenGLFunctions_2_1,QOpenGLFunctions_3_0,QOpenGLFunctions_3_1,QOpenGLFunctions_3_2_Compatibility,QOpenGLFunctions_3_2_Core,QOpenGLFunctions_3_3_Compatibility,QOpenGLFunctions_3_3_Core,QOpenGLFunctions_4_0_Compatibility,QOpenGLFunctions_4_0_Core,QOpenGLFunctions_4_1_Compatibility,QOpenGLFunctions_4_1_Core,QOpenGLFunctions_4_2_Compatibility,QOpenGLFunctions_4_2_Core,QOpenGLFunctions_4_3_Compatibility,QOpenGLFunctions_4_3_Core,QOpenGLFunctions_4_4_Compatibility,QOpenGLFunctions_4_4_Core,QOpenGLFunctions_4_5_Compatibility,QOpenGLFunctions_4_5_Core, 以及QOpenGLFunctions_ES2 |
公共函数
virtual | ~QAbstractOpenGLFunctions() |
受保护函数
详细说明
不同平台上的 OpenGL 实现可以链接到不同数量的 OpenGL 函数,具体取决于该平台上的 OpenGL ABI。例如,在 Microsoft Windows 上,在构建时只能链接 OpenGL 1.1 以下的函数。所有其他函数都必须在运行时解决。传统的解决方法是使用QOpenGLContext::getProcAddress() 或QOpenGLFunctions 。前者既繁琐又容易出错,而且意味着要直接处理函数指针。后者只公开 OpenGL ES 2 和桌面 OpenGL 的常用函数。不过,有许多新的 OpenGL 功能在编写现实世界的 OpenGL 应用程序时非常有用。
Qt 现在提供了一系列类,它们都继承自 QAbstractOpenGLFunctions,这些类通过相应的成员函数公开了每个核心 OpenGL 函数。OpenGL 版本和配置文件的每一种有效组合都有一个类。每个类都遵循命名约定:
QOpenGLFunctions_<MAJOR VERSION>_<MINOR VERSION>[_PROFILE]
对于从 1.0 到 3.0 的 OpenGL 版本,没有配置文件,因此需要类:
- QOpenGLFunctions_1_0
- QOpenGLFunctions_1_1
- QOpenGLFunctions_1_2
- QOpenGLFunctions_1_3
- QOpenGLFunctions_1_4
- QOpenGLFunctions_1_5
- QOpenGLFunctions_2_0
- QOpenGLFunctions_2_1
- QOpenGLFunctions_3_0
其中每个类都继承自 QAbstractOpenGLFunctions。
OpenGL 3.1 版删除了许多过时的函数,从而使 API 更加简单和通用。
OpenGL 3.2 引入了配置文件的概念。目前为 OpenGL 定义了两个配置文件:核心配置文件和兼容性配置文件。
核心配置文件不包含任何在 OpenGL 3.1 中删除的函数。兼容性配置文件包含同一版本的核心配置文件中的所有功能,以及在 OpenGL 3.1 中删除的所有功能。因此,兼容性配置文件类允许使用较新的 OpenGL 功能,但也允许您继续使用原有的 OpenGL 代码。对于新的 OpenGL 代码,应首选核心配置文件。
请注意,有些供应商,特别是苹果公司,并没有实施兼容性配置文件。因此,如果您希望在 macOS 上使用新的 OpenGL 功能,则应确保通过QSurfaceFormat::setProfile() 申请 Core 配置文件上下文。
Qt 为所有版本、Core 和 Compatibility 配置文件组合提供了类。适用于 OpenGL 3.1 至 4.3 版本的类有
- QOpenGLFunctions_3_1
- QOpenGLFunctions_3_2_Core
- QOpenGLFunctions_3_2_Compatibility
- QOpenGLFunctions_3_3_Core
- QOpenGLFunctions_3_3_Compatibility
- QOpenGLFunctions_4_0_Core
- QOpenGLFunctions_4_0_Compatibility
- QOpenGLFunctions_4_1_Core
- QOpenGLFunctions_4_1_Compatibility
- QOpenGLFunctions_4_2_Core
- QOpenGLFunctions_4_2_Compatibility
- QOpenGLFunctions_4_3_Core
- QOpenGLFunctions_4_3_Compatibility
其中每个类都继承自 QAbstractOpenGLFunctions。
可通过QOpenGLVersionFunctionsFactory::get() 获取指向与所使用的 OpenGL 版本和配置文件相对应的类对象的指针。如果以这种方式获取,请注意QOpenGLContext 保留对该对象的所有权。这样就可以缓存和共享该实例。
在调用任何公开的 OpenGL 函数之前,必须确保对象已解析了指向 OpenGL 函数的函数指针。每个实例只需通过 initializeOpenGLFunctions() 完成一次。初始化后,该对象即可用于调用相应版本和配置文件的任何 OpenGL 函数。请注意,initializeOpenGLFunctions() 在某些情况下可能会失败,因此请检查返回值。如果某个版本或配置文件的函数对象包含的函数不属于用于解析函数指针的上下文,初始化就会失败。
如果只使用函数对象,那么在尝试使用不包含在该版本和配置文件中的函数时,就会出现编译错误。这显然比运行时的未定义行为更容易调试。
另请参见 QOpenGLVersionFunctionsFactory::get()。
成员函数文档
[protected]
QAbstractOpenGLFunctions::QAbstractOpenGLFunctions()
构造一个 QAbstractOpenGLFunctions 对象。
[virtual noexcept]
QAbstractOpenGLFunctions::~QAbstractOpenGLFunctions()
销毁QAbstractOpenGLFunctions 对象。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.