[562] | 1 | #include <QtGui> |
---|
| 2 | #include <QtOpenGL> |
---|
| 3 | |
---|
| 4 | #include <math.h> |
---|
| 5 | |
---|
| 6 | #include "glwidget.h" |
---|
| 7 | |
---|
| 8 | GLWidget::GLWidget(QWidget *parent) |
---|
| 9 | : QGLWidget(parent) |
---|
| 10 | { |
---|
| 11 | object = 0; |
---|
| 12 | xRot = 0; |
---|
| 13 | yRot = 0; |
---|
| 14 | zRot = 0; |
---|
| 15 | |
---|
| 16 | trolltechGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0); |
---|
| 17 | trolltechPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0); |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | GLWidget::~GLWidget() |
---|
| 21 | { |
---|
| 22 | makeCurrent(); |
---|
| 23 | glDeleteLists(object, 1); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | QSize GLWidget::minimumSizeHint() const |
---|
| 27 | { |
---|
| 28 | return QSize(50, 50); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | QSize GLWidget::sizeHint() const |
---|
| 32 | { |
---|
| 33 | return QSize(400, 400); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void GLWidget::setXRotation(int angle) |
---|
| 37 | { |
---|
| 38 | normalizeAngle(&angle); |
---|
| 39 | if (angle != xRot) { |
---|
| 40 | xRot = angle; |
---|
| 41 | emit xRotationChanged(angle); |
---|
| 42 | updateGL(); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void GLWidget::setYRotation(int angle) |
---|
| 47 | { |
---|
| 48 | normalizeAngle(&angle); |
---|
| 49 | if (angle != yRot) { |
---|
| 50 | yRot = angle; |
---|
| 51 | emit yRotationChanged(angle); |
---|
| 52 | updateGL(); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void GLWidget::setZRotation(int angle) |
---|
| 57 | { |
---|
| 58 | normalizeAngle(&angle); |
---|
| 59 | if (angle != zRot) { |
---|
| 60 | zRot = angle; |
---|
| 61 | emit zRotationChanged(angle); |
---|
| 62 | updateGL(); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void GLWidget::initializeGL() |
---|
| 67 | { |
---|
| 68 | qglClearColor(trolltechPurple.dark()); |
---|
| 69 | object = makeObject(); |
---|
| 70 | glShadeModel(GL_FLAT); |
---|
| 71 | glEnable(GL_DEPTH_TEST); |
---|
| 72 | glEnable(GL_CULL_FACE); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | void GLWidget::paintGL() |
---|
| 76 | { |
---|
| 77 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 78 | glLoadIdentity(); |
---|
| 79 | glTranslated(0.0, 0.0, -10.0); |
---|
| 80 | glRotated(xRot / 16.0, 1.0, 0.0, 0.0); |
---|
| 81 | glRotated(yRot / 16.0, 0.0, 1.0, 0.0); |
---|
| 82 | glRotated(zRot / 16.0, 0.0, 0.0, 1.0); |
---|
| 83 | glCallList(object); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void GLWidget::resizeGL(int width, int height) |
---|
| 87 | { |
---|
| 88 | int side = qMin(width, height); |
---|
| 89 | glViewport((width - side) / 2, (height - side) / 2, side, side); |
---|
| 90 | |
---|
| 91 | glMatrixMode(GL_PROJECTION); |
---|
| 92 | glLoadIdentity(); |
---|
| 93 | glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); |
---|
| 94 | glMatrixMode(GL_MODELVIEW); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void GLWidget::mousePressEvent(QMouseEvent *event) |
---|
| 98 | { |
---|
| 99 | lastPos = event->pos(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void GLWidget::mouseMoveEvent(QMouseEvent *event) |
---|
| 103 | { |
---|
| 104 | int dx = event->x() - lastPos.x(); |
---|
| 105 | int dy = event->y() - lastPos.y(); |
---|
| 106 | |
---|
| 107 | if (event->buttons() & Qt::LeftButton) { |
---|
| 108 | setXRotation(xRot + 8 * dy); |
---|
| 109 | setYRotation(yRot + 8 * dx); |
---|
| 110 | } else if (event->buttons() & Qt::RightButton) { |
---|
| 111 | setXRotation(xRot + 8 * dy); |
---|
| 112 | setZRotation(zRot + 8 * dx); |
---|
| 113 | } |
---|
| 114 | lastPos = event->pos(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | GLuint GLWidget::makeObject() |
---|
| 118 | { |
---|
| 119 | GLuint list = glGenLists(1); |
---|
| 120 | glNewList(list, GL_COMPILE); |
---|
| 121 | |
---|
| 122 | glBegin(GL_QUADS); |
---|
| 123 | |
---|
| 124 | GLdouble x1 = +0.06; |
---|
| 125 | GLdouble y1 = -0.14; |
---|
| 126 | GLdouble x2 = +0.14; |
---|
| 127 | GLdouble y2 = -0.06; |
---|
| 128 | GLdouble x3 = +0.08; |
---|
| 129 | GLdouble y3 = +0.00; |
---|
| 130 | GLdouble x4 = +0.30; |
---|
| 131 | GLdouble y4 = +0.22; |
---|
| 132 | |
---|
| 133 | quad(x1, y1, x2, y2, y2, x2, y1, x1); |
---|
| 134 | quad(x3, y3, x4, y4, y4, x4, y3, x3); |
---|
| 135 | |
---|
| 136 | extrude(x1, y1, x2, y2); |
---|
| 137 | extrude(x2, y2, y2, x2); |
---|
| 138 | extrude(y2, x2, y1, x1); |
---|
| 139 | extrude(y1, x1, x1, y1); |
---|
| 140 | extrude(x3, y3, x4, y4); |
---|
| 141 | extrude(x4, y4, y4, x4); |
---|
| 142 | extrude(y4, x4, y3, x3); |
---|
| 143 | |
---|
| 144 | const double Pi = 3.14159265358979323846; |
---|
| 145 | const int NumSectors = 200; |
---|
| 146 | |
---|
| 147 | for (int i = 0; i < NumSectors; ++i) { |
---|
| 148 | double angle1 = (i * 2 * Pi) / NumSectors; |
---|
| 149 | GLdouble x5 = 0.30 * sin(angle1); |
---|
| 150 | GLdouble y5 = 0.30 * cos(angle1); |
---|
| 151 | GLdouble x6 = 0.20 * sin(angle1); |
---|
| 152 | GLdouble y6 = 0.20 * cos(angle1); |
---|
| 153 | |
---|
| 154 | double angle2 = ((i + 1) * 2 * Pi) / NumSectors; |
---|
| 155 | GLdouble x7 = 0.20 * sin(angle2); |
---|
| 156 | GLdouble y7 = 0.20 * cos(angle2); |
---|
| 157 | GLdouble x8 = 0.30 * sin(angle2); |
---|
| 158 | GLdouble y8 = 0.30 * cos(angle2); |
---|
| 159 | |
---|
| 160 | quad(x5, y5, x6, y6, x7, y7, x8, y8); |
---|
| 161 | |
---|
| 162 | extrude(x6, y6, x7, y7); |
---|
| 163 | extrude(x8, y8, x5, y5); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | glEnd(); |
---|
| 167 | |
---|
| 168 | glEndList(); |
---|
| 169 | return list; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | void GLWidget::quad(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2, |
---|
| 173 | GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4) |
---|
| 174 | { |
---|
| 175 | qglColor(trolltechGreen); |
---|
| 176 | |
---|
| 177 | glVertex3d(x1, y1, -0.05); |
---|
| 178 | glVertex3d(x2, y2, -0.05); |
---|
| 179 | glVertex3d(x3, y3, -0.05); |
---|
| 180 | glVertex3d(x4, y4, -0.05); |
---|
| 181 | |
---|
| 182 | glVertex3d(x4, y4, +0.05); |
---|
| 183 | glVertex3d(x3, y3, +0.05); |
---|
| 184 | glVertex3d(x2, y2, +0.05); |
---|
| 185 | glVertex3d(x1, y1, +0.05); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void GLWidget::extrude(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2) |
---|
| 189 | { |
---|
| 190 | qglColor(trolltechGreen.dark(250 + int(100 * x1))); |
---|
| 191 | |
---|
| 192 | glVertex3d(x1, y1, +0.05); |
---|
| 193 | glVertex3d(x2, y2, +0.05); |
---|
| 194 | glVertex3d(x2, y2, -0.05); |
---|
| 195 | glVertex3d(x1, y1, -0.05); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | void GLWidget::normalizeAngle(int *angle) |
---|
| 199 | { |
---|
| 200 | while (*angle < 0) |
---|
| 201 | *angle += 360 * 16; |
---|
| 202 | while (*angle > 360 * 16) |
---|
| 203 | *angle -= 360 * 16; |
---|
| 204 | } |
---|