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