| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtGui>
|
|---|
| 43 | #include <QtOpenGL>
|
|---|
| 44 | #include <stdlib.h>
|
|---|
| 45 |
|
|---|
| 46 | #include <math.h>
|
|---|
| 47 |
|
|---|
| 48 | #include "bubble.h"
|
|---|
| 49 | #include "glwidget.h"
|
|---|
| 50 |
|
|---|
| 51 | #ifndef GL_MULTISAMPLE
|
|---|
| 52 | #define GL_MULTISAMPLE 0x809D
|
|---|
| 53 | #endif
|
|---|
| 54 |
|
|---|
| 55 | //! [0]
|
|---|
| 56 | GLWidget::GLWidget(QWidget *parent)
|
|---|
| 57 | : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
|
|---|
| 58 | {
|
|---|
| 59 | QTime midnight(0, 0, 0);
|
|---|
| 60 | qsrand(midnight.secsTo(QTime::currentTime()));
|
|---|
| 61 |
|
|---|
| 62 | object = 0;
|
|---|
| 63 | xRot = 0;
|
|---|
| 64 | yRot = 0;
|
|---|
| 65 | zRot = 0;
|
|---|
| 66 |
|
|---|
| 67 | trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
|
|---|
| 68 | trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
|
|---|
| 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]
|
|---|
| 81 | GLWidget::~GLWidget()
|
|---|
| 82 | {
|
|---|
| 83 | makeCurrent();
|
|---|
| 84 | glDeleteLists(object, 1);
|
|---|
| 85 | }
|
|---|
| 86 | //! [1]
|
|---|
| 87 |
|
|---|
| 88 | void GLWidget::setXRotation(int angle)
|
|---|
| 89 | {
|
|---|
| 90 | normalizeAngle(&angle);
|
|---|
| 91 | if (angle != xRot)
|
|---|
| 92 | xRot = angle;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | void GLWidget::setYRotation(int angle)
|
|---|
| 96 | {
|
|---|
| 97 | normalizeAngle(&angle);
|
|---|
| 98 | if (angle != yRot)
|
|---|
| 99 | yRot = angle;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void GLWidget::setZRotation(int angle)
|
|---|
| 103 | {
|
|---|
| 104 | normalizeAngle(&angle);
|
|---|
| 105 | if (angle != zRot)
|
|---|
| 106 | zRot = angle;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | //! [2]
|
|---|
| 110 | void GLWidget::initializeGL()
|
|---|
| 111 | {
|
|---|
| 112 | object = makeObject();
|
|---|
| 113 | }
|
|---|
| 114 | //! [2]
|
|---|
| 115 |
|
|---|
| 116 | void GLWidget::mousePressEvent(QMouseEvent *event)
|
|---|
| 117 | {
|
|---|
| 118 | lastPos = event->pos();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void GLWidget::mouseMoveEvent(QMouseEvent *event)
|
|---|
| 122 | {
|
|---|
| 123 | int dx = event->x() - lastPos.x();
|
|---|
| 124 | int dy = event->y() - lastPos.y();
|
|---|
| 125 |
|
|---|
| 126 | if (event->buttons() & Qt::LeftButton) {
|
|---|
| 127 | setXRotation(xRot + 8 * dy);
|
|---|
| 128 | setYRotation(yRot + 8 * dx);
|
|---|
| 129 | } else if (event->buttons() & Qt::RightButton) {
|
|---|
| 130 | setXRotation(xRot + 8 * dy);
|
|---|
| 131 | setZRotation(zRot + 8 * dx);
|
|---|
| 132 | }
|
|---|
| 133 | lastPos = event->pos();
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | void GLWidget::paintEvent(QPaintEvent *event)
|
|---|
| 137 | {
|
|---|
| 138 | makeCurrent();
|
|---|
| 139 | //! [4]
|
|---|
| 140 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 141 | glPushMatrix();
|
|---|
| 142 | //! [4]
|
|---|
| 143 |
|
|---|
| 144 | //! [6]
|
|---|
| 145 | qglClearColor(trolltechPurple.dark());
|
|---|
| 146 | glShadeModel(GL_SMOOTH);
|
|---|
| 147 | glEnable(GL_DEPTH_TEST);
|
|---|
| 148 | glEnable(GL_CULL_FACE);
|
|---|
| 149 | glEnable(GL_LIGHTING);
|
|---|
| 150 | glEnable(GL_LIGHT0);
|
|---|
| 151 | glEnable(GL_MULTISAMPLE);
|
|---|
| 152 | static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
|
|---|
| 153 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
|---|
| 154 |
|
|---|
| 155 | setupViewport(width(), height());
|
|---|
| 156 | //! [6]
|
|---|
| 157 |
|
|---|
| 158 | //! [7]
|
|---|
| 159 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|---|
| 160 | glLoadIdentity();
|
|---|
| 161 | glTranslated(0.0, 0.0, -10.0);
|
|---|
| 162 | glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
|
|---|
| 163 | glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
|
|---|
| 164 | glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
|
|---|
| 165 | glCallList(object);
|
|---|
| 166 | //! [7]
|
|---|
| 167 |
|
|---|
| 168 | //! [8]
|
|---|
| 169 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 170 | glPopMatrix();
|
|---|
| 171 | //! [8]
|
|---|
| 172 |
|
|---|
| 173 | //! [10]
|
|---|
| 174 | QPainter painter(this);
|
|---|
| 175 | painter.setRenderHint(QPainter::Antialiasing);
|
|---|
| 176 | foreach (Bubble *bubble, bubbles) {
|
|---|
| 177 | if (bubble->rect().intersects(event->rect()))
|
|---|
| 178 | bubble->drawBubble(&painter);
|
|---|
| 179 | }
|
|---|
| 180 | drawInstructions(&painter);
|
|---|
| 181 | painter.end();
|
|---|
| 182 | }
|
|---|
| 183 | //! [10]
|
|---|
| 184 |
|
|---|
| 185 | //! [11]
|
|---|
| 186 | void GLWidget::resizeGL(int width, int height)
|
|---|
| 187 | {
|
|---|
| 188 | setupViewport(width, height);
|
|---|
| 189 | }
|
|---|
| 190 | //! [11]
|
|---|
| 191 |
|
|---|
| 192 | //! [12]
|
|---|
| 193 | void GLWidget::showEvent(QShowEvent *event)
|
|---|
| 194 | {
|
|---|
| 195 | Q_UNUSED(event);
|
|---|
| 196 | createBubbles(20 - bubbles.count());
|
|---|
| 197 | }
|
|---|
| 198 | //! [12]
|
|---|
| 199 |
|
|---|
| 200 | QSize GLWidget::sizeHint() const
|
|---|
| 201 | {
|
|---|
| 202 | return QSize(400, 400);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | GLuint GLWidget::makeObject()
|
|---|
| 206 | {
|
|---|
| 207 | GLuint list = glGenLists(1);
|
|---|
| 208 | glNewList(list, GL_COMPILE);
|
|---|
| 209 |
|
|---|
| 210 | glEnable(GL_NORMALIZE);
|
|---|
| 211 | glBegin(GL_QUADS);
|
|---|
| 212 |
|
|---|
| 213 | static GLfloat logoDiffuseColor[4] = {trolltechGreen.red()/255.0,
|
|---|
| 214 | trolltechGreen.green()/255.0,
|
|---|
| 215 | trolltechGreen.blue()/255.0, 1.0};
|
|---|
| 216 | glMaterialfv(GL_FRONT, GL_DIFFUSE, logoDiffuseColor);
|
|---|
| 217 |
|
|---|
| 218 | GLdouble x1 = +0.06;
|
|---|
| 219 | GLdouble y1 = -0.14;
|
|---|
| 220 | GLdouble x2 = +0.14;
|
|---|
| 221 | GLdouble y2 = -0.06;
|
|---|
| 222 | GLdouble x3 = +0.08;
|
|---|
| 223 | GLdouble y3 = +0.00;
|
|---|
| 224 | GLdouble x4 = +0.30;
|
|---|
| 225 | GLdouble y4 = +0.22;
|
|---|
| 226 |
|
|---|
| 227 | quad(x1, y1, x2, y2, y2, x2, y1, x1);
|
|---|
| 228 | quad(x3, y3, x4, y4, y4, x4, y3, x3);
|
|---|
| 229 |
|
|---|
| 230 | extrude(x1, y1, x2, y2);
|
|---|
| 231 | extrude(x2, y2, y2, x2);
|
|---|
| 232 | extrude(y2, x2, y1, x1);
|
|---|
| 233 | extrude(y1, x1, x1, y1);
|
|---|
| 234 | extrude(x3, y3, x4, y4);
|
|---|
| 235 | extrude(x4, y4, y4, x4);
|
|---|
| 236 | extrude(y4, x4, y3, x3);
|
|---|
| 237 |
|
|---|
| 238 | const double Pi = 3.14159265358979323846;
|
|---|
| 239 | const int NumSectors = 200;
|
|---|
| 240 |
|
|---|
| 241 | for (int i = 0; i < NumSectors; ++i) {
|
|---|
| 242 | double angle1 = (i * 2 * Pi) / NumSectors;
|
|---|
| 243 | GLdouble x5 = 0.30 * sin(angle1);
|
|---|
| 244 | GLdouble y5 = 0.30 * cos(angle1);
|
|---|
| 245 | GLdouble x6 = 0.20 * sin(angle1);
|
|---|
| 246 | GLdouble y6 = 0.20 * cos(angle1);
|
|---|
| 247 |
|
|---|
| 248 | double angle2 = ((i + 1) * 2 * Pi) / NumSectors;
|
|---|
| 249 | GLdouble x7 = 0.20 * sin(angle2);
|
|---|
| 250 | GLdouble y7 = 0.20 * cos(angle2);
|
|---|
| 251 | GLdouble x8 = 0.30 * sin(angle2);
|
|---|
| 252 | GLdouble y8 = 0.30 * cos(angle2);
|
|---|
| 253 |
|
|---|
| 254 | quad(x5, y5, x6, y6, x7, y7, x8, y8);
|
|---|
| 255 |
|
|---|
| 256 | extrude(x6, y6, x7, y7);
|
|---|
| 257 | extrude(x8, y8, x5, y5);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | glEnd();
|
|---|
| 261 |
|
|---|
| 262 | glEndList();
|
|---|
| 263 | return list;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
|
|---|
| 267 | GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4)
|
|---|
| 268 | {
|
|---|
| 269 | glNormal3d(0.0, 0.0, -1.0);
|
|---|
| 270 | glVertex3d(x1, y1, -0.05);
|
|---|
| 271 | glVertex3d(x2, y2, -0.05);
|
|---|
| 272 | glVertex3d(x3, y3, -0.05);
|
|---|
| 273 | glVertex3d(x4, y4, -0.05);
|
|---|
| 274 |
|
|---|
| 275 | glNormal3d(0.0, 0.0, 1.0);
|
|---|
| 276 | glVertex3d(x4, y4, +0.05);
|
|---|
| 277 | glVertex3d(x3, y3, +0.05);
|
|---|
| 278 | glVertex3d(x2, y2, +0.05);
|
|---|
| 279 | glVertex3d(x1, y1, +0.05);
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
|
|---|
| 283 | {
|
|---|
| 284 | qglColor(trolltechGreen.dark(250 + int(100 * x1)));
|
|---|
| 285 |
|
|---|
| 286 | glNormal3d((x1 + x2)/2.0, (y1 + y2)/2.0, 0.0);
|
|---|
| 287 | glVertex3d(x1, y1, +0.05);
|
|---|
| 288 | glVertex3d(x2, y2, +0.05);
|
|---|
| 289 | glVertex3d(x2, y2, -0.05);
|
|---|
| 290 | glVertex3d(x1, y1, -0.05);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | void GLWidget::normalizeAngle(int *angle)
|
|---|
| 294 | {
|
|---|
| 295 | while (*angle < 0)
|
|---|
| 296 | *angle += 360 * 16;
|
|---|
| 297 | while (*angle > 360 * 16)
|
|---|
| 298 | *angle -= 360 * 16;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | void GLWidget::createBubbles(int number)
|
|---|
| 302 | {
|
|---|
| 303 | for (int i = 0; i < number; ++i) {
|
|---|
| 304 | QPointF position(width()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))),
|
|---|
| 305 | height()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))));
|
|---|
| 306 | qreal radius = qMin(width(), height())*(0.0125 + 0.0875*qrand()/(RAND_MAX+1.0));
|
|---|
| 307 | QPointF velocity(width()*0.0125*(-0.5 + qrand()/(RAND_MAX+1.0)),
|
|---|
| 308 | height()*0.0125*(-0.5 + qrand()/(RAND_MAX+1.0)));
|
|---|
| 309 |
|
|---|
| 310 | bubbles.append(new Bubble(position, radius, velocity));
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | //! [13]
|
|---|
| 315 | void GLWidget::animate()
|
|---|
| 316 | {
|
|---|
| 317 | QMutableListIterator<Bubble*> iter(bubbles);
|
|---|
| 318 |
|
|---|
| 319 | while (iter.hasNext()) {
|
|---|
| 320 | Bubble *bubble = iter.next();
|
|---|
| 321 | bubble->move(rect());
|
|---|
| 322 | }
|
|---|
| 323 | update();
|
|---|
| 324 | }
|
|---|
| 325 | //! [13]
|
|---|
| 326 |
|
|---|
| 327 | //! [14]
|
|---|
| 328 | void GLWidget::setupViewport(int width, int height)
|
|---|
| 329 | {
|
|---|
| 330 | int side = qMin(width, height);
|
|---|
| 331 | glViewport((width - side) / 2, (height - side) / 2, side, side);
|
|---|
| 332 |
|
|---|
| 333 | glMatrixMode(GL_PROJECTION);
|
|---|
| 334 | glLoadIdentity();
|
|---|
| 335 | glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
|
|---|
| 336 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 337 | }
|
|---|
| 338 | //! [14]
|
|---|
| 339 |
|
|---|
| 340 | //! [15]
|
|---|
| 341 | void GLWidget::drawInstructions(QPainter *painter)
|
|---|
| 342 | {
|
|---|
| 343 | QString text = tr("Click and drag with the left mouse button "
|
|---|
| 344 | "to rotate the Qt logo.");
|
|---|
| 345 | QFontMetrics metrics = QFontMetrics(font());
|
|---|
| 346 | int border = qMax(4, metrics.leading());
|
|---|
| 347 |
|
|---|
| 348 | QRect rect = metrics.boundingRect(0, 0, width() - 2*border, int(height()*0.125),
|
|---|
| 349 | Qt::AlignCenter | Qt::TextWordWrap, text);
|
|---|
| 350 | painter->setRenderHint(QPainter::TextAntialiasing);
|
|---|
| 351 | painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border),
|
|---|
| 352 | QColor(0, 0, 0, 127));
|
|---|
| 353 | painter->setPen(Qt::white);
|
|---|
| 354 | painter->fillRect(QRect(0, 0, width(), rect.height() + 2*border),
|
|---|
| 355 | QColor(0, 0, 0, 127));
|
|---|
| 356 | painter->drawText((width() - rect.width())/2, border,
|
|---|
| 357 | rect.width(), rect.height(),
|
|---|
| 358 | Qt::AlignCenter | Qt::TextWordWrap, text);
|
|---|
| 359 | }
|
|---|
| 360 | //! [15]
|
|---|