1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui/QImage>
|
---|
42 | #include "glwidget.h"
|
---|
43 |
|
---|
44 | #include <math.h>
|
---|
45 |
|
---|
46 | #ifndef GL_MULTISAMPLE
|
---|
47 | #define GL_MULTISAMPLE 0x809D
|
---|
48 | #endif
|
---|
49 |
|
---|
50 | GLWidget::GLWidget(QWidget *parent)
|
---|
51 | : QGLWidget(QGLFormat(QGL::SampleBuffers|QGL::AlphaChannel), parent)
|
---|
52 | {
|
---|
53 | setWindowTitle(tr("OpenGL framebuffer objects"));
|
---|
54 | makeCurrent();
|
---|
55 |
|
---|
56 | if (QGLFramebufferObject::hasOpenGLFramebufferBlit()) {
|
---|
57 | QGLFramebufferObjectFormat format;
|
---|
58 | format.setSamples(4);
|
---|
59 | format.setAttachment(QGLFramebufferObject::CombinedDepthStencil);
|
---|
60 |
|
---|
61 | render_fbo = new QGLFramebufferObject(512, 512, format);
|
---|
62 | texture_fbo = new QGLFramebufferObject(512, 512);
|
---|
63 | } else {
|
---|
64 | render_fbo = new QGLFramebufferObject(1024, 1024);
|
---|
65 | texture_fbo = render_fbo;
|
---|
66 | }
|
---|
67 |
|
---|
68 | rot_x = rot_y = rot_z = 0.0f;
|
---|
69 | scale = 0.1f;
|
---|
70 | anim = new QTimeLine(750, this);
|
---|
71 | anim->setUpdateInterval(20);
|
---|
72 | connect(anim, SIGNAL(valueChanged(qreal)), SLOT(animate(qreal)));
|
---|
73 | connect(anim, SIGNAL(finished()), SLOT(animFinished()));
|
---|
74 |
|
---|
75 | svg_renderer = new QSvgRenderer(QLatin1String(":/res/bubbles.svg"), this);
|
---|
76 | connect(svg_renderer, SIGNAL(repaintNeeded()), this, SLOT(draw()));
|
---|
77 |
|
---|
78 | logo = QImage(":/res/designer.png");
|
---|
79 | logo = logo.convertToFormat(QImage::Format_ARGB32);
|
---|
80 |
|
---|
81 | tile_list = glGenLists(1);
|
---|
82 | glNewList(tile_list, GL_COMPILE);
|
---|
83 | glBegin(GL_QUADS);
|
---|
84 | {
|
---|
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 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
|
---|
89 |
|
---|
90 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
|
---|
91 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
|
---|
92 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
|
---|
93 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
|
---|
94 |
|
---|
95 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
|
---|
96 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
|
---|
97 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
|
---|
98 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
|
---|
99 |
|
---|
100 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
|
---|
101 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
|
---|
102 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
|
---|
103 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
|
---|
104 |
|
---|
105 | glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
|
---|
106 | glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
|
---|
107 | glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
|
---|
108 | glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
|
---|
109 |
|
---|
110 | glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
|
---|
111 | glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
|
---|
112 | glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
|
---|
113 | glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
|
---|
114 | }
|
---|
115 | glEnd();
|
---|
116 | glEndList();
|
---|
117 |
|
---|
118 | wave = new GLfloat[logo.width()*logo.height()];
|
---|
119 | memset(wave, 0, logo.width()*logo.height());
|
---|
120 | startTimer(30); // wave timer
|
---|
121 | }
|
---|
122 |
|
---|
123 | GLWidget::~GLWidget()
|
---|
124 | {
|
---|
125 | delete[] wave;
|
---|
126 | glDeleteLists(tile_list, 1);
|
---|
127 | delete texture_fbo;
|
---|
128 | if (render_fbo != texture_fbo)
|
---|
129 | delete render_fbo;
|
---|
130 | }
|
---|
131 |
|
---|
132 | void GLWidget::paintEvent(QPaintEvent *)
|
---|
133 | {
|
---|
134 | draw();
|
---|
135 | }
|
---|
136 |
|
---|
137 | void GLWidget::draw()
|
---|
138 | {
|
---|
139 | QPainter p(this); // used for text overlay
|
---|
140 |
|
---|
141 | // save the GL state set for QPainter
|
---|
142 | saveGLState();
|
---|
143 |
|
---|
144 | // render the 'bubbles.svg' file into our framebuffer object
|
---|
145 | QPainter fbo_painter(render_fbo);
|
---|
146 | svg_renderer->render(&fbo_painter);
|
---|
147 | fbo_painter.end();
|
---|
148 |
|
---|
149 | if (render_fbo != texture_fbo) {
|
---|
150 | QRect rect(0, 0, render_fbo->width(), render_fbo->height());
|
---|
151 | QGLFramebufferObject::blitFramebuffer(texture_fbo, rect,
|
---|
152 | render_fbo, rect);
|
---|
153 | }
|
---|
154 |
|
---|
155 | // draw into the GL widget
|
---|
156 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
157 | glMatrixMode(GL_PROJECTION);
|
---|
158 | glLoadIdentity();
|
---|
159 | glFrustum(-1, 1, -1, 1, 10, 100);
|
---|
160 | glTranslatef(0.0f, 0.0f, -15.0f);
|
---|
161 | glMatrixMode(GL_MODELVIEW);
|
---|
162 | glLoadIdentity();
|
---|
163 | glViewport(0, 0, width(), height());
|
---|
164 | glEnable(GL_BLEND);
|
---|
165 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
166 |
|
---|
167 | glBindTexture(GL_TEXTURE_2D, texture_fbo->texture());
|
---|
168 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
---|
169 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
---|
170 | glEnable(GL_TEXTURE_2D);
|
---|
171 | glEnable(GL_MULTISAMPLE);
|
---|
172 | glEnable(GL_CULL_FACE);
|
---|
173 |
|
---|
174 | // draw background
|
---|
175 | glPushMatrix();
|
---|
176 | glScalef(1.7f, 1.7f, 1.7f);
|
---|
177 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
---|
178 | glCallList(tile_list);
|
---|
179 | glPopMatrix();
|
---|
180 |
|
---|
181 | const int w = logo.width();
|
---|
182 | const int h = logo.height();
|
---|
183 |
|
---|
184 | glRotatef(rot_x, 1.0f, 0.0f, 0.0f);
|
---|
185 | glRotatef(rot_y, 0.0f, 1.0f, 0.0f);
|
---|
186 | glRotatef(rot_z, 0.0f, 0.0f, 1.0f);
|
---|
187 | glScalef(scale/w, scale/w, scale/w);
|
---|
188 |
|
---|
189 | glDepthFunc(GL_LESS);
|
---|
190 | glEnable(GL_DEPTH_TEST);
|
---|
191 | // draw the Qt icon
|
---|
192 | glTranslatef(-w+1, -h+1, 0.0f);
|
---|
193 | for (int y=h-1; y>=0; --y) {
|
---|
194 | uint *p = (uint*) logo.scanLine(y);
|
---|
195 | uint *end = p + w;
|
---|
196 | int x = 0;
|
---|
197 | while (p < end) {
|
---|
198 | glColor4ub(qRed(*p), qGreen(*p), qBlue(*p), uchar(qAlpha(*p)*.9));
|
---|
199 | glTranslatef(0.0f, 0.0f, wave[y*w+x]);
|
---|
200 | if (qAlpha(*p) > 128)
|
---|
201 | glCallList(tile_list);
|
---|
202 | glTranslatef(0.0f, 0.0f, -wave[y*w+x]);
|
---|
203 | glTranslatef(2.0f, 0.0f, 0.0f);
|
---|
204 | ++x;
|
---|
205 | ++p;
|
---|
206 | }
|
---|
207 | glTranslatef(-w*2.0f, 2.0f, 0.0f);
|
---|
208 | }
|
---|
209 |
|
---|
210 | // restore the GL state that QPainter expects
|
---|
211 | restoreGLState();
|
---|
212 |
|
---|
213 | // draw the overlayed text using QPainter
|
---|
214 | p.setPen(QColor(197, 197, 197, 157));
|
---|
215 | p.setBrush(QColor(197, 197, 197, 127));
|
---|
216 | p.drawRect(QRect(0, 0, width(), 50));
|
---|
217 | p.setPen(Qt::black);
|
---|
218 | p.setBrush(Qt::NoBrush);
|
---|
219 | const QString str1(tr("A simple OpenGL framebuffer object example."));
|
---|
220 | const QString str2(tr("Use the mouse wheel to zoom, press buttons and move mouse to rotate, double-click to flip."));
|
---|
221 | QFontMetrics fm(p.font());
|
---|
222 | p.drawText(width()/2 - fm.width(str1)/2, 20, str1);
|
---|
223 | p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2);
|
---|
224 | }
|
---|
225 |
|
---|
226 | void GLWidget::mousePressEvent(QMouseEvent *e)
|
---|
227 | {
|
---|
228 | anchor = e->pos();
|
---|
229 | }
|
---|
230 |
|
---|
231 | void GLWidget::mouseMoveEvent(QMouseEvent *e)
|
---|
232 | {
|
---|
233 | QPoint diff = e->pos() - anchor;
|
---|
234 | if (e->buttons() & Qt::LeftButton) {
|
---|
235 | rot_x += diff.y()/5.0f;
|
---|
236 | rot_y += diff.x()/5.0f;
|
---|
237 | } else if (e->buttons() & Qt::RightButton) {
|
---|
238 | rot_z += diff.x()/5.0f;
|
---|
239 | }
|
---|
240 |
|
---|
241 | anchor = e->pos();
|
---|
242 | draw();
|
---|
243 | }
|
---|
244 |
|
---|
245 | void GLWidget::wheelEvent(QWheelEvent *e)
|
---|
246 | {
|
---|
247 | e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f;
|
---|
248 | draw();
|
---|
249 | }
|
---|
250 |
|
---|
251 | void GLWidget::mouseDoubleClickEvent(QMouseEvent *)
|
---|
252 | {
|
---|
253 | anim->start();
|
---|
254 | }
|
---|
255 |
|
---|
256 | void GLWidget::animate(qreal val)
|
---|
257 | {
|
---|
258 | rot_y = val * 180;
|
---|
259 | draw();
|
---|
260 | }
|
---|
261 |
|
---|
262 | void GLWidget::animFinished()
|
---|
263 | {
|
---|
264 | if (anim->direction() == QTimeLine::Forward)
|
---|
265 | anim->setDirection(QTimeLine::Backward);
|
---|
266 | else
|
---|
267 | anim->setDirection(QTimeLine::Forward);
|
---|
268 | }
|
---|
269 |
|
---|
270 | void GLWidget::saveGLState()
|
---|
271 | {
|
---|
272 | glPushAttrib(GL_ALL_ATTRIB_BITS);
|
---|
273 | glMatrixMode(GL_PROJECTION);
|
---|
274 | glPushMatrix();
|
---|
275 | glMatrixMode(GL_MODELVIEW);
|
---|
276 | glPushMatrix();
|
---|
277 | }
|
---|
278 |
|
---|
279 | void GLWidget::restoreGLState()
|
---|
280 | {
|
---|
281 | glMatrixMode(GL_PROJECTION);
|
---|
282 | glPopMatrix();
|
---|
283 | glMatrixMode(GL_MODELVIEW);
|
---|
284 | glPopMatrix();
|
---|
285 | glPopAttrib();
|
---|
286 | }
|
---|
287 |
|
---|
288 | #define PI 3.14159
|
---|
289 |
|
---|
290 | void GLWidget::timerEvent(QTimerEvent *)
|
---|
291 | {
|
---|
292 | if (QApplication::mouseButtons() != 0)
|
---|
293 | return;
|
---|
294 |
|
---|
295 | static bool scale_in = true;
|
---|
296 |
|
---|
297 | if (scale_in && scale > 35.0f)
|
---|
298 | scale_in = false;
|
---|
299 | else if (!scale_in && scale < .5f)
|
---|
300 | scale_in = true;
|
---|
301 |
|
---|
302 | scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f;
|
---|
303 | rot_z += 0.3f;
|
---|
304 | rot_x += 0.1f;
|
---|
305 |
|
---|
306 | int dx, dy; // disturbance point
|
---|
307 | float s, v, W, t;
|
---|
308 | int i, j;
|
---|
309 | static float wt[128][128];
|
---|
310 | const int width = logo.width();
|
---|
311 | const int AMP = 5;
|
---|
312 |
|
---|
313 | dx = dy = width >> 1;
|
---|
314 |
|
---|
315 | W = .3f;
|
---|
316 | v = -4; // wave speed
|
---|
317 |
|
---|
318 | for (i = 0; i < width; ++i) {
|
---|
319 | for ( j = 0; j < width; ++j) {
|
---|
320 | s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy)));
|
---|
321 | wt[i][j] += 0.1f;
|
---|
322 | t = s / v;
|
---|
323 | if (s != 0)
|
---|
324 | wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2));
|
---|
325 | else
|
---|
326 | wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t));
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|