1 #include "surfacerenderer.h"
3 #include <QOpenGLFunctions>
5 SurfaceRenderer::SurfaceRenderer(QOpenGLContext *context, QWindow *surface)
9 const char *textureVertexProgram =
10 "uniform highp mat4 matrix;\n"
11 "attribute highp vec3 vertexCoordEntry;\n"
12 "attribute highp vec2 textureCoordEntry;\n"
13 "varying highp vec2 textureCoord;\n"
15 " textureCoord = textureCoordEntry;\n"
16 " gl_Position = matrix * vec4(vertexCoordEntry, 1);\n"
19 const char *textureFragmentProgram =
20 "uniform sampler2D texture;\n"
21 "varying highp vec2 textureCoord;\n"
23 " gl_FragColor = texture2D(texture, textureCoord);\n"
26 m_context->makeCurrent(m_surface);
28 //Enable transparent windows
30 glBlendFunc (GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
32 //May need to manually set context here
33 m_shaderProgram = new QGLShaderProgram();
35 m_shaderProgram->addShaderFromSourceCode(QGLShader::Vertex, textureVertexProgram);
36 m_shaderProgram->addShaderFromSourceCode(QGLShader::Fragment, textureFragmentProgram);
37 m_shaderProgram->link();
38 m_shaderProgram->bind();
40 m_vertexCoordEntry = m_shaderProgram->attributeLocation("vertexCoordEntry");
41 m_textureCoordEntry = m_shaderProgram->attributeLocation("textureCoordEntry");
42 m_matrixLocation = m_shaderProgram->uniformLocation("matrix");
46 void SurfaceRenderer::drawImage(const QImage &image, const QRect &geometry)
48 drawTexture(textureFromImage(image), geometry);
51 void SurfaceRenderer::drawTexture(int textureId, const QRect &geometry, int depth)
53 GLfloat zValue = depth / 1000.0f;
54 //Set Texture and Vertex coordinates
55 GLfloat textureCoordinates[] = { 0, 0,
61 GLfloat vertexCoordinates[] = { geometry.left(), geometry.top(), zValue,
62 geometry.right(), geometry.top(), zValue,
63 geometry.right(), geometry.bottom(), zValue,
64 geometry.left(), geometry.bottom(), zValue
67 //Set matrix to transfrom geometry values into gl coordinate space.
68 m_transformMatrix.setToIdentity();
69 m_transformMatrix.scale( 2.0f / m_surface->geometry().width(), 2.0f / m_surface->geometry().height());
70 m_transformMatrix.translate(-m_surface->geometry().width() / 2.0f, -m_surface->geometry().height() / 2.0f);
72 m_shaderProgram->bind();
75 m_context->functions()->glEnableVertexAttribArray(m_vertexCoordEntry);
76 m_context->functions()->glEnableVertexAttribArray(m_textureCoordEntry);
78 m_context->functions()->glVertexAttribPointer(m_vertexCoordEntry, 3, GL_FLOAT, GL_FALSE, 0, vertexCoordinates);
79 m_context->functions()->glVertexAttribPointer(m_textureCoordEntry, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates);
80 m_shaderProgram->setUniformValue(m_matrixLocation, m_transformMatrix);
82 glBindTexture(GL_TEXTURE_2D, textureId);
84 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
86 glBindTexture(GL_TEXTURE_2D, 0);
88 m_context->functions()->glDisableVertexAttribArray(m_vertexCoordEntry);
89 m_context->functions()->glDisableVertexAttribArray(m_textureCoordEntry);
90 m_shaderProgram->release();
93 GLuint SurfaceRenderer::textureFromImage(const QImage &image)
95 //TODO: Replace this line
96 QImage convertedImage = QGLWidget::convertToGLFormat(image);
99 //Copy QImage data to Texture
100 glBindTexture(GL_TEXTURE_2D, textureId);
101 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, convertedImage.width(), convertedImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, convertedImage.constBits());
103 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
104 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
105 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
106 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);