QOpenGLTextureBlitter 类

QOpenGLTextureBlitter 类提供凭借 OpenGL 绘制纹理四边形的方便手段。 更多...

头: #include <QOpenGLTextureBlitter>
qmake: QT += gui
Since: Qt 5.8

该类在 Qt 5.8 引入。

公共类型

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

静态公共成员

QMatrix3x3 sourceTransform (const QRectF & subTexture , const QSize & textureSize , QOpenGLTextureBlitter::Origin origin )
QMatrix4x4 targetTransform (const QRectF & target , const QRect & viewport )

详细描述

Drawing textured quads, in order to get the contents of a texture onto the screen, is a common operation when developing 2D user interfaces. QOpenGLTextureBlitter provides a convenience class to avoid repeating vertex data, shader sources, buffer and program management and matrix calculations.

For example, a QOpenGLWidget subclass can do the following to draw the contents rendered into a framebuffer at the pixel position (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();
}
					

The blitter implements GLSL shaders both for GLSL 1.00 (suitable for OpenGL (ES) 2.x and compatibility profiles of newer OpenGL versions) and version 150 (suitable for core profile contexts with OpenGL 3.2 and newer).

成员类型文档编制

enum QOpenGLTextureBlitter:: Origin

常量 描述
QOpenGLTextureBlitter::OriginBottomLeft 0 Indicates that the data in the texture follows the OpenGL convention of coordinate systems, meaning Y is running from bottom to top.
QOpenGLTextureBlitter::OriginTopLeft 1 Indicates that the data in the texture has Y running from top to bottom, which is typical with regular, unflipped image data.

另请参阅 blit ().

成员函数文档编制

QOpenGLTextureBlitter:: QOpenGLTextureBlitter ()

Constructs a new QOpenGLTextureBlitter instance.

注意: no graphics resources are initialized in the constructor. This makes it safe to place plain QOpenGLTextureBlitter members into classes because the actual initialization that depends on the OpenGL context happens only in create ().

QOpenGLTextureBlitter:: ~QOpenGLTextureBlitter ()

Destructs the instance.

注意: When the OpenGL context - or a context sharing resources with it - that was current when calling create () is not current, graphics resources will not be released. Therefore, it is recommended to call destroy () manually instead of relying on the destructor to perform OpenGL resource cleanup.

void QOpenGLTextureBlitter:: bind ( GLenum target = GL_TEXTURE_2D)

Binds the graphics resources used by the blitter. This must be called before calling blit (). Code modifying the OpenGL state should be avoided between the call to bind() and blit () because otherwise conflicts may arise.

target is the texture target for the source texture and must be either GL_TEXTURE_2D , GL_TEXTURE_RECTANGLE ,或 GL_OES_EGL_image_external .

另请参阅 release () 和 blit ().

void QOpenGLTextureBlitter:: blit ( GLuint texture , const QMatrix4x4 & targetTransform , QOpenGLTextureBlitter::Origin sourceOrigin )

Performs the blit with the source texture texture .

targetTransform specifies the transformation applied. This is usually generated by the targetTransform () helper function.

sourceOrigin specifies if the image data needs flipping. When texture corresponds to a texture attached to an FBO pass OriginBottomLeft . On the other hand, when texture is based on unflipped image data, pass OriginTopLeft . This is more efficient than using QImage::mirrored ().

另请参阅 targetTransform (), Origin ,和 bind ().

void QOpenGLTextureBlitter:: blit ( GLuint texture , const QMatrix4x4 & targetTransform , const QMatrix3x3 & sourceTransform )

Performs the blit with the source texture texture .

targetTransform specifies the transformation applied. This is usually generated by the targetTransform () helper function.

sourceTransform specifies the transformation applied to the source. This allows using only a sub-rect of the source texture. This is usually generated by the sourceTransform () helper function.

另请参阅 sourceTransform (), targetTransform (), Origin ,和 bind ().

bool QOpenGLTextureBlitter:: create ()

Initializes the graphics resources used by the blitter.

返回 true 若成功, false if there was a failure. Failures can occur when there is no OpenGL context current on the current thread, or when shader compilation fails for some reason.

另请参阅 isCreated () 和 destroy ().

void QOpenGLTextureBlitter:: destroy ()

Frees all graphics resources held by the blitter. Assumes that the OpenGL context, or another context sharing resources with it, that was current on the thread when invoking create () is current.

The function has no effect when the blitter is not in created state.

另请参阅 create ().

bool QOpenGLTextureBlitter:: isCreated () const

返回 true if create () was called and succeeded. false 否则。

另请参阅 create () 和 destroy ().

void QOpenGLTextureBlitter:: release ()

Unbinds the graphics resources used by the blitter.

另请参阅 bind ().

void QOpenGLTextureBlitter:: setOpacity ( float opacity )

Changes the opacity to opacity . The default opacity is 1.0.

注意: the blitter does not alter the blend state. It is up to the caller of blit () to ensure the correct blend settings are active.

void QOpenGLTextureBlitter:: setRedBlueSwizzle ( bool swizzle )

Sets whether swizzling is enabled for the red and blue color channels to swizzle . An BGRA to RGBA conversion (occurring in the shader on the GPU, instead of a slow CPU-side transformation) can be useful when the source texture contains data from a QImage with a format like QImage::Format_ARGB32 which maps to BGRA on little endian systems.

By default the red-blue swizzle is disabled since this is what a texture attached to an framebuffer object or a texture based on a byte ordered QImage format (like QImage::Format_RGBA8888 ) needs.

[static] QMatrix3x3 QOpenGLTextureBlitter:: sourceTransform (const QRectF & subTexture , const QSize & textureSize , QOpenGLTextureBlitter::Origin origin )

Calculates a 3x3 matrix suitable as the input to blit (). This is used when only a part of the texture is to be used in the blit.

subTexture is the desired source rectangle in pixels, textureSize is the full width and height of the texture data. origin specifies the orientation of the image data when it comes to the Y axis.

另请参阅 blit () 和 Origin .

bool QOpenGLTextureBlitter:: supportsExternalOESTarget () const

返回 true bind () accepts GL_TEXTURE_EXTERNAL_OES as its target argument.

另请参阅 bind () 和 blit ().

[static] QMatrix4x4 QOpenGLTextureBlitter:: targetTransform (const QRectF & target , const QRect & viewport )

Calculates a target transform suitable for blit ().

target is the target rectangle in pixels. viewport describes the source dimensions and will in most cases be set to (0, 0, image width, image height).

For unscaled output the size of target and viewport should match.

另请参阅 blit ().