1 #include "textureblitter.h"
3 #include <QtGui/QOpenGLShaderProgram>
4 #include <QtGui/QOpenGLContext>
5 #include <QtGui/QOpenGLFunctions>
7 TextureBlitter::TextureBlitter()
8 : m_shaderProgram(new QOpenGLShaderProgram())
10 static const char *textureVertexProgram =
11 "uniform highp mat4 matrix;\n"
12 "attribute highp vec3 vertexCoordEntry;\n"
13 "attribute highp vec2 textureCoordEntry;\n"
14 "varying highp vec2 textureCoord;\n"
16 " textureCoord = textureCoordEntry;\n"
17 " gl_Position = matrix * vec4(vertexCoordEntry, 1);\n"
20 static const char *textureFragmentProgram =
21 "uniform sampler2D texture;\n"
22 "varying highp vec2 textureCoord;\n"
24 " gl_FragColor = texture2D(texture, textureCoord);\n"
27 //Enable transparent windows
29 glBlendFunc (GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
31 m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, textureVertexProgram);
32 m_shaderProgram->addShaderFromSourceCode(QOpenGLShader::Fragment, textureFragmentProgram);
33 m_shaderProgram->link();
37 void TextureBlitter::bind()
40 m_shaderProgram->bind();
42 m_vertexCoordEntry = m_shaderProgram->attributeLocation("vertexCoordEntry");
43 m_textureCoordEntry = m_shaderProgram->attributeLocation("textureCoordEntry");
44 m_matrixLocation = m_shaderProgram->uniformLocation("matrix");
47 void TextureBlitter::release()
49 m_shaderProgram->release();
52 void TextureBlitter::drawTexture(int textureId, const QRectF &targetRect, const QSize &targetSize, int depth, bool targethasInvertedY, bool sourceHasInvertedY)
55 glViewport(0,0,targetSize.width(),targetSize.height());
56 GLfloat zValue = depth / 1000.0f;
57 //Set Texture and Vertex coordinates
58 const GLfloat textureCoordinates[] = {
65 int x1 = targetRect.left();
66 int x2 = targetRect.right();
68 if (targethasInvertedY) {
69 if (sourceHasInvertedY) {
70 y1 = targetRect.top();
71 y2 = targetRect.bottom();
73 y1 = targetRect.bottom();
74 y2 = targetRect.top();
77 if (sourceHasInvertedY) {
78 y1 = targetSize.height() - targetRect.top();
79 y2 = targetSize.height() - targetRect.bottom();
81 y1 = targetSize.height() - targetRect.bottom();
82 y2 = targetSize.height() - targetRect.top();
86 const GLfloat vertexCoordinates[] = {
93 //Set matrix to transfrom geometry values into gl coordinate space.
94 m_transformMatrix.setToIdentity();
95 m_transformMatrix.scale( 2.0f / targetSize.width(), 2.0f / targetSize.height() );
96 m_transformMatrix.translate(-targetSize.width() / 2.0f, -targetSize.height() / 2.0f);
99 QOpenGLContext *currentContext = QOpenGLContext::currentContext();
100 currentContext->functions()->glEnableVertexAttribArray(m_vertexCoordEntry);
101 currentContext->functions()->glEnableVertexAttribArray(m_textureCoordEntry);
103 currentContext->functions()->glVertexAttribPointer(m_vertexCoordEntry, 3, GL_FLOAT, GL_FALSE, 0, vertexCoordinates);
104 currentContext->functions()->glVertexAttribPointer(m_textureCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);
105 m_shaderProgram->setUniformValue(m_matrixLocation, m_transformMatrix);
107 glBindTexture(GL_TEXTURE_2D, textureId);
109 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
112 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
114 glBindTexture(GL_TEXTURE_2D, 0);
116 currentContext->functions()->glDisableVertexAttribArray(m_vertexCoordEntry);
117 currentContext->functions()->glDisableVertexAttribArray(m_textureCoordEntry);