6#include <QtOpenGL/QOpenGLShaderProgram>
7#include <QtOpenGL/QOpenGLVertexArrayObject>
8#include <QtOpenGL/QOpenGLBuffer>
9#include <QtGui/QOpenGLContext>
10#include <QtGui/QOpenGLFunctions>
11#include <QtGui/QOpenGLExtraFunctions>
13#ifndef GL_TEXTURE_EXTERNAL_OES
14#define GL_TEXTURE_EXTERNAL_OES 0x8D65
16#ifndef GL_TEXTURE_RECTANGLE
17#define GL_TEXTURE_RECTANGLE 0x84F5
19#ifndef GL_TEXTURE_WIDTH
20#define GL_TEXTURE_WIDTH 0x1000
22#ifndef GL_TEXTURE_HEIGHT
23#define GL_TEXTURE_HEIGHT 0x1001
73 "in vec3 vertexCoord;"
74 "in vec2 textureCoord;"
76 "uniform mat4 vertexTransform;"
77 "uniform mat3 textureTransform;"
79 " uv = (textureTransform * vec3(textureCoord,1.0)).xy;"
80 " gl_Position = vertexTransform * vec4(vertexCoord,1.0);"
87 "uniform sampler2D textureSampler;"
88 "uniform bool swizzle;"
89 "uniform float opacity;"
91 " vec4 tmpFragColor = texture(textureSampler, uv);"
92 " tmpFragColor.a *= opacity;"
93 " fragcolor = swizzle ? tmpFragColor.bgra : tmpFragColor;"
97 "attribute highp vec3 vertexCoord;"
98 "attribute highp vec2 textureCoord;"
99 "varying highp vec2 uv;"
100 "uniform highp mat4 vertexTransform;"
101 "uniform highp mat3 textureTransform;"
103 " uv = (textureTransform * vec3(textureCoord,1.0)).xy;"
104 " gl_Position = vertexTransform * vec4(vertexCoord,1.0);"
108 "varying highp vec2 uv;"
109 "uniform sampler2D textureSampler;"
110 "uniform bool swizzle;"
111 "uniform highp float opacity;"
113 " highp vec4 tmpFragColor = texture2D(textureSampler,uv);"
114 " tmpFragColor.a *= opacity;"
115 " gl_FragColor = swizzle ? tmpFragColor.bgra : tmpFragColor;"
119 "#extension GL_OES_EGL_image_external : require\n"
120 "varying highp vec2 uv;"
121 "uniform samplerExternalOES textureSampler;\n"
122 "uniform bool swizzle;"
123 "uniform highp float opacity;"
125 " highp vec4 tmpFragColor = texture2D(textureSampler, uv);"
126 " tmpFragColor.a *= opacity;"
127 " gl_FragColor = swizzle ? tmpFragColor.bgra : tmpFragColor;"
131 "varying highp vec2 uv;"
132 "uniform sampler2DRect textureSampler;"
133 "uniform bool swizzle;"
134 "uniform highp float opacity;"
136 " highp vec4 tmpFragColor = texture2DRect(textureSampler,uv);"
137 " tmpFragColor.a *= opacity;"
138 " gl_FragColor = swizzle ? tmpFragColor.bgra : tmpFragColor;"
142 "#version 150 core\n"
144 "out vec4 fragcolor;"
145 "uniform sampler2DRect textureSampler;"
146 "uniform bool swizzle;"
147 "uniform float opacity;"
149 " vec4 tmpFragColor = texture(textureSampler, uv);"
150 " tmpFragColor.a *= opacity;"
151 " fragcolor = swizzle ? tmpFragColor.bgra : tmpFragColor;"
278 program->glProgram->enableAttributeArray(
program->vertexCoordAttribPos);
281 program->glProgram->setUniformValue(
program->vertexTransformUniformPos, vertexTransform);
285 program->glProgram->enableAttributeArray(
program->textureCoordAttribPos);
329 program->glProgram->setUniformValue(
program->textureTransformUniformPos, textureTransform);
351 program->glProgram->setUniformValue(
program->textureTransformUniformPos, textureTransform);
356 program->glProgram->setUniformValue(
program->textureTransformUniformPos, textureTransform);
371 p->glProgram->link();
372 if (!
p->glProgram->isLinked()) {
373 qWarning() <<
"Could not link shader program:\n" <<
p->glProgram->log();
377 p->glProgram->bind();
379 p->vertexCoordAttribPos =
p->glProgram->attributeLocation(
"vertexCoord");
380 p->vertexTransformUniformPos =
p->glProgram->uniformLocation(
"vertexTransform");
381 p->textureCoordAttribPos =
p->glProgram->attributeLocation(
"textureCoord");
382 p->textureTransformUniformPos =
p->glProgram->uniformLocation(
"textureTransform");
383 p->swizzleUniformPos =
p->glProgram->uniformLocation(
"swizzle");
384 p->opacityUniformPos =
p->glProgram->uniformLocation(
"opacity");
386 p->glProgram->setUniformValue(
p->swizzleUniformPos,
false);
389 p->glProgram->release();
420 return !
programs[idx].glProgram.isNull();
486 d->vertexBuffer.create();
487 d->vertexBuffer.bind();
489 d->vertexBuffer.release();
491 d->textureBuffer.create();
492 d->textureBuffer.bind();
494 d->textureBuffer.release();
527 d->vertexBuffer.destroy();
528 d->textureBuffer.destroy();
541 return ctx &&
ctx->isOpenGLES() &&
ctx->hasExtension(
"GL_OES_EGL_image_external");
553 if (!
ctx ||
ctx->isOpenGLES())
556 if (
ctx->hasExtension(
"GL_ARB_texture_rectangle"))
559 if (
ctx->hasExtension(
"GL_EXT_texture_rectangle"))
563 const auto version =
qMakePair(
f.majorVersion(),
f.minorVersion());
585 if (
d->vao->isCreated())
590 if (!
d->ensureProgram(programIndex))
594 p->glProgram->bind();
596 d->vertexBuffer.bind();
597 p->glProgram->setAttributeBuffer(
p->vertexCoordAttribPos,
GL_FLOAT, 0, 3, 0);
598 p->glProgram->enableAttributeArray(
p->vertexCoordAttribPos);
599 d->vertexBuffer.release();
601 d->textureBuffer.bind();
602 p->glProgram->setAttributeBuffer(
p->textureCoordAttribPos,
GL_FLOAT, 0, 2, 0);
603 p->glProgram->enableAttributeArray(
p->textureCoordAttribPos);
604 d->textureBuffer.release();
617 p->glProgram->release();
618 if (
d->vao->isCreated())
650 d->opacity = opacity;
729 qreal x_translate = x_scale - 1 + ((relative_to_viewport.
x() /
viewport.width()) * 2);
730 qreal y_translate = -y_scale + 1 - ((relative_to_viewport.
y() /
viewport.height()) * 2);
733 matrix(0,3) = x_translate;
734 matrix(1,3) = y_translate;
754 const QSize &textureSize,
761 qreal x_translate = topLeft.
x() / textureSize.
width();
766 y_translate = 1 - y_translate;
770 matrix(0,2) = x_translate;
771 matrix(1,2) = y_translate;
QBlitterTextureBinder(GLenum target, GLuint textureId)
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
void scale(const QVector3D &vector)
Multiplies this matrix by another that scales coordinates by the components of vector.
QGenericMatrix< N, M, float > toGenericMatrix() const
Constructs a NxM generic matrix from the left-most N columns and top-most M rows of this 4x4 matrix.
The QOpenGLBuffer class provides functions for creating and managing OpenGL buffer objects.
bool bind()
Binds the buffer associated with this object to the current OpenGL context.
void release()
Releases the buffer associated with this object from the current OpenGL context.
QSurfaceFormat format() const
Returns the format of the underlying platform context, if create() has been called.
static QOpenGLContext * currentContext()
Returns the last context which called makeCurrent in the current thread, or \nullptr,...
QOpenGLFunctions * functions() const
Get the QOpenGLFunctions instance for this context.
void glDrawArrays(GLenum mode, GLint first, GLsizei count)
Convenience function that calls glDrawArrays(mode, first, count).
void glBindTexture(GLenum target, GLuint texture)
Convenience function that calls glBindTexture(target, texture).
The QOpenGLShaderProgram class allows OpenGL shader programs to be linked and used.
QOpenGLTextureBlitter * q
QMatrix3x3 toTextureCoordinates(const QMatrix3x3 &sourceTransform) const
bool ensureProgram(ProgramIndex idx)
QOpenGLBuffer vertexBuffer
QScopedPointer< QOpenGLVertexArrayObject > vao
QOpenGLTextureBlitterPrivate(QOpenGLTextureBlitter *q_ptr)
QOpenGLBuffer textureBuffer
bool buildProgram(ProgramIndex idx, const char *vs, const char *fs)
void blit(GLuint texture, const QMatrix4x4 &targetTransform, const QMatrix3x3 &sourceTransform)
bool prepareProgram(const QMatrix4x4 &vertexTransform)
The QOpenGLTextureBlitter class provides a convenient way to draw textured quads via OpenGL.
bool supportsRectangleTarget() const
bool create()
Initializes the graphics resources used by the blitter.
void destroy()
Frees all graphics resources held by the blitter.
Origin
\value OriginBottomLeft Indicates that the data in the texture follows the OpenGL convention of coord...
QOpenGLTextureBlitter()
Constructs a new QOpenGLTextureBlitter instance.
void setOpacity(float opacity)
Changes the opacity to opacity.
void blit(GLuint texture, const QMatrix4x4 &targetTransform, Origin sourceOrigin)
Performs the blit with the source texture texture.
~QOpenGLTextureBlitter()
Destructs the instance.
void setRedBlueSwizzle(bool swizzle)
Sets whether swizzling is enabled for the red and blue color channels to swizzle.
static QMatrix3x3 sourceTransform(const QRectF &subTexture, const QSize &textureSize, Origin origin)
Calculates a 3x3 matrix suitable as the input to blit().
void release()
Unbinds the graphics resources used by the blitter.
bool supportsExternalOESTarget() const
static QMatrix4x4 targetTransform(const QRectF &target, const QRect &viewport)
Calculates a target transform suitable for blit().
void bind(GLenum target=GL_TEXTURE_2D)
Binds the graphics resources used by the blitter.
The QOpenGLVertexArrayObject::Binder class is a convenience class to help with the binding and releas...
The QOpenGLVertexArrayObject class wraps an OpenGL Vertex Array Object.
\inmodule QtCore\reentrant
constexpr qreal x() const noexcept
Returns the x coordinate of this point.
constexpr qreal y() const noexcept
Returns the y coordinate of this point.
\inmodule QtCore\reentrant
constexpr qreal height() const noexcept
Returns the height of the rectangle.
constexpr qreal width() const noexcept
Returns the width of the rectangle.
constexpr QPointF topLeft() const noexcept
Returns the position of the rectangle's top-left corner.
\inmodule QtCore\reentrant
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
Deletes the existing object it is pointing to (if any), and sets its pointer to other.
constexpr int height() const noexcept
Returns the height.
constexpr int width() const noexcept
Returns the width.
Combined button and popup list for selecting options.
static QMatrix4x4 targetTransform(const QRectF &target, const QRect &viewport, bool invertY)
static QMatrix3x3 sourceTransform(const QRectF &subTexture, const QSize &textureSize, SourceTransformOrigin origin)
QGenericMatrix< 3, 3, float > QMatrix3x3
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
GLint GLsizei GLsizei height
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLint GLsizei GLsizei GLenum format
GLdouble GLdouble GLdouble GLdouble q
static const char fragment_shader_external_oes[]
#define GL_TEXTURE_HEIGHT
static const char vertex_shader[]
static const GLfloat vertex_buffer_data[]
#define GL_TEXTURE_RECTANGLE
static const char fragment_shader150[]
static const char fragment_shader_rectangle[]
static const char vertex_shader150[]
static const char fragment_shader[]
#define GL_TEXTURE_EXTERNAL_OES
static const char fragment_shader150_rectangle[]
static QOpenGLTextureBlitterPrivate::ProgramIndex targetToProgramIndex(GLenum target)
static const GLfloat texture_buffer_data[]
constexpr decltype(auto) qMakePair(T1 &&value1, T2 &&value2) noexcept(noexcept(std::make_pair(std::forward< T1 >(value1), std::forward< T2 >(value2))))
view viewport() -> scroll(dx, dy, deviceRect)
GLuint vertexTransformUniformPos
GLuint vertexCoordAttribPos
GLuint textureCoordAttribPos
GLuint textureTransformUniformPos
QScopedPointer< QOpenGLShaderProgram > glProgram
TextureMatrixUniform textureMatrixUniformState