QOpenGLTextureBlitter Class
QOpenGLTextureBlitter 类提供了通过 OpenGL 绘制纹理四边形的便捷方法。更多
Header: | #include <QOpenGLTextureBlitter> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS OpenGL) target_link_libraries(mytarget PRIVATE Qt6::OpenGL) |
qmake: | QT += opengl |
- 所有成员(包括继承成员)的列表
- QOpenGLTextureBlitter 是3D 渲染的一部分。
公共类型
enum | Origin { OriginBottomLeft, OriginTopLeft } |
公共函数
QOpenGLTextureBlitter() | |
~QOpenGLTextureBlitter() | |
void | bind(GLenum target = GL_TEXTURE_2D) |
void | blit(GLuint texture, const QMatrix4x4 &targetTransform, QOpenGLTextureBlitter::Origin sourceOrigin) |
void | blit(GLuint texture, const QMatrix4x4 &targetTransform, const QMatrix3x3 &sourceTransform) |
bool | create() |
void | destroy() |
bool | isCreated() const |
void | release() |
void | setOpacity(float opacity) |
void | setRedBlueSwizzle(bool swizzle) |
bool | supportsExternalOESTarget() const |
bool | supportsRectangleTarget() const |
静态公共成员
QMatrix3x3 | sourceTransform(const QRectF &subTexture, const QSize &textureSize, QOpenGLTextureBlitter::Origin origin) |
QMatrix4x4 | targetTransform(const QRectF &target, const QRect &viewport) |
详细说明
绘制纹理四边形以将纹理内容显示到屏幕上是开发 2D 用户界面时的常见操作。QOpenGLTextureBlitter 提供了一个方便的类,以避免重复顶点数据、着色器源、缓冲区和程序管理以及矩阵计算。
例如,QOpenGLWidget 子类可以执行以下操作,在像素位置绘制渲染到帧缓冲中的内容(x, y)
:
void OpenGLWidget::initializeGL() { m_blitter.create(); m_fbo = new QOpenGLFramebufferObject(size); } void OpenGLWidget::paintGL() { m_fbo->bind(); // update offscreen content m_fbo->release(); m_blitter.bind(); const QRect targetRect(QPoint(x, y), m_fbo->size()); const QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(0, 0), m_fbo->size())); m_blitter.blit(m_fbo->texture(), target, QOpenGLTextureBlitter::OriginBottomLeft); m_blitter.release(); }
该混合器实现了 GLSL 1.00(适用于 OpenGL (ES) 2.x 和较新 OpenGL 版本的兼容配置文件)和 150 版本(适用于 OpenGL 3.2 和较新版本的核心配置文件上下文)的 GLSL 着色器。
成员类型文档
enum QOpenGLTextureBlitter::Origin
常数 | 值 | 说明 |
---|---|---|
QOpenGLTextureBlitter::OriginBottomLeft | 0 | 表示纹理中的数据遵循 OpenGL 坐标系惯例,即 Y 轴从下到上。 |
QOpenGLTextureBlitter::OriginTopLeft | 1 | 表示纹理中的数据 Y 轴从上到下,这是普通未翻转图像数据的典型特征。 |
另请参阅 blit().
成员函数文档
QOpenGLTextureBlitter::QOpenGLTextureBlitter()
构造一个新的 QOpenGLTextureBlitter 实例。
注意: 在构造函数中不会初始化图形资源。这使得将普通 QOpenGLTextureBlitter 成员放入类中是安全的,因为依赖于 OpenGL 上下文的实际初始化只发生在create() 中。
[noexcept]
QOpenGLTextureBlitter::~QOpenGLTextureBlitter()
销毁实例。
注意: 如果调用create() 时的 OpenGL 上下文(或与其共享资源的上下文)不是当前的,则不会释放图形资源。因此,建议手动调用destroy() 而不是依赖析构函数来执行 OpenGL 资源清理。
void QOpenGLTextureBlitter::bind(GLenum target = GL_TEXTURE_2D)
绑定混合器使用的图形资源。必须在调用blit() 之前调用。应避免在调用 bind() 和blit() 之间编写修改 OpenGL 状态的代码,否则可能会产生冲突。
target 是源纹理的纹理目标,必须是 、 或 。GL_TEXTURE_2D
GL_TEXTURE_RECTANGLE
GL_OES_EGL_image_external
void QOpenGLTextureBlitter::blit(GLuint texture, const QMatrix4x4 &targetTransform, QOpenGLTextureBlitter::Origin sourceOrigin)
使用源纹理texture 执行混合。
targetTransform 指定应用的变换。通常由 () 辅助函数生成。targetTransform
sourceOrigin 指定图像数据是否需要翻转。当 对应于 FBO 传递的纹理时, 。另一方面,当 基于未翻转的图像数据时,则通过 。这比使用 () 更有效。texture OriginBottomLeft texture OriginTopLeft QImage::flipped
另请参阅 targetTransform(),Origin, 和bind() 。
void QOpenGLTextureBlitter::blit(GLuint texture, const QMatrix4x4 &targetTransform, const QMatrix3x3 &sourceTransform)
使用源纹理texture 执行混合。
targetTransform 指定应用的变换。通常由 () 辅助函数生成。targetTransform
sourceTransform 指定应用于源纹理的变换。这样就可以只使用源纹理的一个子部分。通常由 () 辅助函数生成。sourceTransform
另请参阅 sourceTransform()、targetTransform()、Origin 和bind()。
bool QOpenGLTextureBlitter::create()
初始化混合器使用的图形资源。
成功则返回true
,失败则返回false
。失败可能发生在当前线程上没有 OpenGL 上下文或着色器编译因某种原因失败时。
void QOpenGLTextureBlitter::destroy()
释放混合器持有的所有图形资源。假定调用create() 时线程上的 OpenGL 上下文或与其共享资源的其他上下文是当前的。
当混合器未处于创建状态时,该函数不起作用。
另请参见 create()。
bool QOpenGLTextureBlitter::isCreated() const
如果调用create() 成功,则返回true
。否则返回false
。
void QOpenGLTextureBlitter::release()
解除混合器使用的图形资源的绑定。
另请参见 bind().
void QOpenGLTextureBlitter::setOpacity(float opacity)
将不透明度更改为opacity 。默认不透明度为 1.0。
注意: 混合器不会改变混合状态。调用blit() 的用户应确保混合设置正确有效。
void QOpenGLTextureBlitter::setRedBlueSwizzle(bool swizzle)
设置是否启用红色和蓝色通道的旋转,swizzle 。当源纹理包含来自QImage 的数据时,BGRA 到 RGBA 的转换(在 GPU 上的着色器中进行,而不是在 CPU 端进行缓慢的转换)将非常有用,因为QImage::Format_ARGB32 等格式在小端系统中映射为 BGRA。
默认情况下,红蓝旋转是禁用的,因为这是连接到帧缓冲对象的纹理或基于字节有序QImage 格式(如QImage::Format_RGBA8888 )的纹理所需要的。
[static]
QMatrix3x3 QOpenGLTextureBlitter::sourceTransform(const QRectF &subTexture, const QSize &textureSize, QOpenGLTextureBlitter::Origin origin)
计算适合作为blit() 输入的 3x3 矩阵。当只有部分纹理要用于混合时,就会用到这个矩阵。
subTexture textureSize origin 指定图像数据在 Y 轴上的方向。
bool QOpenGLTextureBlitter::supportsExternalOESTarget() const
当bind() 接受GL_TEXTURE_EXTERNAL_OES
作为目标参数时,返回true
。
bool QOpenGLTextureBlitter::supportsRectangleTarget() const
当bind() 接受GL_TEXTURE_RECTANGLE
作为目标参数时,返回true
。
[static]
QMatrix4x4 QOpenGLTextureBlitter::targetTransform(const QRectF &target, const QRect &viewport)
计算适合blit() 的目标变换。
target 是以像素为单位的目标矩形。 描述了源尺寸,在大多数情况下会设置为(0, 0, 图像宽,图像高)。viewport
对于无缩放输出,target 和viewport 的尺寸应一致。
另请参见 blit()。
© 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.