1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
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
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.
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."
39 ****************************************************************************/
41 #include "hellowindow.h"
43 #include <QOpenGLContext>
49 Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
50 : m_initialized(false)
53 m_context = new QOpenGLContext(this);
55 m_context->setScreen(screen);
56 m_context->setFormat(format);
58 m_context->setShareContext(share->m_context);
62 HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
63 : m_colorIndex(0), m_renderer(renderer), m_timer(0)
65 setSurfaceType(QWindow::OpenGLSurface);
66 setWindowFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
68 setGeometry(QRect(10, 10, 640, 480));
70 setFormat(renderer->format());
74 connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
75 renderer.data(), SLOT(render(QSurface *, const QColor &, const QSize &)));
80 void HelloWindow::exposeEvent(QExposeEvent *event)
87 m_timer = new QTimer(this);
88 connect(m_timer, SIGNAL(timeout()), this, SLOT(render()));
93 void HelloWindow::mousePressEvent(QMouseEvent *)
98 void HelloWindow::render()
100 emit needRender(this, m_color, size());
103 void HelloWindow::updateColor()
111 m_color = colors[m_colorIndex];
114 if (m_colorIndex >= int(sizeof(colors) / sizeof(colors[0])))
118 void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
120 if (!m_context->makeCurrent(surface))
123 if (!m_initialized) {
125 m_initialized = true;
128 glViewport(0, 0, viewSize.width(), viewSize.height());
130 glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
131 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134 glCullFace(GL_FRONT);
135 glEnable(GL_CULL_FACE);
136 glEnable(GL_DEPTH_TEST);
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);
145 m_program->setUniformValue(matrixUniform, modelview);
146 m_program->setUniformValue(colorUniform, color);
148 m_program->release();
150 glDisable(GL_DEPTH_TEST);
151 glDisable(GL_CULL_FACE);
153 m_context->swapBuffers(surface);
158 void Renderer::paintQtLogo()
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);
169 void Renderer::initialize()
171 glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
173 QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
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"
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"
189 vshader->compileSourceCode(vsrc);
191 QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
193 "varying mediump vec4 color;\n"
196 " gl_FragColor = color;\n"
198 fshader->compileSourceCode(fsrc);
200 m_program = new QOpenGLShaderProgram(this);
201 m_program->addShader(vshader);
202 m_program->addShader(fshader);
205 vertexAttr = m_program->attributeLocation("vertex");
206 normalAttr = m_program->attributeLocation("normal");
207 matrixUniform = m_program->uniformLocation("matrix");
208 colorUniform = m_program->uniformLocation("sourceColor");
214 void Renderer::createGeometry()
228 quad(x1, y1, x2, y2, y2, x2, y1, x1);
229 quad(x3, y3, x4, y4, y4, x4, y3, x3);
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);
239 const qreal Pi = 3.14159f;
240 const int NumSectors = 100;
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);
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);
255 quad(x5, y5, x6, y6, x7, y7, x8, y8);
257 extrude(x6, y6, x7, y7);
258 extrude(x8, y8, x5, y5);
261 for (int i = 0;i < vertices.size();i++)
265 void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
267 vertices << QVector3D(x1, y1, -0.05f);
268 vertices << QVector3D(x2, y2, -0.05f);
269 vertices << QVector3D(x4, y4, -0.05f);
271 vertices << QVector3D(x3, y3, -0.05f);
272 vertices << QVector3D(x4, y4, -0.05f);
273 vertices << QVector3D(x2, y2, -0.05f);
275 QVector3D n = QVector3D::normal
276 (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
286 vertices << QVector3D(x4, y4, 0.05f);
287 vertices << QVector3D(x2, y2, 0.05f);
288 vertices << QVector3D(x1, y1, 0.05f);
290 vertices << QVector3D(x2, y2, 0.05f);
291 vertices << QVector3D(x4, y4, 0.05f);
292 vertices << QVector3D(x3, y3, 0.05f);
294 n = QVector3D::normal
295 (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
306 void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
308 vertices << QVector3D(x1, y1, +0.05f);
309 vertices << QVector3D(x2, y2, +0.05f);
310 vertices << QVector3D(x1, y1, -0.05f);
312 vertices << QVector3D(x2, y2, -0.05f);
313 vertices << QVector3D(x1, y1, -0.05f);
314 vertices << QVector3D(x2, y2, +0.05f);
316 QVector3D n = QVector3D::normal
317 (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));