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 <QtGui/QImage>
|
---|
43 | #include "glwidget.h"
|
---|
44 |
|
---|
45 | #include <math.h>
|
---|
46 |
|
---|
47 | #ifndef GL_MULTISAMPLE
|
---|
48 | #define GL_MULTISAMPLE 0x809D
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | GLWidget::GLWidget(QWidget *parent)
|
---|
52 | : QGLWidget(QGLFormat(QGL::SampleBuffers|QGL::AlphaChannel), parent)
|
---|
53 | {
|
---|
54 | setWindowTitle(tr("OpenGL framebuffer objects"));
|
---|
55 | makeCurrent();
|
---|
56 | fbo = new QGLFramebufferObject(1024, 1024);
|
---|
57 | rot_x = rot_y = rot_z = 0.0f;
|
---|
58 | scale = 0.1f;
|
---|
59 | anim = new QTimeLine(750, this);
|
---|
60 | anim->setUpdateInterval(20);
|
---|
61 | connect(anim, SIGNAL(valueChanged(qreal)), SLOT(animate(qreal)));
|
---|
62 | connect(anim, SIGNAL(finished()), SLOT(animFinished()));
|
---|
63 |
|
---|
64 | svg_renderer = new QSvgRenderer(QLatin1String(":/res/bubbles.svg"), this);
|
---|
65 | connect(svg_renderer, SIGNAL(repaintNeeded()), this, SLOT(draw()));
|
---|
66 |
|
---|
67 | logo = QImage(":/res/designer.png");
|
---|
68 | logo = logo.convertToFormat(QImage::Format_ARGB32);
|
---|
69 |
|
---|
70 | tile_list = glGenLists(1);
|
---|
71 | glNewList(tile_list, GL_COMPILE);
|
---|
72 | glBegin(GL_QUADS);
|
---|
73 | {
|
---|
74 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
|
---|
75 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
|
---|
76 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
|
---|
77 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
|
---|
78 |
|
---|
79 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
|
---|
80 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
|
---|
81 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
|
---|
82 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
|
---|
83 |
|
---|
84 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
|
---|
85 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
|
---|
86 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
|
---|
87 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
|
---|
88 |
|
---|
89 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
|
---|
90 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
|
---|
91 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
|
---|
92 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
|
---|
93 |
|
---|
94 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
|
---|
95 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
|
---|
96 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
|
---|
97 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
|
---|
98 |
|
---|
99 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
|
---|
100 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
|
---|
101 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
|
---|
102 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
|
---|
103 | }
|
---|
104 | glEnd();
|
---|
105 | glEndList();
|
---|
106 |
|
---|
107 | wave = new GLfloat[logo.width()*logo.height()];
|
---|
108 | memset(wave, 0, logo.width()*logo.height());
|
---|
109 | startTimer(30); // wave timer
|
---|
110 | }
|
---|
111 |
|
---|
112 | GLWidget::~GLWidget()
|
---|
113 | {
|
---|
114 | delete[] wave;
|
---|
115 | glDeleteLists(tile_list, 1);
|
---|
116 | delete fbo;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void GLWidget::paintEvent(QPaintEvent *)
|
---|
120 | {
|
---|
121 | draw();
|
---|
122 | }
|
---|
123 |
|
---|
124 | void GLWidget::draw()
|
---|
125 | {
|
---|
126 | QPainter p(this); // used for text overlay
|
---|
127 |
|
---|
128 | // save the GL state set for QPainter
|
---|
129 | saveGLState();
|
---|
130 |
|
---|
131 | // render the 'bubbles.svg' file into our framebuffer object
|
---|
132 | QPainter fbo_painter(fbo);
|
---|
133 | svg_renderer->render(&fbo_painter);
|
---|
134 | fbo_painter.end();
|
---|
135 |
|
---|
136 | // draw into the GL widget
|
---|
137 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
138 | glMatrixMode(GL_PROJECTION);
|
---|
139 | glLoadIdentity();
|
---|
140 | glFrustum(-1, 1, -1, 1, 10, 100);
|
---|
141 | glTranslatef(0.0f, 0.0f, -15.0f);
|
---|
142 | glMatrixMode(GL_MODELVIEW);
|
---|
143 | glLoadIdentity();
|
---|
144 | glViewport(0, 0, width(), height());
|
---|
145 | glEnable(GL_BLEND);
|
---|
146 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
147 |
|
---|
148 | glBindTexture(GL_TEXTURE_2D, fbo->texture());
|
---|
149 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
150 | glEnable(GL_TEXTURE_2D);
|
---|
151 | glEnable(GL_MULTISAMPLE);
|
---|
152 | glEnable(GL_CULL_FACE);
|
---|
153 |
|
---|
154 | // draw background
|
---|
155 | glPushMatrix();
|
---|
156 | glScalef(1.7f, 1.7f, 1.7f);
|
---|
157 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
158 | glCallList(tile_list);
|
---|
159 | glPopMatrix();
|
---|
160 |
|
---|
161 | const int w = logo.width();
|
---|
162 | const int h = logo.height();
|
---|
163 |
|
---|
164 | glRotatef(rot_x, 1.0f, 0.0f, 0.0f);
|
---|
165 | glRotatef(rot_y, 0.0f, 1.0f, 0.0f);
|
---|
166 | glRotatef(rot_z, 0.0f, 0.0f, 1.0f);
|
---|
167 | glScalef(scale/w, scale/w, scale/w);
|
---|
168 |
|
---|
169 | glDepthFunc(GL_LESS);
|
---|
170 | glEnable(GL_DEPTH_TEST);
|
---|
171 | // draw the Qt icon
|
---|
172 | glTranslatef(-w+1, -h+1, 0.0f);
|
---|
173 | for (int y=h-1; y>=0; --y) {
|
---|
174 | uint *p = (uint*) logo.scanLine(y);
|
---|
175 | uint *end = p + w;
|
---|
176 | int x = 0;
|
---|
177 | while (p < end) {
|
---|
178 | glColor4ub(qRed(*p), qGreen(*p), qBlue(*p), uchar(qAlpha(*p)*.9));
|
---|
179 | glTranslatef(0.0f, 0.0f, wave[y*w+x]);
|
---|
180 | if (qAlpha(*p) > 128)
|
---|
181 | glCallList(tile_list);
|
---|
182 | glTranslatef(0.0f, 0.0f, -wave[y*w+x]);
|
---|
183 | glTranslatef(2.0f, 0.0f, 0.0f);
|
---|
184 | ++x;
|
---|
185 | ++p;
|
---|
186 | }
|
---|
187 | glTranslatef(-w*2.0f, 2.0f, 0.0f);
|
---|
188 | }
|
---|
189 |
|
---|
190 | // restore the GL state that QPainter expects
|
---|
191 | restoreGLState();
|
---|
192 |
|
---|
193 | // draw the overlayed text using QPainter
|
---|
194 | p.setPen(QColor(197, 197, 197, 157));
|
---|
195 | p.setBrush(QColor(197, 197, 197, 127));
|
---|
196 | p.drawRect(QRect(0, 0, width(), 50));
|
---|
197 | p.setPen(Qt::black);
|
---|
198 | p.setBrush(Qt::NoBrush);
|
---|
199 | const QString str1(tr("A simple OpenGL framebuffer object example."));
|
---|
200 | const QString str2(tr("Use the mouse wheel to zoom, press buttons and move mouse to rotate, double-click to flip."));
|
---|
201 | QFontMetrics fm(p.font());
|
---|
202 | p.drawText(width()/2 - fm.width(str1)/2, 20, str1);
|
---|
203 | p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2);
|
---|
204 | }
|
---|
205 |
|
---|
206 | void GLWidget::mousePressEvent(QMouseEvent *e)
|
---|
207 | {
|
---|
208 | anchor = e->pos();
|
---|
209 | }
|
---|
210 |
|
---|
211 | void GLWidget::mouseMoveEvent(QMouseEvent *e)
|
---|
212 | {
|
---|
213 | QPoint diff = e->pos() - anchor;
|
---|
214 | if (e->buttons() & Qt::LeftButton) {
|
---|
215 | rot_x += diff.y()/5.0f;
|
---|
216 | rot_y += diff.x()/5.0f;
|
---|
217 | } else if (e->buttons() & Qt::RightButton) {
|
---|
218 | rot_z += diff.x()/5.0f;
|
---|
219 | }
|
---|
220 |
|
---|
221 | anchor = e->pos();
|
---|
222 | draw();
|
---|
223 | }
|
---|
224 |
|
---|
225 | void GLWidget::wheelEvent(QWheelEvent *e)
|
---|
226 | {
|
---|
227 | e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f;
|
---|
228 | draw();
|
---|
229 | }
|
---|
230 |
|
---|
231 | void GLWidget::mouseDoubleClickEvent(QMouseEvent *)
|
---|
232 | {
|
---|
233 | anim->start();
|
---|
234 | }
|
---|
235 |
|
---|
236 | void GLWidget::animate(qreal val)
|
---|
237 | {
|
---|
238 | rot_y = val * 180;
|
---|
239 | draw();
|
---|
240 | }
|
---|
241 |
|
---|
242 | void GLWidget::animFinished()
|
---|
243 | {
|
---|
244 | if (anim->direction() == QTimeLine::Forward)
|
---|
245 | anim->setDirection(QTimeLine::Backward);
|
---|
246 | else
|
---|
247 | anim->setDirection(QTimeLine::Forward);
|
---|
248 | }
|
---|
249 |
|
---|
250 | void GLWidget::saveGLState()
|
---|
251 | {
|
---|
252 | glPushAttrib(GL_ALL_ATTRIB_BITS);
|
---|
253 | glMatrixMode(GL_PROJECTION);
|
---|
254 | glPushMatrix();
|
---|
255 | glMatrixMode(GL_MODELVIEW);
|
---|
256 | glPushMatrix();
|
---|
257 | }
|
---|
258 |
|
---|
259 | void GLWidget::restoreGLState()
|
---|
260 | {
|
---|
261 | glMatrixMode(GL_PROJECTION);
|
---|
262 | glPopMatrix();
|
---|
263 | glMatrixMode(GL_MODELVIEW);
|
---|
264 | glPopMatrix();
|
---|
265 | glPopAttrib();
|
---|
266 | }
|
---|
267 |
|
---|
268 | #define PI 3.14159
|
---|
269 |
|
---|
270 | void GLWidget::timerEvent(QTimerEvent *)
|
---|
271 | {
|
---|
272 | if (QApplication::mouseButtons() != 0)
|
---|
273 | return;
|
---|
274 |
|
---|
275 | static bool scale_in = true;
|
---|
276 |
|
---|
277 | if (scale_in && scale > 35.0f)
|
---|
278 | scale_in = false;
|
---|
279 | else if (!scale_in && scale < .5f)
|
---|
280 | scale_in = true;
|
---|
281 |
|
---|
282 | scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f;
|
---|
283 | rot_z += 0.3f;
|
---|
284 | rot_x += 0.1f;
|
---|
285 |
|
---|
286 | int dx, dy; // disturbance point
|
---|
287 | float s, v, W, t;
|
---|
288 | int i, j;
|
---|
289 | static float wt[128][128];
|
---|
290 | const int width = logo.width();
|
---|
291 | const int AMP = 5;
|
---|
292 |
|
---|
293 | dx = dy = width >> 1;
|
---|
294 |
|
---|
295 | W = .3f;
|
---|
296 | v = -4; // wave speed
|
---|
297 |
|
---|
298 | for (i = 0; i < width; ++i) {
|
---|
299 | for ( j = 0; j < width; ++j) {
|
---|
300 | s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy)));
|
---|
301 | wt[i][j] += 0.1f;
|
---|
302 | t = s / v;
|
---|
303 | if (s != 0)
|
---|
304 | wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2));
|
---|
305 | else
|
---|
306 | wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t));
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|