7770949d45305e05a74f37828fcbef3f09cea51f
[profile/ivi/qtbase.git] / examples / opengl / hellowindow / hellowindow.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 **     of its contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "hellowindow.h"
42
43 #include <QOpenGLContext>
44
45 #include <QTimer>
46
47 #include <qmath.h>
48
49 Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
50     : m_initialized(false)
51     , m_format(format)
52 {
53     m_context = new QOpenGLContext(this);
54     if (screen)
55         m_context->setScreen(screen);
56     m_context->setFormat(format);
57     if (share)
58         m_context->setShareContext(share->m_context);
59     m_context->create();
60 }
61
62 HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
63     : m_colorIndex(0), m_renderer(renderer), m_timer(0)
64 {
65     setSurfaceType(QWindow::OpenGLSurface);
66     setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
67
68     setGeometry(QRect(10, 10, 640, 480));
69
70     setFormat(renderer->format());
71
72     create();
73
74     connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
75             renderer.data(), SLOT(render(QSurface *, const QColor &, const QSize &)));
76
77     updateColor();
78 }
79
80 void HelloWindow::exposeEvent(QExposeEvent *event)
81 {
82     Q_UNUSED(event);
83
84     render();
85
86     if (!m_timer) {
87         m_timer = new QTimer(this);
88         connect(m_timer, SIGNAL(timeout()), this, SLOT(render()));
89         m_timer->start(10);
90     }
91 }
92
93 void HelloWindow::mousePressEvent(QMouseEvent *)
94 {
95     updateColor();
96 }
97
98 void HelloWindow::render()
99 {
100     emit needRender(this, m_color, size());
101 }
102
103 void HelloWindow::updateColor()
104 {
105     QColor colors[] =
106     {
107         QColor(100, 255, 0),
108         QColor(0, 100, 255)
109     };
110
111     m_color = colors[m_colorIndex];
112
113     m_colorIndex++;
114     if (m_colorIndex >= int(sizeof(colors) / sizeof(colors[0])))
115         m_colorIndex = 0;
116 }
117
118 void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
119 {
120     if (!m_context->makeCurrent(surface))
121         return;
122
123     if (!m_initialized) {
124         initialize();
125         m_initialized = true;
126     }
127
128     glViewport(0, 0, viewSize.width(), viewSize.height());
129
130     glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
131     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132
133     glFrontFace(GL_CW);
134     glCullFace(GL_FRONT);
135     glEnable(GL_CULL_FACE);
136     glEnable(GL_DEPTH_TEST);
137
138     QMatrix4x4 modelview;
139     modelview.rotate(m_fAngle, 0.0f, 1.0f, 0.0f);
140     modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
141     modelview.rotate(m_fAngle, 0.0f, 0.0f, 1.0f);
142     modelview.translate(0.0f, -0.2f, 0.0f);
143
144     m_program->bind();
145     m_program->setUniformValue(matrixUniform, modelview);
146     m_program->setUniformValue(colorUniform, color);
147     paintQtLogo();
148     m_program->release();
149
150     glDisable(GL_DEPTH_TEST);
151     glDisable(GL_CULL_FACE);
152
153     m_context->swapBuffers(surface);
154
155     m_fAngle += 1.0f;
156 }
157
158 void Renderer::paintQtLogo()
159 {
160     m_program->enableAttributeArray(normalAttr);
161     m_program->enableAttributeArray(vertexAttr);
162     m_program->setAttributeArray(vertexAttr, vertices.constData());
163     m_program->setAttributeArray(normalAttr, normals.constData());
164     glDrawArrays(GL_TRIANGLES, 0, vertices.size());
165     m_program->disableAttributeArray(normalAttr);
166     m_program->disableAttributeArray(vertexAttr);
167 }
168
169 void Renderer::initialize()
170 {
171     glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
172
173     QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
174     const char *vsrc =
175         "attribute highp vec4 vertex;\n"
176         "attribute mediump vec3 normal;\n"
177         "uniform mediump mat4 matrix;\n"
178         "uniform lowp vec4 sourceColor;\n"
179         "varying mediump vec4 color;\n"
180         "void main(void)\n"
181         "{\n"
182         "    vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
183         "    float angle = max(dot(normal, toLight), 0.0);\n"
184         "    vec3 col = sourceColor.rgb;\n"
185         "    color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n"
186         "    color = clamp(color, 0.0, 1.0);\n"
187         "    gl_Position = matrix * vertex;\n"
188         "}\n";
189     vshader->compileSourceCode(vsrc);
190
191     QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
192     const char *fsrc =
193         "varying mediump vec4 color;\n"
194         "void main(void)\n"
195         "{\n"
196         "    gl_FragColor = color;\n"
197         "}\n";
198     fshader->compileSourceCode(fsrc);
199
200     m_program = new QOpenGLShaderProgram(this);
201     m_program->addShader(vshader);
202     m_program->addShader(fshader);
203     m_program->link();
204
205     vertexAttr = m_program->attributeLocation("vertex");
206     normalAttr = m_program->attributeLocation("normal");
207     matrixUniform = m_program->uniformLocation("matrix");
208     colorUniform = m_program->uniformLocation("sourceColor");
209
210     m_fAngle = 0;
211     createGeometry();
212 }
213
214 void Renderer::createGeometry()
215 {
216     vertices.clear();
217     normals.clear();
218
219     qreal x1 = +0.06f;
220     qreal y1 = -0.14f;
221     qreal x2 = +0.14f;
222     qreal y2 = -0.06f;
223     qreal x3 = +0.08f;
224     qreal y3 = +0.00f;
225     qreal x4 = +0.30f;
226     qreal y4 = +0.22f;
227
228     quad(x1, y1, x2, y2, y2, x2, y1, x1);
229     quad(x3, y3, x4, y4, y4, x4, y3, x3);
230
231     extrude(x1, y1, x2, y2);
232     extrude(x2, y2, y2, x2);
233     extrude(y2, x2, y1, x1);
234     extrude(y1, x1, x1, y1);
235     extrude(x3, y3, x4, y4);
236     extrude(x4, y4, y4, x4);
237     extrude(y4, x4, y3, x3);
238
239     const qreal Pi = 3.14159f;
240     const int NumSectors = 100;
241
242     for (int i = 0; i < NumSectors; ++i) {
243         qreal angle1 = (i * 2 * Pi) / NumSectors;
244         qreal x5 = 0.30 * qSin(angle1);
245         qreal y5 = 0.30 * qCos(angle1);
246         qreal x6 = 0.20 * qSin(angle1);
247         qreal y6 = 0.20 * qCos(angle1);
248
249         qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors;
250         qreal x7 = 0.20 * qSin(angle2);
251         qreal y7 = 0.20 * qCos(angle2);
252         qreal x8 = 0.30 * qSin(angle2);
253         qreal y8 = 0.30 * qCos(angle2);
254
255         quad(x5, y5, x6, y6, x7, y7, x8, y8);
256
257         extrude(x6, y6, x7, y7);
258         extrude(x8, y8, x5, y5);
259     }
260
261     for (int i = 0;i < vertices.size();i++)
262         vertices[i] *= 2.0f;
263 }
264
265 void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
266 {
267     vertices << QVector3D(x1, y1, -0.05f);
268     vertices << QVector3D(x2, y2, -0.05f);
269     vertices << QVector3D(x4, y4, -0.05f);
270
271     vertices << QVector3D(x3, y3, -0.05f);
272     vertices << QVector3D(x4, y4, -0.05f);
273     vertices << QVector3D(x2, y2, -0.05f);
274
275     QVector3D n = QVector3D::normal
276         (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
277
278     normals << n;
279     normals << n;
280     normals << n;
281
282     normals << n;
283     normals << n;
284     normals << n;
285
286     vertices << QVector3D(x4, y4, 0.05f);
287     vertices << QVector3D(x2, y2, 0.05f);
288     vertices << QVector3D(x1, y1, 0.05f);
289
290     vertices << QVector3D(x2, y2, 0.05f);
291     vertices << QVector3D(x4, y4, 0.05f);
292     vertices << QVector3D(x3, y3, 0.05f);
293
294     n = QVector3D::normal
295         (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
296
297     normals << n;
298     normals << n;
299     normals << n;
300
301     normals << n;
302     normals << n;
303     normals << n;
304 }
305
306 void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
307 {
308     vertices << QVector3D(x1, y1, +0.05f);
309     vertices << QVector3D(x2, y2, +0.05f);
310     vertices << QVector3D(x1, y1, -0.05f);
311
312     vertices << QVector3D(x2, y2, -0.05f);
313     vertices << QVector3D(x1, y1, -0.05f);
314     vertices << QVector3D(x2, y2, +0.05f);
315
316     QVector3D n = QVector3D::normal
317         (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));
318
319     normals << n;
320     normals << n;
321     normals << n;
322
323     normals << n;
324     normals << n;
325     normals << n;
326 }