source: trunk/examples/opengl/overpainting/glwidget.cpp@ 1028

Last change on this file since 1028 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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