| 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 "glwidget.h" |
| 52 |
|
| 53 |
#include <QMouseEvent> |
| 54 |
#include <QTimer> |
| 55 |
#include <qmath.h> |
| 56 |
|
| 57 |
GLWidget::GLWidget(QWidget *parent) |
| 58 |
: QGLWidget(parent) |
| 59 |
{ |
| 60 |
gear1 = 0; |
| 61 |
gear2 = 0; |
| 62 |
gear3 = 0; |
| 63 |
xRot = 0; |
| 64 |
yRot = 0; |
| 65 |
zRot = 0; |
| 66 |
gear1Rot = 0; |
| 67 |
|
| 68 |
QTimer *timer = new QTimer(this); |
| 69 |
connect(timer, SIGNAL(timeout()), this, SLOT(advanceGears())); |
| 70 |
timer->start(20); |
| 71 |
} |
| 72 |
|
| 73 |
GLWidget::~GLWidget() |
| 74 |
{ |
| 75 |
makeCurrent(); |
| 76 |
glDeleteLists(gear1, 1); |
| 77 |
glDeleteLists(gear2, 1); |
| 78 |
glDeleteLists(gear3, 1); |
| 79 |
} |
| 80 |
|
| 81 |
void GLWidget::setXRotation(int angle) |
| 82 |
{ |
| 83 |
normalizeAngle(&angle); |
| 84 |
if (angle != xRot) { |
| 85 |
xRot = angle; |
| 86 |
emit xRotationChanged(angle); |
| 87 |
updateGL(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void GLWidget::setYRotation(int angle) |
| 92 |
{ |
| 93 |
normalizeAngle(&angle); |
| 94 |
if (angle != yRot) { |
| 95 |
yRot = angle; |
| 96 |
emit yRotationChanged(angle); |
| 97 |
updateGL(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void GLWidget::setZRotation(int angle) |
| 102 |
{ |
| 103 |
normalizeAngle(&angle); |
| 104 |
if (angle != zRot) { |
| 105 |
zRot = angle; |
| 106 |
emit zRotationChanged(angle); |
| 107 |
updateGL(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void GLWidget::initializeGL() |
| 112 |
{ |
| 113 |
static const GLfloat lightPos[4] = { 5.0f, 5.0f, 10.0f, 1.0f }; |
| 114 |
static const GLfloat reflectance1[4] = { 0.8f, 0.1f, 0.0f, 1.0f }; |
| 115 |
static const GLfloat reflectance2[4] = { 0.0f, 0.8f, 0.2f, 1.0f }; |
| 116 |
static const GLfloat reflectance3[4] = { 0.2f, 0.2f, 1.0f, 1.0f }; |
| 117 |
|
| 118 |
glLightfv(GL_LIGHT0, GL_POSITION, lightPos); |
| 119 |
glEnable(GL_LIGHTING); |
| 120 |
glEnable(GL_LIGHT0); |
| 121 |
glEnable(GL_DEPTH_TEST); |
| 122 |
|
| 123 |
gear1 = makeGear(reflectance1, 1.0, 4.0, 1.0, 0.7, 20); |
| 124 |
gear2 = makeGear(reflectance2, 0.5, 2.0, 2.0, 0.7, 10); |
| 125 |
gear3 = makeGear(reflectance3, 1.3, 2.0, 0.5, 0.7, 10); |
| 126 |
|
| 127 |
glEnable(GL_NORMALIZE); |
| 128 |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 129 |
} |
| 130 |
|
| 131 |
void GLWidget::paintGL() |
| 132 |
{ |
| 133 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 134 |
|
| 135 |
glPushMatrix(); |
| 136 |
glRotated(xRot / 16.0, 1.0, 0.0, 0.0); |
| 137 |
glRotated(yRot / 16.0, 0.0, 1.0, 0.0); |
| 138 |
glRotated(zRot / 16.0, 0.0, 0.0, 1.0); |
| 139 |
|
| 140 |
drawGear(gear1, -3.0, -2.0, 0.0, gear1Rot / 16.0); |
| 141 |
drawGear(gear2, +3.1, -2.0, 0.0, -2.0 * (gear1Rot / 16.0) - 9.0); |
| 142 |
|
| 143 |
glRotated(+90.0, 1.0, 0.0, 0.0); |
| 144 |
drawGear(gear3, -3.1, -1.8, -2.2, +2.0 * (gear1Rot / 16.0) - 2.0); |
| 145 |
|
| 146 |
glPopMatrix(); |
| 147 |
} |
| 148 |
|
| 149 |
void GLWidget::resizeGL(int width, int height) |
| 150 |
{ |
| 151 |
int side = qMin(width, height); |
| 152 |
glViewport((width - side) / 2, (height - side) / 2, side, side); |
| 153 |
|
| 154 |
glMatrixMode(GL_PROJECTION); |
| 155 |
glLoadIdentity(); |
| 156 |
glFrustum(-1.0, +1.0, -1.0, 1.0, 5.0, 60.0); |
| 157 |
glMatrixMode(GL_MODELVIEW); |
| 158 |
glLoadIdentity(); |
| 159 |
glTranslated(0.0, 0.0, -40.0); |
| 160 |
} |
| 161 |
|
| 162 |
void GLWidget::mousePressEvent(QMouseEvent *event) |
| 163 |
{ |
| 164 |
lastPos = event->pos(); |
| 165 |
} |
| 166 |
|
| 167 |
void GLWidget::mouseMoveEvent(QMouseEvent *event) |
| 168 |
{ |
| 169 |
int dx = event->x() - lastPos.x(); |
| 170 |
int dy = event->y() - lastPos.y(); |
| 171 |
|
| 172 |
if (event->buttons() & Qt::LeftButton) { |
| 173 |
setXRotation(xRot + 8 * dy); |
| 174 |
setYRotation(yRot + 8 * dx); |
| 175 |
} else if (event->buttons() & Qt::RightButton) { |
| 176 |
setXRotation(xRot + 8 * dy); |
| 177 |
setZRotation(zRot + 8 * dx); |
| 178 |
} |
| 179 |
lastPos = event->pos(); |
| 180 |
} |
| 181 |
|
| 182 |
void GLWidget::advanceGears() |
| 183 |
{ |
| 184 |
gear1Rot += 2 * 16; |
| 185 |
updateGL(); |
| 186 |
} |
| 187 |
|
| 188 |
GLuint GLWidget::makeGear(const GLfloat *reflectance, GLdouble innerRadius, |
| 189 |
GLdouble outerRadius, GLdouble thickness, |
| 190 |
GLdouble toothSize, GLint toothCount) |
| 191 |
{ |
| 192 |
GLuint list = glGenLists(1); |
| 193 |
glNewList(list, GL_COMPILE); |
| 194 |
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, reflectance); |
| 195 |
|
| 196 |
GLdouble r0 = innerRadius; |
| 197 |
GLdouble r1 = outerRadius - toothSize / 2.0; |
| 198 |
GLdouble r2 = outerRadius + toothSize / 2.0; |
| 199 |
GLdouble toothAngle = 2 * M_PI / toothCount; |
| 200 |
GLdouble delta = toothAngle / 4.0; |
| 201 |
GLdouble z = thickness / 2.0; |
| 202 |
|
| 203 |
glShadeModel(GL_FLAT); |
| 204 |
|
| 205 |
for (int i = 0; i < 2; ++i) { |
| 206 |
GLdouble sign = (i == 0) ? +1.0 : -1.0; |
| 207 |
|
| 208 |
glNormal3d(0.0, 0.0, sign); |
| 209 |
|
| 210 |
glBegin(GL_QUAD_STRIP); |
| 211 |
for (int j = 0; j <= toothCount; ++j) { |
| 212 |
GLdouble angle = j * toothAngle; |
| 213 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); |
| 214 |
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); |
| 215 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), sign * z); |
| 216 |
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), sign * z); |
| 217 |
} |
| 218 |
glEnd(); |
| 219 |
|
| 220 |
glBegin(GL_QUADS); |
| 221 |
for (int j = 0; j < toothCount; ++j) { |
| 222 |
GLdouble angle = j * toothAngle; |
| 223 |
glVertex3d(r1 * cos(angle), r1 * sin(angle), sign * z); |
| 224 |
glVertex3d(r2 * cos(angle + delta), r2 * sin(angle + delta), sign * z); |
| 225 |
glVertex3d(r2 * cos(angle + 2 * delta), r2 * sin(angle + 2 * delta), sign * z); |
| 226 |
glVertex3d(r1 * cos(angle + 3 * delta), r1 * sin(angle + 3 * delta), sign * z); |
| 227 |
} |
| 228 |
glEnd(); |
| 229 |
} |
| 230 |
|
| 231 |
glBegin(GL_QUAD_STRIP); |
| 232 |
for (int i = 0; i < toothCount; ++i) { |
| 233 |
for (int j = 0; j < 2; ++j) { |
| 234 |
GLdouble angle = (i + j / 2.0) * toothAngle; |
| 235 |
GLdouble s1 = r1; |
| 236 |
GLdouble s2 = r2; |
| 237 |
if (j == 1) |
| 238 |
qSwap(s1, s2); |
| 239 |
|
| 240 |
glNormal3d(cos(angle), sin(angle), 0.0); |
| 241 |
glVertex3d(s1 * cos(angle), s1 * sin(angle), +z); |
| 242 |
glVertex3d(s1 * cos(angle), s1 * sin(angle), -z); |
| 243 |
|
| 244 |
glNormal3d(s2 * sin(angle + delta) - s1 * sin(angle), |
| 245 |
s1 * cos(angle) - s2 * cos(angle + delta), 0.0); |
| 246 |
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), +z); |
| 247 |
glVertex3d(s2 * cos(angle + delta), s2 * sin(angle + delta), -z); |
| 248 |
} |
| 249 |
} |
| 250 |
glVertex3d(r1, 0.0, +z); |
| 251 |
glVertex3d(r1, 0.0, -z); |
| 252 |
glEnd(); |
| 253 |
|
| 254 |
glShadeModel(GL_SMOOTH); |
| 255 |
|
| 256 |
glBegin(GL_QUAD_STRIP); |
| 257 |
for (int i = 0; i <= toothCount; ++i) { |
| 258 |
GLdouble angle = i * toothAngle; |
| 259 |
glNormal3d(-cos(angle), -sin(angle), 0.0); |
| 260 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), +z); |
| 261 |
glVertex3d(r0 * cos(angle), r0 * sin(angle), -z); |
| 262 |
} |
| 263 |
glEnd(); |
| 264 |
|
| 265 |
glEndList(); |
| 266 |
|
| 267 |
return list; |
| 268 |
} |
| 269 |
|
| 270 |
void GLWidget::drawGear(GLuint gear, GLdouble dx, GLdouble dy, GLdouble dz, |
| 271 |
GLdouble angle) |
| 272 |
{ |
| 273 |
glPushMatrix(); |
| 274 |
glTranslated(dx, dy, dz); |
| 275 |
glRotated(angle, 0.0, 0.0, 1.0); |
| 276 |
glCallList(gear); |
| 277 |
glPopMatrix(); |
| 278 |
} |
| 279 |
|
| 280 |
void GLWidget::normalizeAngle(int *angle) |
| 281 |
{ |
| 282 |
while (*angle < 0) |
| 283 |
*angle += 360 * 16; |
| 284 |
while (*angle > 360 * 16) |
| 285 |
*angle -= 360 * 16; |
| 286 |
} |