1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
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 [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "glwidget.h"
|
---|
43 | #include <QPainter>
|
---|
44 | #include <math.h>
|
---|
45 |
|
---|
46 | #include "bubble.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | const int bubbleNum = 8;
|
---|
50 |
|
---|
51 | inline void CrossProduct(qreal &xOut, qreal &yOut, qreal &zOut, qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2)
|
---|
52 | {
|
---|
53 | xOut = y1 * z2 - z1 * y2;
|
---|
54 | yOut = z1 * x2 - x1 * z2;
|
---|
55 | zOut = x1 * y2 - y1 * x2;
|
---|
56 | }
|
---|
57 |
|
---|
58 | inline void Normalize(qreal &x, qreal &y, qreal &z)
|
---|
59 | {
|
---|
60 | qreal l = sqrt(x*x + y*y + z*z);
|
---|
61 | x = x / l;
|
---|
62 | y = y / l;
|
---|
63 | z = z / l;
|
---|
64 | }
|
---|
65 |
|
---|
66 | inline void IdentityMatrix(GLfloat *m)
|
---|
67 | {
|
---|
68 | m[0 * 4 + 0] = 1.0f;
|
---|
69 | m[1 * 4 + 0] = 0.0f;
|
---|
70 | m[2 * 4 + 0] = 0.0f;
|
---|
71 | m[3 * 4 + 0] = 0.0f;
|
---|
72 | m[0 * 4 + 1] = 0.0f;
|
---|
73 | m[1 * 4 + 1] = 1.0f;
|
---|
74 | m[2 * 4 + 1] = 0.0f;
|
---|
75 | m[3 * 4 + 1] = 0.0f;
|
---|
76 | m[0 * 4 + 2] = 0.0f;
|
---|
77 | m[1 * 4 + 2] = 0.0f;
|
---|
78 | m[2 * 4 + 2] = 1.0f;
|
---|
79 | m[3 * 4 + 2] = 0.0f;
|
---|
80 | m[0 * 4 + 3] = 0.0f;
|
---|
81 | m[1 * 4 + 3] = 0.0f;
|
---|
82 | m[2 * 4 + 3] = 0.0f;
|
---|
83 | m[3 * 4 + 3] = 1.0f;
|
---|
84 | }
|
---|
85 |
|
---|
86 | // Adjust a 4x4 matrix to apply a scale.
|
---|
87 | inline void ScaleMatrix(GLfloat *m, GLfloat scalex, GLfloat scaley, GLfloat scalez)
|
---|
88 | {
|
---|
89 | m[0 * 4 + 0] *= scalex;
|
---|
90 | m[0 * 4 + 1] *= scalex;
|
---|
91 | m[0 * 4 + 2] *= scalex;
|
---|
92 | m[0 * 4 + 3] *= scalex;
|
---|
93 | m[1 * 4 + 0] *= scaley;
|
---|
94 | m[1 * 4 + 1] *= scaley;
|
---|
95 | m[1 * 4 + 2] *= scaley;
|
---|
96 | m[1 * 4 + 3] *= scaley;
|
---|
97 | m[2 * 4 + 0] *= scalez;
|
---|
98 | m[2 * 4 + 1] *= scalez;
|
---|
99 | m[2 * 4 + 2] *= scalez;
|
---|
100 | m[2 * 4 + 3] *= scalez;
|
---|
101 | }
|
---|
102 |
|
---|
103 | // Adjust a 4x4 matrix to apply a translation.
|
---|
104 | inline void TranslateMatrix(GLfloat *m, GLfloat translatex, GLfloat translatey, GLfloat translatez)
|
---|
105 | {
|
---|
106 | m[3 * 4 + 0] += m[0 * 4 + 0] * translatex + m[1 * 4 + 0] * translatey + m[2 * 4 + 0] * translatez;
|
---|
107 | m[3 * 4 + 1] += m[0 * 4 + 1] * translatex + m[1 * 4 + 1] * translatey + m[2 * 4 + 1] * translatez;
|
---|
108 | m[3 * 4 + 2] += m[0 * 4 + 2] * translatex + m[1 * 4 + 2] * translatey + m[2 * 4 + 2] * translatez;
|
---|
109 | m[3 * 4 + 3] += m[0 * 4 + 3] * translatex + m[1 * 4 + 3] * translatey + m[2 * 4 + 3] * translatez;
|
---|
110 | }
|
---|
111 |
|
---|
112 | #ifndef M_PI
|
---|
113 | #define M_PI 3.14159265358979323846
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | // Adjust a 4x4 matrix to apply a rotation.
|
---|
117 | inline void RotateMatrix(GLfloat *m, GLfloat angle, GLfloat vx, GLfloat vy, GLfloat vz)
|
---|
118 | {
|
---|
119 | GLfloat len = sqrt(vx * vx + vy * vy + vz * vz);
|
---|
120 | if (len != 0) {
|
---|
121 | vx /= len;
|
---|
122 | vy /= len;
|
---|
123 | vz /= len;
|
---|
124 | }
|
---|
125 |
|
---|
126 | GLfloat c, s, ic;
|
---|
127 | c = cos(angle * M_PI / 180.0);
|
---|
128 | s = sin(angle * M_PI / 180.0);
|
---|
129 | ic = 1.0f - c;
|
---|
130 |
|
---|
131 | GLfloat rot[16];
|
---|
132 | rot[0 * 4 + 0] = vx * vx * ic + c;
|
---|
133 | rot[1 * 4 + 0] = vx * vy * ic - vz * s;
|
---|
134 | rot[2 * 4 + 0] = vx * vz * ic + vy * s;
|
---|
135 | rot[3 * 4 + 0] = 0.0f;
|
---|
136 | rot[0 * 4 + 1] = vy * vx * ic + vz * s;
|
---|
137 | rot[1 * 4 + 1] = vy * vy * ic + c;
|
---|
138 | rot[2 * 4 + 1] = vy * vz * ic - vx * s;
|
---|
139 | rot[3 * 4 + 1] = 0.0f;
|
---|
140 | rot[0 * 4 + 2] = vx * vz * ic - vy * s;
|
---|
141 | rot[1 * 4 + 2] = vy * vz * ic + vx * s;
|
---|
142 | rot[2 * 4 + 2] = vz * vz * ic + c;
|
---|
143 | rot[3 * 4 + 2] = 0.0f;
|
---|
144 | rot[0 * 4 + 3] = 0.0f;
|
---|
145 | rot[1 * 4 + 3] = 0.0f;
|
---|
146 | rot[2 * 4 + 3] = 0.0f;
|
---|
147 | rot[3 * 4 + 3] = 1.0f;
|
---|
148 |
|
---|
149 | GLfloat temp[16];
|
---|
150 | for (int i = 0; i < 4; ++i) {
|
---|
151 | for (int j = 0; j < 4; ++j) {
|
---|
152 | temp[j * 4 + i] = 0.0f;
|
---|
153 | for (int k = 0; k < 4; ++k) {
|
---|
154 | temp[j * 4 + i] += m[k * 4 + i] * rot[j * 4 + k];
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | qMemCopy(m, temp, sizeof(temp));
|
---|
160 | }
|
---|
161 |
|
---|
162 | GLWidget::GLWidget(QWidget *parent)
|
---|
163 | : QGLWidget(parent)
|
---|
164 | {
|
---|
165 | qtLogo = true;
|
---|
166 | createdVertices = 0;
|
---|
167 | createdNormals = 0;
|
---|
168 | m_vertexNumber = 0;
|
---|
169 | frames = 0;
|
---|
170 | setAttribute(Qt::WA_PaintOnScreen);
|
---|
171 | setAttribute(Qt::WA_NoSystemBackground);
|
---|
172 | setAutoBufferSwap(false);
|
---|
173 | m_showBubbles = true;
|
---|
174 | #ifndef Q_WS_QWS
|
---|
175 | setMinimumSize(300, 250);
|
---|
176 | #endif
|
---|
177 | }
|
---|
178 |
|
---|
179 | GLWidget::~GLWidget()
|
---|
180 | {
|
---|
181 | if (createdVertices)
|
---|
182 | delete[] createdVertices;
|
---|
183 | if (createdNormals)
|
---|
184 | delete[] createdNormals;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void GLWidget::setScaling(int scale) {
|
---|
188 |
|
---|
189 | if (scale > 50)
|
---|
190 | m_fScale = 1 + qreal(scale -50) / 50 * 0.5;
|
---|
191 | else if (scale < 50)
|
---|
192 | m_fScale = 1- (qreal(50 - scale) / 50 * 1/2);
|
---|
193 | else
|
---|
194 | m_fScale = 1;
|
---|
195 | }
|
---|
196 |
|
---|
197 | void GLWidget::setLogo() {
|
---|
198 | qtLogo = true;
|
---|
199 | }
|
---|
200 |
|
---|
201 | void GLWidget::setTexture() {
|
---|
202 | qtLogo = false;
|
---|
203 | }
|
---|
204 |
|
---|
205 | void GLWidget::showBubbles(bool bubbles)
|
---|
206 | {
|
---|
207 | m_showBubbles = bubbles;
|
---|
208 | }
|
---|
209 |
|
---|
210 | void GLWidget::paintQtLogo()
|
---|
211 | {
|
---|
212 | glDisable(GL_TEXTURE_2D);
|
---|
213 | glVertexAttribPointer(vertexAttr1, 3, GL_FLOAT, GL_FALSE, 0, createdVertices);
|
---|
214 | glEnableVertexAttribArray(vertexAttr1);
|
---|
215 | glVertexAttribPointer(normalAttr1, 3, GL_FLOAT, GL_FALSE, 0, createdNormals);
|
---|
216 | glEnableVertexAttribArray(normalAttr1);
|
---|
217 | glDrawArrays(GL_TRIANGLES, 0, m_vertexNumber / 3);
|
---|
218 | glDisableVertexAttribArray(normalAttr1);
|
---|
219 | glDisableVertexAttribArray(vertexAttr1);
|
---|
220 | }
|
---|
221 |
|
---|
222 | void GLWidget::paintTexturedCube()
|
---|
223 | {
|
---|
224 | glEnable(GL_TEXTURE_2D);
|
---|
225 | glBindTexture(GL_TEXTURE_2D, m_uiTexture);
|
---|
226 | GLfloat afVertices[] = {
|
---|
227 | -0.5, 0.5, 0.5, 0.5,-0.5,0.5,-0.5,-0.5,0.5,
|
---|
228 | 0.5, -0.5, 0.5, -0.5,0.5,0.5,0.5,0.5,0.5,
|
---|
229 | -0.5, -0.5, -0.5, 0.5,-0.5,-0.5,-0.5,0.5,-0.5,
|
---|
230 | 0.5, 0.5, -0.5, -0.5,0.5,-0.5,0.5,-0.5,-0.5,
|
---|
231 |
|
---|
232 | 0.5, -0.5, -0.5, 0.5,-0.5,0.5,0.5,0.5,-0.5,
|
---|
233 | 0.5, 0.5, 0.5, 0.5,0.5,-0.5,0.5,-0.5,0.5,
|
---|
234 | -0.5, 0.5, -0.5, -0.5,-0.5,0.5,-0.5,-0.5,-0.5,
|
---|
235 | -0.5, -0.5, 0.5, -0.5,0.5,-0.5,-0.5,0.5,0.5,
|
---|
236 |
|
---|
237 | 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5,
|
---|
238 | -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5,
|
---|
239 | -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5,
|
---|
240 | 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5
|
---|
241 | };
|
---|
242 | glVertexAttribPointer(vertexAttr2, 3, GL_FLOAT, GL_FALSE, 0, afVertices);
|
---|
243 | glEnableVertexAttribArray(vertexAttr2);
|
---|
244 |
|
---|
245 | GLfloat afTexCoord[] = {
|
---|
246 | 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
|
---|
247 | 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,
|
---|
248 | 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
|
---|
249 | 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,
|
---|
250 |
|
---|
251 | 1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
|
---|
252 | 0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,
|
---|
253 | 0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
|
---|
254 | 1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,
|
---|
255 |
|
---|
256 | 0.0f,1.0f, 1.0f,0.0f, 1.0f,1.0f,
|
---|
257 | 1.0f,0.0f, 0.0f,1.0f, 0.0f,0.0f,
|
---|
258 | 1.0f,0.0f, 1.0f,1.0f, 0.0f,0.0f,
|
---|
259 | 0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f
|
---|
260 | };
|
---|
261 | glVertexAttribPointer(texCoordAttr2, 2, GL_FLOAT, GL_FALSE, 0, afTexCoord);
|
---|
262 | glEnableVertexAttribArray(texCoordAttr2);
|
---|
263 |
|
---|
264 | GLfloat afNormals[] = {
|
---|
265 |
|
---|
266 | 0,0,-1, 0,0,-1, 0,0,-1,
|
---|
267 | 0,0,-1, 0,0,-1, 0,0,-1,
|
---|
268 | 0,0,1, 0,0,1, 0,0,1,
|
---|
269 | 0,0,1, 0,0,1, 0,0,1,
|
---|
270 |
|
---|
271 | -1,0,0, -1,0,0, -1,0,0,
|
---|
272 | -1,0,0, -1,0,0, -1,0,0,
|
---|
273 | 1,0,0, 1,0,0, 1,0,0,
|
---|
274 | 1,0,0, 1,0,0, 1,0,0,
|
---|
275 |
|
---|
276 | 0,-1,0, 0,-1,0, 0,-1,0,
|
---|
277 | 0,-1,0, 0,-1,0, 0,-1,0,
|
---|
278 | 0,1,0, 0,1,0, 0,1,0,
|
---|
279 | 0,1,0, 0,1,0, 0,1,0
|
---|
280 | };
|
---|
281 | glVertexAttribPointer(normalAttr2, 3, GL_FLOAT, GL_FALSE, 0, afNormals);
|
---|
282 | glEnableVertexAttribArray(normalAttr2);
|
---|
283 |
|
---|
284 | glUniform1i(textureUniform2, 0); // use texture unit 0
|
---|
285 |
|
---|
286 | glDrawArrays(GL_TRIANGLES, 0, 36);
|
---|
287 |
|
---|
288 | glDisableVertexAttribArray(vertexAttr2);
|
---|
289 | glDisableVertexAttribArray(normalAttr2);
|
---|
290 | glDisableVertexAttribArray(texCoordAttr2);
|
---|
291 | }
|
---|
292 |
|
---|
293 | static void reportCompileErrors(GLuint shader, const char *src)
|
---|
294 | {
|
---|
295 | GLint value = 0;
|
---|
296 | glGetShaderiv(shader, GL_COMPILE_STATUS, &value);
|
---|
297 | bool compiled = (value != 0);
|
---|
298 | value = 0;
|
---|
299 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &value);
|
---|
300 | if (!compiled && value > 1) {
|
---|
301 | char *log = new char [value];
|
---|
302 | GLint len;
|
---|
303 | glGetShaderInfoLog(shader, value, &len, log);
|
---|
304 | qWarning("%s\n", log);
|
---|
305 | qWarning("when compiling:\n%s\n", src);
|
---|
306 | delete [] log;
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | static void reportLinkErrors(GLuint program, const char *vsrc, const char *fsrc)
|
---|
311 | {
|
---|
312 | GLint value = 0;
|
---|
313 | glGetProgramiv(program, GL_LINK_STATUS, &value);
|
---|
314 | bool linked = (value != 0);
|
---|
315 | value = 0;
|
---|
316 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value);
|
---|
317 | if (!linked && value > 1) {
|
---|
318 | char *log = new char [value];
|
---|
319 | GLint len;
|
---|
320 | glGetProgramInfoLog(program, value, &len, log);
|
---|
321 | qWarning("%s\n", log);
|
---|
322 | qWarning("when linking:\n%s\nwith:\n%s\n", vsrc, fsrc);
|
---|
323 | delete [] log;
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | void GLWidget::initializeGL ()
|
---|
328 | {
|
---|
329 | glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
|
---|
330 |
|
---|
331 | glEnable(GL_TEXTURE_2D);
|
---|
332 | glGenTextures(1, &m_uiTexture);
|
---|
333 | m_uiTexture = bindTexture(QImage(":/qt.png"));
|
---|
334 |
|
---|
335 | GLuint vshader1 = glCreateShader(GL_VERTEX_SHADER);
|
---|
336 | const char *vsrc1[1] = {
|
---|
337 | "attribute highp vec4 vertex;\n"
|
---|
338 | "attribute mediump vec3 normal;\n"
|
---|
339 | "uniform mediump mat4 matrix;\n"
|
---|
340 | "varying mediump vec4 color;\n"
|
---|
341 | "void main(void)\n"
|
---|
342 | "{\n"
|
---|
343 | " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
|
---|
344 | " float angle = max(dot(normal, toLight), 0.0);\n"
|
---|
345 | " vec3 col = vec3(0.40, 1.0, 0.0);\n"
|
---|
346 | " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);\n"
|
---|
347 | " color = clamp(color, 0.0, 1.0);\n"
|
---|
348 | " gl_Position = matrix * vertex;\n"
|
---|
349 | "}\n"
|
---|
350 | };
|
---|
351 | glShaderSource(vshader1, 1, vsrc1, 0);
|
---|
352 | glCompileShader(vshader1);
|
---|
353 | reportCompileErrors(vshader1, vsrc1[0]);
|
---|
354 |
|
---|
355 | GLuint fshader1 = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
356 | const char *fsrc1[1] = {
|
---|
357 | "varying mediump vec4 color;\n"
|
---|
358 | "void main(void)\n"
|
---|
359 | "{\n"
|
---|
360 | " gl_FragColor = color;\n"
|
---|
361 | "}\n"
|
---|
362 | };
|
---|
363 | glShaderSource(fshader1, 1, fsrc1, 0);
|
---|
364 | glCompileShader(fshader1);
|
---|
365 | reportCompileErrors(fshader1, fsrc1[0]);
|
---|
366 |
|
---|
367 | program1 = glCreateProgram();
|
---|
368 | glAttachShader(program1, vshader1);
|
---|
369 | glAttachShader(program1, fshader1);
|
---|
370 | glLinkProgram(program1);
|
---|
371 | reportLinkErrors(program1, vsrc1[0], fsrc1[0]);
|
---|
372 |
|
---|
373 | vertexAttr1 = glGetAttribLocation(program1, "vertex");
|
---|
374 | normalAttr1 = glGetAttribLocation(program1, "normal");
|
---|
375 | matrixUniform1 = glGetUniformLocation(program1, "matrix");
|
---|
376 |
|
---|
377 | GLuint vshader2 = glCreateShader(GL_VERTEX_SHADER);
|
---|
378 | const char *vsrc2[1] = {
|
---|
379 | "attribute highp vec4 vertex;\n"
|
---|
380 | "attribute highp vec4 texCoord;\n"
|
---|
381 | "attribute mediump vec3 normal;\n"
|
---|
382 | "uniform mediump mat4 matrix;\n"
|
---|
383 | "varying highp vec4 texc;\n"
|
---|
384 | "varying mediump float angle;\n"
|
---|
385 | "void main(void)\n"
|
---|
386 | "{\n"
|
---|
387 | " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n"
|
---|
388 | " angle = max(dot(normal, toLight), 0.0);\n"
|
---|
389 | " gl_Position = matrix * vertex;\n"
|
---|
390 | " texc = texCoord;\n"
|
---|
391 | "}\n"
|
---|
392 | };
|
---|
393 | glShaderSource(vshader2, 1, vsrc2, 0);
|
---|
394 | glCompileShader(vshader2);
|
---|
395 | reportCompileErrors(vshader2, vsrc2[0]);
|
---|
396 |
|
---|
397 | GLuint fshader2 = glCreateShader(GL_FRAGMENT_SHADER);
|
---|
398 | const char *fsrc2[1] = {
|
---|
399 | "varying highp vec4 texc;\n"
|
---|
400 | "uniform sampler2D tex;\n"
|
---|
401 | "varying mediump float angle;\n"
|
---|
402 | "void main(void)\n"
|
---|
403 | "{\n"
|
---|
404 | " highp vec3 color = texture2D(tex, texc.st).rgb;\n"
|
---|
405 | " color = color * 0.2 + color * 0.8 * angle;\n"
|
---|
406 | " gl_FragColor = vec4(clamp(color, 0.0, 1.0), 1.0);\n"
|
---|
407 | "}\n"
|
---|
408 | };
|
---|
409 | glShaderSource(fshader2, 1, fsrc2, 0);
|
---|
410 | glCompileShader(fshader2);
|
---|
411 | reportCompileErrors(fshader2, fsrc2[0]);
|
---|
412 |
|
---|
413 | program2 = glCreateProgram();
|
---|
414 | glAttachShader(program2, vshader2);
|
---|
415 | glAttachShader(program2, fshader2);
|
---|
416 | glLinkProgram(program2);
|
---|
417 | reportLinkErrors(program2, vsrc2[0], fsrc2[0]);
|
---|
418 |
|
---|
419 | vertexAttr2 = glGetAttribLocation(program2, "vertex");
|
---|
420 | normalAttr2 = glGetAttribLocation(program2, "normal");
|
---|
421 | texCoordAttr2 = glGetAttribLocation(program2, "texCoord");
|
---|
422 | matrixUniform2 = glGetUniformLocation(program2, "matrix");
|
---|
423 | textureUniform2 = glGetUniformLocation(program2, "tex");
|
---|
424 |
|
---|
425 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
426 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
427 |
|
---|
428 | m_fAngle = 0;
|
---|
429 | m_fScale = 1;
|
---|
430 | createGeometry();
|
---|
431 | createBubbles(bubbleNum - bubbles.count());
|
---|
432 | }
|
---|
433 |
|
---|
434 | void GLWidget::paintGL()
|
---|
435 | {
|
---|
436 | createBubbles(bubbleNum - bubbles.count());
|
---|
437 |
|
---|
438 | QPainter painter;
|
---|
439 | painter.begin(this);
|
---|
440 |
|
---|
441 | glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
|
---|
442 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
443 | glEnable(GL_TEXTURE_2D);
|
---|
444 |
|
---|
445 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
446 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
447 |
|
---|
448 | glFrontFace(GL_CW);
|
---|
449 | glCullFace(GL_FRONT);
|
---|
450 | glEnable(GL_CULL_FACE);
|
---|
451 | glEnable(GL_DEPTH_TEST);
|
---|
452 |
|
---|
453 | GLfloat modelview[16];
|
---|
454 | IdentityMatrix(modelview);
|
---|
455 | RotateMatrix(modelview, m_fAngle, 0.0, 1.0, 0.0);
|
---|
456 | RotateMatrix(modelview, m_fAngle, 1.0, 0.0, 0.0);
|
---|
457 | RotateMatrix(modelview, m_fAngle, 0.0, 0.0, 1.0);
|
---|
458 | ScaleMatrix(modelview, m_fScale, m_fScale, m_fScale);
|
---|
459 | TranslateMatrix(modelview, 0, -0.2, 0);
|
---|
460 |
|
---|
461 | if (qtLogo) {
|
---|
462 | glUseProgram(program1);
|
---|
463 | glUniformMatrix4fv(matrixUniform1, 1, GL_FALSE, modelview);
|
---|
464 | paintQtLogo();
|
---|
465 | glUseProgram(0);
|
---|
466 | } else {
|
---|
467 | glUseProgram(program2);
|
---|
468 | glUniformMatrix4fv(matrixUniform2, 1, GL_FALSE, modelview);
|
---|
469 | paintTexturedCube();
|
---|
470 | glUseProgram(0);
|
---|
471 | }
|
---|
472 |
|
---|
473 | glDisable(GL_DEPTH_TEST);
|
---|
474 | glDisable(GL_CULL_FACE);
|
---|
475 |
|
---|
476 | if (m_showBubbles)
|
---|
477 | foreach (Bubble *bubble, bubbles) {
|
---|
478 | bubble->drawBubble(&painter);
|
---|
479 | }
|
---|
480 |
|
---|
481 | QString framesPerSecond;
|
---|
482 | framesPerSecond.setNum(frames /(time.elapsed() / 1000.0), 'f', 2);
|
---|
483 |
|
---|
484 | painter.setPen(Qt::white);
|
---|
485 |
|
---|
486 | painter.drawText(20, 40, framesPerSecond + " fps");
|
---|
487 |
|
---|
488 | painter.end();
|
---|
489 |
|
---|
490 | swapBuffers();
|
---|
491 |
|
---|
492 | QMutableListIterator<Bubble*> iter(bubbles);
|
---|
493 |
|
---|
494 | while (iter.hasNext()) {
|
---|
495 | Bubble *bubble = iter.next();
|
---|
496 | bubble->move(rect());
|
---|
497 | }
|
---|
498 | if (!(frames % 100)) {
|
---|
499 | time.start();
|
---|
500 | frames = 0;
|
---|
501 | }
|
---|
502 | m_fAngle += 1.0f;
|
---|
503 | frames ++;
|
---|
504 | }
|
---|
505 |
|
---|
506 | void GLWidget::createBubbles(int number)
|
---|
507 | {
|
---|
508 | for (int i = 0; i < number; ++i) {
|
---|
509 | QPointF position(width()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))),
|
---|
510 | height()*(0.1 + (0.8*qrand()/(RAND_MAX+1.0))));
|
---|
511 | qreal radius = qMin(width(), height())*(0.0175 + 0.0875*qrand()/(RAND_MAX+1.0));
|
---|
512 | QPointF velocity(width()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0)),
|
---|
513 | height()*0.0175*(-0.5 + qrand()/(RAND_MAX+1.0)));
|
---|
514 |
|
---|
515 | bubbles.append(new Bubble(position, radius, velocity));
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | void GLWidget::createGeometry()
|
---|
520 | {
|
---|
521 | vertices.clear();
|
---|
522 | normals.clear();
|
---|
523 |
|
---|
524 | qreal x1 = +0.06f;
|
---|
525 | qreal y1 = -0.14f;
|
---|
526 | qreal x2 = +0.14f;
|
---|
527 | qreal y2 = -0.06f;
|
---|
528 | qreal x3 = +0.08f;
|
---|
529 | qreal y3 = +0.00f;
|
---|
530 | qreal x4 = +0.30f;
|
---|
531 | qreal y4 = +0.22f;
|
---|
532 |
|
---|
533 | quad(x1, y1, x2, y2, y2, x2, y1, x1);
|
---|
534 | quad(x3, y3, x4, y4, y4, x4, y3, x3);
|
---|
535 |
|
---|
536 | extrude(x1, y1, x2, y2);
|
---|
537 | extrude(x2, y2, y2, x2);
|
---|
538 | extrude(y2, x2, y1, x1);
|
---|
539 | extrude(y1, x1, x1, y1);
|
---|
540 | extrude(x3, y3, x4, y4);
|
---|
541 | extrude(x4, y4, y4, x4);
|
---|
542 | extrude(y4, x4, y3, x3);
|
---|
543 |
|
---|
544 | const qreal Pi = 3.14159f;
|
---|
545 | const int NumSectors = 100;
|
---|
546 |
|
---|
547 | for (int i = 0; i < NumSectors; ++i) {
|
---|
548 | qreal angle1 = (i * 2 * Pi) / NumSectors;
|
---|
549 | qreal x5 = 0.30 * sin(angle1);
|
---|
550 | qreal y5 = 0.30 * cos(angle1);
|
---|
551 | qreal x6 = 0.20 * sin(angle1);
|
---|
552 | qreal y6 = 0.20 * cos(angle1);
|
---|
553 |
|
---|
554 | qreal angle2 = ((i + 1) * 2 * Pi) / NumSectors;
|
---|
555 | qreal x7 = 0.20 * sin(angle2);
|
---|
556 | qreal y7 = 0.20 * cos(angle2);
|
---|
557 | qreal x8 = 0.30 * sin(angle2);
|
---|
558 | qreal y8 = 0.30 * cos(angle2);
|
---|
559 |
|
---|
560 | quad(x5, y5, x6, y6, x7, y7, x8, y8);
|
---|
561 |
|
---|
562 | extrude(x6, y6, x7, y7);
|
---|
563 | extrude(x8, y8, x5, y5);
|
---|
564 | }
|
---|
565 |
|
---|
566 | m_vertexNumber = vertices.size();
|
---|
567 | createdVertices = new GLfloat[m_vertexNumber];
|
---|
568 | createdNormals = new GLfloat[m_vertexNumber];
|
---|
569 | for (int i = 0;i < m_vertexNumber;i++) {
|
---|
570 | createdVertices[i] = vertices.at(i) * 2;
|
---|
571 | createdNormals[i] = normals.at(i);
|
---|
572 | }
|
---|
573 | vertices.clear();
|
---|
574 | normals.clear();
|
---|
575 | }
|
---|
576 |
|
---|
577 | void GLWidget::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
|
---|
578 | {
|
---|
579 | qreal nx, ny, nz;
|
---|
580 |
|
---|
581 | vertices << x1 << y1 << -0.05f;
|
---|
582 | vertices << x2 << y2 << -0.05f;
|
---|
583 | vertices << x4 << y4 << -0.05f;
|
---|
584 |
|
---|
585 | vertices << x3 << y3 << -0.05f;
|
---|
586 | vertices << x4 << y4 << -0.05f;
|
---|
587 | vertices << x2 << y2 << -0.05f;
|
---|
588 |
|
---|
589 | CrossProduct(nx, ny, nz, x2 - x1, y2 - y1, 0, x4 - x1, y4 - y1, 0);
|
---|
590 | Normalize(nx, ny, nz);
|
---|
591 |
|
---|
592 | normals << nx << ny << nz;
|
---|
593 | normals << nx << ny << nz;
|
---|
594 | normals << nx << ny << nz;
|
---|
595 |
|
---|
596 | normals << nx << ny << nz;
|
---|
597 | normals << nx << ny << nz;
|
---|
598 | normals << nx << ny << nz;
|
---|
599 |
|
---|
600 | vertices << x4 << y4 << 0.05f;
|
---|
601 | vertices << x2 << y2 << 0.05f;
|
---|
602 | vertices << x1 << y1 << 0.05f;
|
---|
603 |
|
---|
604 | vertices << x2 << y2 << 0.05f;
|
---|
605 | vertices << x4 << y4 << 0.05f;
|
---|
606 | vertices << x3 << y3 << 0.05f;
|
---|
607 |
|
---|
608 | CrossProduct(nx, ny, nz, x2 - x4, y2 - y4, 0, x1 - x4, y1 - y4, 0);
|
---|
609 | Normalize(nx, ny, nz);
|
---|
610 |
|
---|
611 | normals << nx << ny << nz;
|
---|
612 | normals << nx << ny << nz;
|
---|
613 | normals << nx << ny << nz;
|
---|
614 |
|
---|
615 | normals << nx << ny << nz;
|
---|
616 | normals << nx << ny << nz;
|
---|
617 | normals << nx << ny << nz;
|
---|
618 | }
|
---|
619 |
|
---|
620 | void GLWidget::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
|
---|
621 | {
|
---|
622 | qreal nx, ny, nz;
|
---|
623 |
|
---|
624 | vertices << x1 << y1 << +0.05f;
|
---|
625 | vertices << x2 << y2 << +0.05f;
|
---|
626 | vertices << x1 << y1 << -0.05f;
|
---|
627 |
|
---|
628 | vertices << x2 << y2 << -0.05f;
|
---|
629 | vertices << x1 << y1 << -0.05f;
|
---|
630 | vertices << x2 << y2 << +0.05f;
|
---|
631 |
|
---|
632 | CrossProduct(nx, ny, nz, x2 - x1, y2 - y1, 0.0f, 0.0f, 0.0f, -0.1f);
|
---|
633 | Normalize(nx, ny, nz);
|
---|
634 |
|
---|
635 | normals << nx << ny << nz;
|
---|
636 | normals << nx << ny << nz;
|
---|
637 | normals << nx << ny << nz;
|
---|
638 |
|
---|
639 | normals << nx << ny << nz;
|
---|
640 | normals << nx << ny << nz;
|
---|
641 | normals << nx << ny << nz;
|
---|
642 | }
|
---|