QOpenGLVertexArrayObject Class

QOpenGLVertexArrayObject 类封装了一个 OpenGL 顶点数组对象。更多

头文件: #include <QOpenGLVertexArrayObject>
CMake: find_package(Qt6 REQUIRED COMPONENTS OpenGL)
target_link_libraries(mytarget PRIVATE Qt6::OpenGL)
qmake: QT += opengl
继承: QObject

公共类型

class Binder

公共函数

QOpenGLVertexArrayObject(QObject *parent = nullptr)
virtual ~QOpenGLVertexArrayObject()
void bind()
bool create()
void destroy()
bool isCreated() const
GLuint objectId() const
void release()

详细说明

顶点阵列对象(VAO)是一种 OpenGL 容器对象,它封装了向 OpenGL 管道指定每个顶点属性数据所需的状态。换句话说,VAO 会记住缓冲区对象(参见QOpenGLBuffer )的状态及其相关状态(如顶点属性除数)。这使得在渲染场景中的不同 "对象 "时,在 OpenGL 缓冲区状态之间进行切换成为一种非常简单高效的方法。QOpenGLVertexArrayObject 类是 OpenGL VAO 的薄包装。

对于桌面,VAO 是 OpenGL 3.0 或更新版本的核心功能,而对于旧版本,VAO 则由 GL_ARB_vertex_array_object 支持。在 OpenGL ES 2 中,VAO 由可选的 GL_OES_vertex_array_object 扩展提供。您可以使用 QOpenGLContext::surfaceFormat() 检查 OpenGL 的版本,并使用QOpenGLContext::hasExtension() 检查扩展是否存在。

与其他Qt OpenGL 类一样,QOpenGLVertexArrayObject 也有一个create() 函数用于创建底层 OpenGL 对象。这样做是为了让开发人员确保当时有一个有效的当前 OpenGL 上下文。

成功创建 VAO 后,典型的使用模式是

  • 在场景初始化函数中,为每个可视化对象:
    • 绑定 VAO
    • 为该视觉对象设置顶点数据状态(顶点、法线、纹理坐标等)
    • 解除绑定(release()) VAO
  • 在渲染函数中,为每个视觉对象
    • 绑定 VAO(如果需要,还可绑定着色器程序)
    • 调用 glDraw*() 函数
    • 解除绑定 (release()) VAO

在呈现函数中绑定 VAO 的作用是恢复初始化阶段设置的所有顶点数据状态。这样,我们就可以在设置 VAO 时设置大量状态,并在要渲染的对象的状态集之间高效切换。使用 VAO 还可以让 OpenGL 驱动程序简化顶点数据的验证检查。

注意: 顶点阵列对象(Vertex Array Objects)与所有其他 OpenGL 容器对象一样,只适用于创建它们的上下文,不能在上下文组之间共享。

另请参阅 QOpenGLVertexArrayObject::BinderQOpenGLBuffer

成员函数文档

[explicit] QOpenGLVertexArrayObject::QOpenGLVertexArrayObject(QObject *parent = nullptr)

使用给定的parent 创建一个 QOpenGLVertexArrayObject。使用前必须使用有效的 OpenGL 上下文调用create() 。

[virtual noexcept] QOpenGLVertexArrayObject::~QOpenGLVertexArrayObject()

销毁QOpenGLVertexArrayObject 和底层 OpenGL 资源。

void QOpenGLVertexArrayObject::bind()

将此顶点数组对象绑定到 OpenGL 绑定点。从此时开始,直到调用release() 或绑定另一个顶点数组对象,对顶点数据状态所做的任何修改都将存储在此顶点数组对象中。

如果绑定了另一个顶点数组对象,以后可以通过再次调用该对象上的 bind() 恢复与该对象相关的状态集。这样就可以在渲染函数中有效地改变顶点数据状态。

bool QOpenGLVertexArrayObject::create()

创建底层 OpenGL 顶点数组对象。必须有支持顶点数组对象的有效 OpenGL 上下文,此函数才能成功执行。

如果成功创建了 OpenGL 顶点数组对象,则返回true

返回值为false 时,顶点数组对象不支持。这不是错误:在使用 OpenGL 2.x 或 OpenGL ES 2.0 的系统中,可能不支持顶点数组对象。在这种情况下,应用程序可以继续执行,但也必须准备以无 VAO 的方式运行。这意味着,在没有顶点数组对象的情况下,必须检查isCreated() 的值,并以传统方式初始化顶点数组,而不是仅仅调用bind() 。

另请参见 isCreated()。

void QOpenGLVertexArrayObject::destroy()

销毁底层 OpenGL 顶点数组对象。必须有支持顶点数组对象的有效 OpenGL 上下文,此函数才能成功执行。

bool QOpenGLVertexArrayObject::isCreated() const

如果已创建底层 OpenGL 顶点数组对象,则返回true 。如果返回true 且相关的 OpenGL 上下文是当前的,则可以bind() 此对象。

GLuint QOpenGLVertexArrayObject::objectId() const

返回底层 OpenGL 顶点数组对象的 ID。

void QOpenGLVertexArrayObject::release()

通过绑定默认顶点数组对象(id = 0)来解除对该顶点数组对象的绑定。

© 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.