QOpenGLBuffer Class
QOpenGLBuffer 类提供了用于创建和管理 OpenGL 缓冲对象的函数。更多
Header: | #include <QOpenGLBuffer> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS OpenGL) target_link_libraries(mytarget PRIVATE Qt6::OpenGL) |
qmake: | QT += opengl |
- 所有成员(包括继承成员)的列表
- QOpenGLBuffer 是3D 渲染的一部分。
公共类型
enum | Access { ReadOnly, WriteOnly, ReadWrite } |
enum | RangeAccessFlag { RangeRead, RangeWrite, RangeInvalidate, RangeInvalidateBuffer, RangeFlushExplicit, RangeUnsynchronized } |
flags | RangeAccessFlags |
enum | Type { VertexBuffer, IndexBuffer, PixelPackBuffer, PixelUnpackBuffer } |
enum | UsagePattern { StreamDraw, StreamRead, StreamCopy, StaticDraw, StaticRead, …, DynamicCopy } |
公共函数
QOpenGLBuffer() | |
QOpenGLBuffer(QOpenGLBuffer::Type type) | |
QOpenGLBuffer(const QOpenGLBuffer &other) | |
(since 6.5) | QOpenGLBuffer(QOpenGLBuffer &&other) |
~QOpenGLBuffer() | |
void | allocate(const void *data, int count) |
void | allocate(int count) |
bool | bind() |
GLuint | bufferId() const |
bool | create() |
void | destroy() |
bool | isCreated() const |
void * | map(QOpenGLBuffer::Access access) |
void * | mapRange(int offset, int count, QOpenGLBuffer::RangeAccessFlags access) |
bool | read(int offset, void *data, int count) |
void | release() |
void | setUsagePattern(QOpenGLBuffer::UsagePattern value) |
int | size() const |
(since 6.5) void | swap(QOpenGLBuffer &other) |
QOpenGLBuffer::Type | type() const |
bool | unmap() |
QOpenGLBuffer::UsagePattern | usagePattern() const |
void | write(int offset, const void *data, int count) |
(since 6.5) QOpenGLBuffer & | operator=(QOpenGLBuffer &&other) |
QOpenGLBuffer & | operator=(const QOpenGLBuffer &other) |
静态公共成员
void | release(QOpenGLBuffer::Type type) |
详细说明
缓冲对象在 OpenGL 服务器中创建,这样客户端程序就可以避免每次需要时都上传顶点、索引、纹理图像数据等。
QOpenGLBuffer 对象可以作为底层 OpenGL 缓冲对象的引用被复制:
QOpenGLBuffer buffer1(QOpenGLBuffer::IndexBuffer); buffer1.create(); QOpenGLBuffer buffer2 = buffer1;
以这种方式复制对象时,QOpenGLBuffer 会执行浅层复制,但不会执行写入时复制语义。只要修改副本,原始对象就会受到影响。
成员类型文档
enum QOpenGLBuffer::Access
该枚举定义了QOpenGLBuffer::map() 的访问模式。
常量 | 值 | 说明 |
---|---|---|
QOpenGLBuffer::ReadOnly | 0x88B8 | 缓冲区只映射为读取模式。 |
QOpenGLBuffer::WriteOnly | 0x88B9 | 缓冲区只映射用于写入。 |
QOpenGLBuffer::ReadWrite | 0x88BA | 缓冲区将被映射用于读取和写入。 |
枚举 QOpenGLBuffer::RangeAccessFlag
flags QOpenGLBuffer::RangeAccessFlags
该枚举定义了QOpenGLBuffer::mapRange() 的访问模式位。
常量 | 值 | 说明 |
---|---|---|
QOpenGLBuffer::RangeRead | 0x0001 | 缓冲区将被映射用于读取。 |
QOpenGLBuffer::RangeWrite | 0x0002 | 缓冲区将被映射用于写入。 |
QOpenGLBuffer::RangeInvalidate | 0x0004 | 丢弃指定范围内之前的内容。 |
QOpenGLBuffer::RangeInvalidateBuffer | 0x0008 | 丢弃整个缓冲区之前的内容。 |
QOpenGLBuffer::RangeFlushExplicit | 0x0010 | 表示要通过glFlushMappedBufferRange 明确刷新修改内容。 |
QOpenGLBuffer::RangeUnsynchronized | 0x0020 | 表示在从mapRange() 返回之前不应同步待处理操作。 |
RangeAccessFlags 类型是QFlags<RangeAccessFlag> 的类型定义。它存储 RangeAccessFlag 值的 OR 组合。
enum QOpenGLBuffer::Type
该枚举定义了使用QOpenGLBuffer 创建的 OpenGL 缓冲区对象的类型。
常量 | 值 | 描述 |
---|---|---|
QOpenGLBuffer::VertexBuffer | 0x8892 | 顶点缓冲对象,用于指定顶点阵列。 |
QOpenGLBuffer::IndexBuffer | 0x8893 | 索引缓冲区对象,用于glDrawElements() 。 |
QOpenGLBuffer::PixelPackBuffer | 0x88EB | 像素包缓冲区对象,用于从 OpenGL 服务器读取像素数据(例如,使用glReadPixels() )。在 OpenGL/ES 下不支持。 |
QOpenGLBuffer::PixelUnpackBuffer | 0x88EC | 像素解包缓冲区对象,用于将像素数据写入 OpenGL 服务器(例如,使用glTexImage2D() )。OpenGL/ES 下不支持。 |
enum QOpenGLBuffer::UsagePattern
该枚举定义了QOpenGLBuffer 对象的使用模式。
常量 | 值 | 说明 |
---|---|---|
QOpenGLBuffer::StreamDraw | 0x88E0 | 数据将被设置一次,并在绘制操作中多次使用。在 OpenGL/ES 1.1 下,这与 StaticDraw 相同。 |
QOpenGLBuffer::StreamRead | 0x88E1 | 数据将被设置一次,并在从 OpenGL 服务器读取数据时使用几次。在 OpenGL/ES 下不支持。 |
QOpenGLBuffer::StreamCopy | 0x88E2 | 数据将被设置一次,并在从 OpenGL 服务器读回数据时使用几次,以便在进一步的绘图操作中使用。OpenGL/ES 下不支持。 |
QOpenGLBuffer::StaticDraw | 0x88E4 | 数据将设置一次,并多次用于绘制操作。 |
QOpenGLBuffer::StaticRead | 0x88E5 | 数据将被设置一次,并多次用于从 OpenGL 服务器读回数据。在 OpenGL/ES 下不支持。 |
QOpenGLBuffer::StaticCopy | 0x88E6 | 数据将设置一次,并多次用于从 OpenGL 服务器读回数据,以用于进一步的绘制操作。OpenGL/ES 下不支持。 |
QOpenGLBuffer::DynamicDraw | 0x88E8 | 数据将被反复修改并多次用于绘制操作。 |
QOpenGLBuffer::DynamicRead | 0x88E9 | 数据将被反复修改,并多次用于从 OpenGL 服务器读回数据。在 OpenGL/ES 下不支持。 |
QOpenGLBuffer::DynamicCopy | 0x88EA | 数据将被反复修改,并多次用于从 OpenGL 服务器读回数据,以用于进一步的绘制操作。在 OpenGL/ES 下不支持。 |
成员函数文档
QOpenGLBuffer::QOpenGLBuffer()
构造一个类型为QOpenGLBuffer::VertexBuffer 的新缓冲区对象。
注意:该构造函数只是创建 QOpenGLBuffer 实例。OpenGL 服务器中的实际缓冲区对象要到调用create() 时才会创建。
另请参阅 create() 。
[explicit]
QOpenGLBuffer::QOpenGLBuffer(QOpenGLBuffer::Type type)
构造一个type 的新缓冲区对象。
注意:此构造函数只是创建 QOpenGLBuffer 实例。OpenGL 服务器中的实际缓冲区对象要到调用create() 时才会创建。
另请参阅 create() 。
QOpenGLBuffer::QOpenGLBuffer(const QOpenGLBuffer &other)
构造other 的浅层副本。
注意:QOpenGLBuffer 没有实现写入时复制语义,因此只要修改副本,other 就会受到影响。
[noexcept, since 6.5]
QOpenGLBuffer::QOpenGLBuffer(QOpenGLBuffer &&other)
Move- 从other 构建一个新的 QOpenGLBuffer。
注意: moved-from 对象other 处于部分形成状态,其中唯一有效的操作是销毁和赋值。
此函数在 Qt 6.5 中引入。
[noexcept]
QOpenGLBuffer::~QOpenGLBuffer()
销毁此缓冲对象,包括 OpenGL 服务器中正在使用的存储空间。
void QOpenGLBuffer::allocate(const void *data, int count)
为缓冲区分配count 字节的空间,初始化为data 的内容。之前的内容将被删除。
假定create() 已在此缓冲区上调用过,且已绑定到当前上下文。
另请参阅 create()、read() 和write()。
void QOpenGLBuffer::allocate(int count)
这是一个重载函数。
为缓冲区分配count 字节的空间。之前的内容将被删除。
假定create() 已在此缓冲区上调用过,且已与当前上下文绑定。
bool QOpenGLBuffer::bind()
将与此对象关联的缓冲区绑定到当前 OpenGL 上下文。如果无法绑定,则返回false
,通常是因为该 OpenGL 实现不支持type()。
缓冲区必须绑定到调用create() 时的同一个QOpenGLContext 或与之共享的另一个QOpenGLContext 。否则,此函数将返回 false。
GLuint QOpenGLBuffer::bufferId() const
返回与此缓冲区相关的 OpenGL 标识符;如果缓冲区尚未创建,则返回 0。
另请参阅 isCreated().
bool QOpenGLBuffer::create()
在 OpenGL 服务器中创建缓冲区对象。如果对象已创建,则返回true
;否则返回 false。
调用此函数时必须使用当前QOpenGLContext 。缓冲区将绑定到该上下文(或与其共享的任何其他上下文),且只能在该上下文中使用。
如果 OpenGL 实现不支持缓冲区,或当前没有QOpenGLContext ,则此函数将返回 false。
另请参阅 isCreated(),allocate(),write() 和destroy().
void QOpenGLBuffer::destroy()
销毁此缓冲对象,包括 OpenGL 服务器中使用的存储空间。所有对缓冲区的引用都将失效。
bool QOpenGLBuffer::isCreated() const
如果缓冲区已创建,则返回true
;否则返回 false。
void *QOpenGLBuffer::map(QOpenGLBuffer::Access access)
将此缓冲区的内容映射到应用程序的内存空间,并返回一个指针。如果无法进行内存映射,则返回空值。access 参数指示要执行的访问类型。
假定已在此缓冲区上调用create() 并将其绑定到当前上下文。
注: 只有在 OpenGL ES 2.0 或更早版本(如果有GL_OES_mapbuffer
扩展)下才支持此函数。
注: 在 OpenGL ES 3.0 及更新版本中,或者在桌面 OpenGL 中,如果支持GL_ARB_map_buffer_range
,则此函数使用glMapBufferRange
而不是glMapBuffer
。
另请参阅 unmap()、create()、bind() 和mapRange()。
void *QOpenGLBuffer::mapRange(int offset, int count, QOpenGLBuffer::RangeAccessFlags access)
将offset 和count 指定的缓冲区内容范围映射到应用程序的内存空间,并返回一个指针。如果无法进行内存映射,则返回空值。access 参数指定了访问标志组合。
假定create() 已在此缓冲区上调用,且已绑定到当前上下文。
注意: 此函数不适用于 OpenGL ES 2.0 及更早版本。
另请参阅 unmap()、create() 和bind()。
bool QOpenGLBuffer::read(int offset, void *data, int count)
从offset 开始,将该缓冲区中的count 字节读入data 。成功时返回true
;如果不支持从缓冲区读取,则返回 false。OpenGL/ES 不支持缓冲区读取。
假定此缓冲区已绑定到当前上下文。
void QOpenGLBuffer::release()
从当前 OpenGL 上下文中释放与此对象相关联的缓冲区。
调用此函数时,必须使用与在缓冲区上调用bind() 时相同的QOpenGLContext current。
另请参阅 bind()。
[static]
void QOpenGLBuffer::release(QOpenGLBuffer::Type type)
释放当前QOpenGLContext 中与type 相关联的缓冲区。
该函数是对glBindBuffer(type, 0)
的直接调用,用于调用者不知道哪个QOpenGLBuffer 已绑定到上下文,但又想确保它已被释放的情况。
QOpenGLBuffer::release(QOpenGLBuffer::VertexBuffer);
void QOpenGLBuffer::setUsagePattern(QOpenGLBuffer::UsagePattern value)
将此缓冲区对象的使用模式设置为value 。必须在调用allocate() 或write() 之前调用此函数。
另请参阅 usagePattern()、allocate() 和write()。
int QOpenGLBuffer::size() const
返回该缓冲区中数据的大小,用于读取操作。如果不支持获取缓冲区大小,或缓冲区尚未创建,则返回-1。
假定此缓冲区已绑定到当前上下文。
[noexcept, since 6.5]
void QOpenGLBuffer::swap(QOpenGLBuffer &other)
将此缓冲区与other 互换。该操作速度非常快,而且不会失败。
此函数在 Qt 6.5 中引入。
QOpenGLBuffer::Type QOpenGLBuffer::type() const
返回此对象所代表的缓冲区类型。
bool QOpenGLBuffer::unmap()
在调用map() 将缓冲区映射到应用程序的内存空间后,将其解除映射。如果取消映射成功,则返回true
;否则返回 false。
假定该缓冲区已绑定到当前上下文,并且之前已通过map() 映射过。
注意: 只有在 OpenGL ES 2.0 及更早版本中存在GL_OES_mapbuffer
扩展时,才支持此函数。
另请参阅 map() 。
QOpenGLBuffer::UsagePattern QOpenGLBuffer::usagePattern() const
返回此缓冲区对象的使用模式。默认值是StaticDraw 。
另请参阅 setUsagePattern() 。
void QOpenGLBuffer::write(int offset, const void *data, int count)
用data 的内容替换缓冲区中从offset 开始的count 字节。缓冲区中的其他字节将保持不变。
假定create() 已在此缓冲区上调用,且已绑定到当前上下文。
另请参阅 create()、read() 和allocate()。
[noexcept, since 6.5]
QOpenGLBuffer &QOpenGLBuffer::operator=(QOpenGLBuffer &&other)
移动--将other 赋值给此QOpenGLBuffer 实例。
注意: moved-from 对象other 处于部分形成状态,在这种状态下,唯一有效的操作是销毁和赋一个新值。
此函数在 Qt 6.5 中引入。
QOpenGLBuffer &QOpenGLBuffer::operator=(const QOpenGLBuffer &other)
将other 的浅层副本分配给此对象。
注意:QOpenGLBuffer 没有实现写入时复制语义,因此只要修改副本,other 就会受到影响。
© 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.