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