本页内容

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 及更高版本的核心功能受到支持;对于较旧的版本,则通过 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*() 函数
    • 解除 VAO 的绑定 (release()))

在渲染函数中绑定 VAO 的操作,会恢复初始化阶段设置的所有顶点数据状态。通过这种方式,我们可以在设置 VAO 时配置大量状态,并高效地在待渲染对象的状态集之间进行切换。 使用 VAO 还允许 OpenGL 驱动程序分摊顶点数据的验证检查工作。

注意:顶点 数组对象(VAO)与所有其他 OpenGL 容器对象一样,仅适用于其创建时的上下文,且无法在上下文组内共享。

另请参阅 QOpenGLVertexArrayObject::Binder 和QOpenGLBuffer 。

成员函数文档

[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的方式运行的准备。这意味着,不能仅仅调用bind(),而必须检查isCreated()的返回值,并在没有顶点数组对象时以传统方式初始化顶点数组。

另请参阅 isCreated()。

void QOpenGLVertexArrayObject::destroy()

销毁底层的 OpenGL 顶点数组对象。要使此函数成功,当前必须存在一个支持顶点数组对象的有效 OpenGL 上下文。

bool QOpenGLVertexArrayObject::isCreated() const

若返回true ,表示已创建了相应的 OpenGL 顶点数组对象。如果返回true 且关联的 OpenGL 上下文为当前上下文,则可以对该对象调用bind()。

GLuint QOpenGLVertexArrayObject::objectId() const

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

void QOpenGLVertexArrayObject::release()

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

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