| 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 |
|
|---|
| 45 | #include <math.h>
|
|---|
| 46 |
|
|---|
| 47 | #include "glwidget.h"
|
|---|
| 48 |
|
|---|
| 49 | //! [0]
|
|---|
| 50 | GLWidget::GLWidget(QWidget *parent)
|
|---|
| 51 | : QGLWidget(parent)
|
|---|
| 52 | {
|
|---|
| 53 | object = 0;
|
|---|
| 54 | xRot = 0;
|
|---|
| 55 | yRot = 0;
|
|---|
| 56 | zRot = 0;
|
|---|
| 57 |
|
|---|
| 58 | trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
|
|---|
| 59 | trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
|
|---|
| 60 | }
|
|---|
| 61 | //! [0]
|
|---|
| 62 |
|
|---|
| 63 | //! [1]
|
|---|
| 64 | GLWidget::~GLWidget()
|
|---|
| 65 | {
|
|---|
| 66 | makeCurrent();
|
|---|
| 67 | glDeleteLists(object, 1);
|
|---|
| 68 | }
|
|---|
| 69 | //! [1]
|
|---|
| 70 |
|
|---|
| 71 | //! [2]
|
|---|
| 72 | QSize GLWidget::minimumSizeHint() const
|
|---|
| 73 | {
|
|---|
| 74 | return QSize(50, 50);
|
|---|
| 75 | }
|
|---|
| 76 | //! [2]
|
|---|
| 77 |
|
|---|
| 78 | //! [3]
|
|---|
| 79 | QSize GLWidget::sizeHint() const
|
|---|
| 80 | //! [3] //! [4]
|
|---|
| 81 | {
|
|---|
| 82 | return QSize(400, 400);
|
|---|
| 83 | }
|
|---|
| 84 | //! [4]
|
|---|
| 85 |
|
|---|
| 86 | //! [5]
|
|---|
| 87 | void GLWidget::setXRotation(int angle)
|
|---|
| 88 | {
|
|---|
| 89 | normalizeAngle(&angle);
|
|---|
| 90 | if (angle != xRot) {
|
|---|
| 91 | xRot = angle;
|
|---|
| 92 | emit xRotationChanged(angle);
|
|---|
| 93 | updateGL();
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | //! [5]
|
|---|
| 97 |
|
|---|
| 98 | void GLWidget::setYRotation(int angle)
|
|---|
| 99 | {
|
|---|
| 100 | normalizeAngle(&angle);
|
|---|
| 101 | if (angle != yRot) {
|
|---|
| 102 | yRot = angle;
|
|---|
| 103 | emit yRotationChanged(angle);
|
|---|
| 104 | updateGL();
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | void GLWidget::setZRotation(int angle)
|
|---|
| 109 | {
|
|---|
| 110 | normalizeAngle(&angle);
|
|---|
| 111 | if (angle != zRot) {
|
|---|
| 112 | zRot = angle;
|
|---|
| 113 | emit zRotationChanged(angle);
|
|---|
| 114 | updateGL();
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | //! [6]
|
|---|
| 119 | void GLWidget::initializeGL()
|
|---|
| 120 | {
|
|---|
| 121 | qglClearColor(trolltechPurple.dark());
|
|---|
| 122 | object = makeObject();
|
|---|
| 123 | glShadeModel(GL_FLAT);
|
|---|
| 124 | glEnable(GL_DEPTH_TEST);
|
|---|
| 125 | glEnable(GL_CULL_FACE);
|
|---|
| 126 | }
|
|---|
| 127 | //! [6]
|
|---|
| 128 |
|
|---|
| 129 | //! [7]
|
|---|
| 130 | void GLWidget::paintGL()
|
|---|
| 131 | {
|
|---|
| 132 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|---|
| 133 | glLoadIdentity();
|
|---|
| 134 | glTranslated(0.0, 0.0, -10.0);
|
|---|
| 135 | glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
|
|---|
| 136 | glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
|
|---|
| 137 | glRotated(zRot / 16.0, 0.0, 0.0, 1.0);
|
|---|
| 138 | glCallList(object);
|
|---|
| 139 | }
|
|---|
| 140 | //! [7]
|
|---|
| 141 |
|
|---|
| 142 | //! [8]
|
|---|
| 143 | void GLWidget::resizeGL(int width, int height)
|
|---|
| 144 | {
|
|---|
| 145 | int side = qMin(width, height);
|
|---|
| 146 | glViewport((width - side) / 2, (height - side) / 2, side, side);
|
|---|
| 147 |
|
|---|
| 148 | glMatrixMode(GL_PROJECTION);
|
|---|
| 149 | glLoadIdentity();
|
|---|
| 150 | glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
|
|---|
| 151 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 152 | }
|
|---|
| 153 | //! [8]
|
|---|
| 154 |
|
|---|
| 155 | //! [9]
|
|---|
| 156 | void GLWidget::mousePressEvent(QMouseEvent *event)
|
|---|
| 157 | {
|
|---|
| 158 | lastPos = event->pos();
|
|---|
| 159 | }
|
|---|
| 160 | //! [9]
|
|---|
| 161 |
|
|---|
| 162 | //! [10]
|
|---|
| 163 | void GLWidget::mouseMoveEvent(QMouseEvent *event)
|
|---|
| 164 | {
|
|---|
| 165 | int dx = event->x() - lastPos.x();
|
|---|
| 166 | int dy = event->y() - lastPos.y();
|
|---|
| 167 |
|
|---|
| 168 | if (event->buttons() & Qt::LeftButton) {
|
|---|
| 169 | setXRotation(xRot + 8 * dy);
|
|---|
| 170 | setYRotation(yRot + 8 * dx);
|
|---|
| 171 | } else if (event->buttons() & Qt::RightButton) {
|
|---|
| 172 | setXRotation(xRot + 8 * dy);
|
|---|
| 173 | setZRotation(zRot + 8 * dx);
|
|---|
| 174 | }
|
|---|
| 175 | lastPos = event->pos();
|
|---|
| 176 | }
|
|---|
| 177 | //! [10]
|
|---|
| 178 |
|
|---|
| 179 | GLuint GLWidget::makeObject()
|
|---|
| 180 | {
|
|---|
| 181 | GLuint list = glGenLists(1);
|
|---|
| 182 | glNewList(list, GL_COMPILE);
|
|---|
| 183 |
|
|---|
| 184 | glBegin(GL_QUADS);
|
|---|
| 185 |
|
|---|
| 186 | GLdouble x1 = +0.06;
|
|---|
| 187 | GLdouble y1 = -0.14;
|
|---|
| 188 | GLdouble x2 = +0.14;
|
|---|
| 189 | GLdouble y2 = -0.06;
|
|---|
| 190 | GLdouble x3 = +0.08;
|
|---|
| 191 | GLdouble y3 = +0.00;
|
|---|
| 192 | GLdouble x4 = +0.30;
|
|---|
| 193 | GLdouble y4 = +0.22;
|
|---|
| 194 |
|
|---|
| 195 | quad(x1, y1, x2, y2, y2, x2, y1, x1);
|
|---|
| 196 | quad(x3, y3, x4, y4, y4, x4, y3, x3);
|
|---|
| 197 |
|
|---|
| 198 | extrude(x1, y1, x2, y2);
|
|---|
| 199 | extrude(x2, y2, y2, x2);
|
|---|
| 200 | extrude(y2, x2, y1, x1);
|
|---|
| 201 | extrude(y1, x1, x1, y1);
|
|---|
| 202 | extrude(x3, y3, x4, y4);
|
|---|
| 203 | extrude(x4, y4, y4, x4);
|
|---|
| 204 | extrude(y4, x4, y3, x3);
|
|---|
| 205 |
|
|---|
| 206 | const double Pi = 3.14159265358979323846;
|
|---|
| 207 | const int NumSectors = 200;
|
|---|
| 208 |
|
|---|
| 209 | for (int i = 0; i < NumSectors; ++i) {
|
|---|
| 210 | double angle1 = (i * 2 * Pi) / NumSectors;
|
|---|
| 211 | GLdouble x5 = 0.30 * sin(angle1);
|
|---|
| 212 | GLdouble y5 = 0.30 * cos(angle1);
|
|---|
| 213 | GLdouble x6 = 0.20 * sin(angle1);
|
|---|
| 214 | GLdouble y6 = 0.20 * cos(angle1);
|
|---|
| 215 |
|
|---|
| 216 | double angle2 = ((i + 1) * 2 * Pi) / NumSectors;
|
|---|
| 217 | GLdouble x7 = 0.20 * sin(angle2);
|
|---|
| 218 | GLdouble y7 = 0.20 * cos(angle2);
|
|---|
| 219 | GLdouble x8 = 0.30 * sin(angle2);
|
|---|
| 220 | GLdouble y8 = 0.30 * cos(angle2);
|
|---|
| 221 |
|
|---|
| 222 | quad(x5, y5, x6, y6, x7, y7, x8, y8);
|
|---|
| 223 |
|
|---|
| 224 | extrude(x6, y6, x7, y7);
|
|---|
| 225 | extrude(x8, y8, x5, y5);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | glEnd();
|
|---|
| 229 |
|
|---|
| 230 | glEndList();
|
|---|
| 231 | return list;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
|
|---|
| 235 | GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4)
|
|---|
| 236 | {
|
|---|
| 237 | qglColor(trolltechGreen);
|
|---|
| 238 |
|
|---|
| 239 | glVertex3d(x1, y1, -0.05);
|
|---|
| 240 | glVertex3d(x2, y2, -0.05);
|
|---|
| 241 | glVertex3d(x3, y3, -0.05);
|
|---|
| 242 | glVertex3d(x4, y4, -0.05);
|
|---|
| 243 |
|
|---|
| 244 | glVertex3d(x4, y4, +0.05);
|
|---|
| 245 | glVertex3d(x3, y3, +0.05);
|
|---|
| 246 | glVertex3d(x2, y2, +0.05);
|
|---|
| 247 | glVertex3d(x1, y1, +0.05);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2)
|
|---|
| 251 | {
|
|---|
| 252 | qglColor(trolltechGreen.dark(250 + int(100 * x1)));
|
|---|
| 253 |
|
|---|
| 254 | glVertex3d(x1, y1, +0.05);
|
|---|
| 255 | glVertex3d(x2, y2, +0.05);
|
|---|
| 256 | glVertex3d(x2, y2, -0.05);
|
|---|
| 257 | glVertex3d(x1, y1, -0.05);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | void GLWidget::normalizeAngle(int *angle)
|
|---|
| 261 | {
|
|---|
| 262 | while (*angle < 0)
|
|---|
| 263 | *angle += 360 * 16;
|
|---|
| 264 | while (*angle > 360 * 16)
|
|---|
| 265 | *angle -= 360 * 16;
|
|---|
| 266 | }
|
|---|