QOpenGLTextureBlitter Class
QOpenGLTextureBlitter 类提供了一种通过 OpenGL 绘制带纹理四边形的便捷方法。更多内容...
| 头文件: | #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 指定图像数据是否需要翻转。当texture 对应于附加到 FBO 上的纹理时,传递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 的数据时(其格式如QImage::Format_ARGB32 ,在小端字节序系统上映射为BGRA),进行BGRA到RGBA的转换(在GPU上的着色器中进行,而非在CPU端进行低效的转换)会非常有用。
默认情况下,红蓝位交换功能处于禁用状态,因为这是附加到帧缓冲区对象的纹理,或基于字节顺序为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 是目标矩形的像素尺寸。viewport 描述源图像的尺寸,在大多数情况下应设置为 (0, 0, 图像宽度, 图像高度)。
若要生成未缩放的输出,target 和viewport 的大小应保持一致。
另请参阅 blit()。
© 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.