Merge branch 'refactor'
[profile/ivi/qtwayland.git] / examples / qwindow-compositor / surfacerenderer.cpp
1 #include "surfacerenderer.h"
2
3 #include <QOpenGLFunctions>
4
5 SurfaceRenderer::SurfaceRenderer(QOpenGLContext *context, QWindow *surface)
6     : m_context(context)
7     , m_surface(surface)
8 {
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"
14             "void main() {\n"
15             "   textureCoord = textureCoordEntry;\n"
16             "   gl_Position = matrix * vec4(vertexCoordEntry, 1);\n"
17             "}\n";
18
19     const char *textureFragmentProgram =
20             "uniform sampler2D texture;\n"
21             "varying highp vec2 textureCoord;\n"
22             "void main() {\n"
23             "   gl_FragColor = texture2D(texture, textureCoord);\n"
24             "}\n";
25
26     m_context->makeCurrent(m_surface);
27
28     //Enable transparent windows
29     glEnable(GL_BLEND);
30     glBlendFunc (GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
31
32     //May need to manually set context here
33     m_shaderProgram = new QGLShaderProgram();
34
35     m_shaderProgram->addShaderFromSourceCode(QGLShader::Vertex, textureVertexProgram);
36     m_shaderProgram->addShaderFromSourceCode(QGLShader::Fragment, textureFragmentProgram);
37     m_shaderProgram->link();
38     m_shaderProgram->bind();
39
40     m_vertexCoordEntry = m_shaderProgram->attributeLocation("vertexCoordEntry");
41     m_textureCoordEntry = m_shaderProgram->attributeLocation("textureCoordEntry");
42     m_matrixLocation = m_shaderProgram->uniformLocation("matrix");
43
44 }
45
46 void SurfaceRenderer::drawImage(const QImage &image, const QRect &geometry)
47 {
48     drawTexture(textureFromImage(image), geometry);
49 }
50
51 void SurfaceRenderer::drawTexture(int textureId, const QRect &geometry, int depth)
52 {
53     GLfloat zValue = depth / 1000.0f;
54     //Set Texture and Vertex coordinates
55     GLfloat textureCoordinates[] = { 0, 0,
56                                      1, 0,
57                                      1, 1,
58                                      0, 1
59                                    };
60
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
65                                   };
66
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);
71
72     m_shaderProgram->bind();
73
74     //attach the data!
75     m_context->functions()->glEnableVertexAttribArray(m_vertexCoordEntry);
76     m_context->functions()->glEnableVertexAttribArray(m_textureCoordEntry);
77
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);
81
82     glBindTexture(GL_TEXTURE_2D, textureId);
83
84     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
85
86     glBindTexture(GL_TEXTURE_2D, 0);
87
88     m_context->functions()->glDisableVertexAttribArray(m_vertexCoordEntry);
89     m_context->functions()->glDisableVertexAttribArray(m_textureCoordEntry);
90     m_shaderProgram->release();
91 }
92
93 GLuint SurfaceRenderer::textureFromImage(const QImage &image)
94 {
95     //TODO: Replace this line
96     QImage convertedImage = QGLWidget::convertToGLFormat(image);
97
98     GLuint textureId;
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());
102
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);
107
108     return textureId;
109 }