Debian Codegraph @

examples/opengl/legacy/overpainting/glwidget.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include "bubble.h"
52 #include "qtlogo.h"
53 #include "glwidget.h"
54
55 #include <QMouseEvent>
56 #include <QRandomGenerator>
57 #include <QTime>
58
59 #include <math.h>
60 #include <stdlib.h>
61
62 #ifndef GL_MULTISAMPLE
63 #define GL_MULTISAMPLE 0x809D
64 #endif
65
66 //! [0]
67 GLWidget::GLWidget(QWidget *parent)
68 : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
69 {
70 QTime midnight(0, 0, 0);
71
72 logo = 0;
73 xRot = 0;
74 yRot = 0;
75 zRot = 0;
76
77 qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
78 qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
79
80 animationTimer.setSingleShot(false);
81 connect(&animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
82 animationTimer.start(25);
83
84 setAutoFillBackground(false);
85 setMinimumSize(200, 200);
86 setWindowTitle(tr("Overpainting a Scene"));
87 }
88 //! [0]
89
90 //! [1]
91 GLWidget::~GLWidget()
92 {
93 }
94 //! [1]
95
96 static void qNormalizeAngle(int &angle)
97 {
98 while (angle < 0)
99 angle += 360 * 16;
100 while (angle > 360 * 16)
101 angle -= 360 * 16;
102 }
103
104 void GLWidget::setXRotation(int angle)
105 {
106 qNormalizeAngle(angle);
107 if (angle != xRot)
108 xRot = angle;
109 }
110
111 void GLWidget::setYRotation(int angle)
112 {
113 qNormalizeAngle(angle);
114 if (angle != yRot)
115 yRot = angle;
116 }
117
118 void GLWidget::setZRotation(int angle)
119 {
120 qNormalizeAngle(angle);
121 if (angle != zRot)
122 zRot = angle;
123 }
124
125 //! [2]
126 void GLWidget::initializeGL()
127 {
128 initializeOpenGLFunctions();
129
130 glEnable(GL_MULTISAMPLE);
131
132 logo = new QtLogo(this);
133 logo->setColor(qtGreen.darker());
134 }
135 //! [2]
136
137 void GLWidget::mousePressEvent(QMouseEvent *event)
138 {
139 lastPos = event->pos();
140 }
141
142 void GLWidget::mouseMoveEvent(QMouseEvent *event)
143 {
144 int dx = event->x() - lastPos.x();
145 int dy = event->y() - lastPos.y();
146
147 if (event->buttons() & Qt::LeftButton) {
148 setXRotation(xRot + 8 * dy);
149 setYRotation(yRot + 8 * dx);
150 } else if (event->buttons() & Qt::RightButton) {
151 setXRotation(xRot + 8 * dy);
152 setZRotation(zRot + 8 * dx);
153 }
154 lastPos = event->pos();
155 }
156
157 void GLWidget::paintEvent(QPaintEvent *event)
158 {
159 makeCurrent();
160 //! [4]
161 glMatrixMode(GL_MODELVIEW);
162 glPushMatrix();
163 //! [4]
164
165 //! [6]
166 qglClearColor(qtPurple.darker());
167 glShadeModel(GL_SMOOTH);
168 glEnable(GL_DEPTH_TEST);
169 glEnable(GL_CULL_FACE);
170 glEnable(GL_LIGHTING);
171 glEnable(GL_LIGHT0);
172 glEnable(GL_MULTISAMPLE);
173 static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
174 glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
175
176 qreal pixelRatio = devicePixelRatio();
177 setupViewport(width() * pixelRatio, height() * pixelRatio);
178 //! [6]
179
180 //! [7]
181 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
182 glLoadIdentity();
183 glTranslatef(0.0, 0.0, -10.0);
184 glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
185 glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
186 glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
187
188 logo->draw(static_cast<QOpenGLFunctions_1_1 *>(this));
189 //! [7]
190
191 //! [8]
192 glShadeModel(GL_FLAT);
193 glDisable(GL_CULL_FACE);
194 glDisable(GL_DEPTH_TEST);
195 glDisable(GL_LIGHTING);
196
197 glMatrixMode(GL_MODELVIEW);
198 glPopMatrix();
199 //! [8]
200
201 //! [10]
202 QPainter painter(this);
203 painter.setRenderHint(QPainter::Antialiasing);
204 for (Bubble *bubble : qAsConst(bubbles)) {
205 if (bubble->rect().intersects(event->rect()))
206 bubble->drawBubble(&painter);
207 }
208 drawInstructions(&painter);
209 painter.end();
210 }
211 //! [10]
212
213 //! [11]
214 void GLWidget::resizeGL(int width, int height)
215 {
216 qreal pixelRatio = devicePixelRatio();
217 setupViewport(width*pixelRatio, height*pixelRatio);
218 }
219 //! [11]
220
221 //! [12]
222 void GLWidget::showEvent(QShowEvent *event)
223 {
224 Q_UNUSED(event);
225 createBubbles(20 - bubbles.count());
226 }
227 //! [12]
228
229 QSize GLWidget::sizeHint() const
230 {
231 return QSize(400, 400);
232 }
233
234 void GLWidget::createBubbles(int number)
235 {
236 for (int i = 0; i < number; ++i) {
237 QPointF position(width()*(0.1 + QRandomGenerator::global()->bounded(0.8)),
238 height()*(0.1 + QRandomGenerator::global()->bounded(0.8)));
239 qreal radius = qMin(width(), height())*(0.0125 + QRandomGenerator::global()->bounded(0.0875));
240 QPointF velocity(width()*0.0125*(-0.5 + QRandomGenerator::global()->bounded(1.0)),
241 height()*0.0125*(-0.5 + QRandomGenerator::global()->bounded(1.0)));
242
243 bubbles.append(new Bubble(position, radius, velocity));
244 }
245 }
246
247 //! [13]
248 void GLWidget::animate()
249 {
250 for (Bubble *bubble : qAsConst(bubbles))
251 bubble->move(rect());
252 update();
253 }
254 //! [13]
255
256 //! [14]
257 void GLWidget::setupViewport(int width, int height)
258 {
259 int side = qMin(width, height);
260 glViewport((width - side) / 2, (height - side) / 2, side, side);
261
262 glMatrixMode(GL_PROJECTION);
263 glLoadIdentity();
264 #ifdef QT_OPENGL_ES
265 glOrthof(-0.5, +0.5, -0.5, 0.5, 4.0, 15.0);
266 #else
267 glOrtho(-0.5, +0.5, -0.5, 0.5, 4.0, 15.0);
268 #endif
269 glMatrixMode(GL_MODELVIEW);
270 }
271 //! [14]
272
273 //! [15]
274 void GLWidget::drawInstructions(QPainter *painter)
275 {
276 QString text = tr("Click and drag with the left mouse button "
277 "to rotate the Qt logo.");
278 QFontMetrics metrics = QFontMetrics(font());
279 int border = qMax(4, metrics.leading());
280
281 QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
282 Qt::AlignCenter | Qt::TextWordWrap, text);
283 painter->setRenderHint(QPainter::TextAntialiasing);
284 painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border),
285 QColor(0, 0, 0, 127));
286 painter->setPen(Qt::white);
287 painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border),
288 QColor(0, 0, 0, 127));
289 painter->drawText((width() - rect.width())/2, border,
290 rect.width(), rect.height(),
291 Qt::AlignCenter | Qt::TextWordWrap, text);
292 }
293 //! [15]