author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Mon, 19 Apr 2010 10:15:19 +0300 | |
branch | RCL_3 |
changeset 9 | b5b118452c7d |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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 |
p.beginNativePainting(); |
|
140 |
saveGLState(); |
|
141 |
||
142 |
// render the 'bubbles.svg' file into our pbuffer |
|
143 |
QPainter pbuffer_painter(pbuffer); |
|
144 |
svg_renderer->render(&pbuffer_painter); |
|
145 |
pbuffer_painter.end(); |
|
146 |
glFlush(); |
|
147 |
||
148 |
// rendering directly to a texture is not supported on X11 and |
|
149 |
// some Windows implementations, unfortunately |
|
150 |
if (!hasDynamicTextureUpdate) |
|
151 |
pbuffer->updateDynamicTexture(dynamicTexture); |
|
152 |
||
153 |
makeCurrent(); |
|
154 |
// draw into the GL widget |
|
155 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
156 |
glMatrixMode(GL_PROJECTION); |
|
157 |
glLoadIdentity(); |
|
158 |
glFrustum(-1, 1, -1, 1, 10, 100); |
|
159 |
glTranslatef(0.0f, 0.0f, -15.0f); |
|
160 |
glMatrixMode(GL_MODELVIEW); |
|
161 |
glLoadIdentity(); |
|
162 |
glViewport(0, 0, width(), height()); |
|
163 |
||
164 |
glBindTexture(GL_TEXTURE_2D, dynamicTexture); |
|
165 |
glEnable(GL_TEXTURE_2D); |
|
166 |
glEnable(GL_MULTISAMPLE); |
|
167 |
glEnable(GL_CULL_FACE); |
|
168 |
glEnable(GL_BLEND); |
|
169 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
170 |
||
171 |
// draw background |
|
172 |
glPushMatrix(); |
|
173 |
glScalef(1.7f, 1.7f, 1.7f); |
|
174 |
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); |
|
175 |
glCallList(tile_list); |
|
176 |
glPopMatrix(); |
|
177 |
||
178 |
const int w = logo.width(); |
|
179 |
const int h = logo.height(); |
|
180 |
||
181 |
glRotatef(rot_x, 1.0f, 0.0f, 0.0f); |
|
182 |
glRotatef(rot_y, 0.0f, 1.0f, 0.0f); |
|
183 |
glRotatef(rot_z, 0.0f, 0.0f, 1.0f); |
|
184 |
glScalef(scale/w, scale/w, scale/w); |
|
185 |
||
186 |
glDepthFunc(GL_LESS); |
|
187 |
glEnable(GL_DEPTH_TEST); |
|
188 |
||
189 |
// draw the Qt icon |
|
190 |
glTranslatef(-w+1, -h+1, 0.0f); |
|
191 |
for (int y=h-1; y>=0; --y) { |
|
192 |
uint *p = (uint*) logo.scanLine(y); |
|
193 |
uint *end = p + w; |
|
194 |
int x = 0; |
|
195 |
while (p < end) { |
|
196 |
glColor4ub(qRed(*p), qGreen(*p), qBlue(*p), uchar(qAlpha(*p)*.9)); |
|
197 |
glTranslatef(0.0f, 0.0f, wave[y*w+x]); |
|
198 |
if (qAlpha(*p) > 128) |
|
199 |
glCallList(tile_list); |
|
200 |
glTranslatef(0.0f, 0.0f, -wave[y*w+x]); |
|
201 |
glTranslatef(2.0f, 0.0f, 0.0f); |
|
202 |
++x; |
|
203 |
++p; |
|
204 |
} |
|
205 |
glTranslatef(-w*2.0f, 2.0f, 0.0f); |
|
206 |
} |
|
207 |
||
208 |
// restore the GL state that QPainter expects |
|
209 |
restoreGLState(); |
|
210 |
p.endNativePainting(); |
|
211 |
||
212 |
// draw the overlayed text using QPainter |
|
213 |
p.setPen(QColor(197, 197, 197, 157)); |
|
214 |
p.setBrush(QColor(197, 197, 197, 127)); |
|
215 |
p.drawRect(QRect(0, 0, width(), 50)); |
|
216 |
p.setPen(Qt::black); |
|
217 |
p.setBrush(Qt::NoBrush); |
|
218 |
const QString str1(tr("A simple OpenGL pbuffer example.")); |
|
219 |
const QString str2(tr("Use the mouse wheel to zoom, press buttons and move mouse to rotate, double-click to flip.")); |
|
220 |
QFontMetrics fm(p.font()); |
|
221 |
p.drawText(width()/2 - fm.width(str1)/2, 20, str1); |
|
222 |
p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2); |
|
223 |
} |
|
224 |
||
225 |
void GLWidget::mousePressEvent(QMouseEvent *e) |
|
226 |
{ |
|
227 |
anchor = e->pos(); |
|
228 |
} |
|
229 |
||
230 |
void GLWidget::mouseMoveEvent(QMouseEvent *e) |
|
231 |
{ |
|
232 |
QPoint diff = e->pos() - anchor; |
|
233 |
if (e->buttons() & Qt::LeftButton) { |
|
234 |
rot_x += diff.y()/5.0f; |
|
235 |
rot_y += diff.x()/5.0f; |
|
236 |
} else if (e->buttons() & Qt::RightButton) { |
|
237 |
rot_z += diff.x()/5.0f; |
|
238 |
} |
|
239 |
||
240 |
anchor = e->pos(); |
|
241 |
draw(); |
|
242 |
} |
|
243 |
||
244 |
void GLWidget::wheelEvent(QWheelEvent *e) |
|
245 |
{ |
|
246 |
e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f; |
|
247 |
draw(); |
|
248 |
} |
|
249 |
||
250 |
void GLWidget::mouseDoubleClickEvent(QMouseEvent *) |
|
251 |
{ |
|
252 |
anim->start(); |
|
253 |
} |
|
254 |
||
255 |
void GLWidget::animate(qreal val) |
|
256 |
{ |
|
257 |
rot_y = val * 180; |
|
258 |
draw(); |
|
259 |
} |
|
260 |
||
261 |
void GLWidget::animFinished() |
|
262 |
{ |
|
263 |
if (anim->direction() == QTimeLine::Forward) |
|
264 |
anim->setDirection(QTimeLine::Backward); |
|
265 |
else |
|
266 |
anim->setDirection(QTimeLine::Forward); |
|
267 |
} |
|
268 |
||
269 |
void GLWidget::saveGLState() |
|
270 |
{ |
|
271 |
glPushAttrib(GL_ALL_ATTRIB_BITS); |
|
272 |
glMatrixMode(GL_PROJECTION); |
|
273 |
glPushMatrix(); |
|
274 |
glMatrixMode(GL_MODELVIEW); |
|
275 |
glPushMatrix(); |
|
276 |
} |
|
277 |
||
278 |
void GLWidget::restoreGLState() |
|
279 |
{ |
|
280 |
glMatrixMode(GL_PROJECTION); |
|
281 |
glPopMatrix(); |
|
282 |
glMatrixMode(GL_MODELVIEW); |
|
283 |
glPopMatrix(); |
|
284 |
glPopAttrib(); |
|
285 |
} |
|
286 |
||
287 |
#define PI 3.14159 |
|
288 |
||
289 |
void GLWidget::timerEvent(QTimerEvent *) |
|
290 |
{ |
|
291 |
if (QApplication::mouseButtons() != 0) |
|
292 |
return; |
|
293 |
||
294 |
static bool scale_in = true; |
|
295 |
||
296 |
if (scale_in && scale > 35.0f) |
|
297 |
scale_in = false; |
|
298 |
else if (!scale_in && scale < .5f) |
|
299 |
scale_in = true; |
|
300 |
||
301 |
scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f; |
|
302 |
rot_z += 0.3f; |
|
303 |
rot_x += 0.1f; |
|
304 |
||
305 |
int dx, dy; // disturbance point |
|
306 |
float s, v, W, t; |
|
307 |
int i, j; |
|
308 |
static float wt[128][128]; |
|
309 |
const int width = logo.width(); |
|
310 |
const int AMP = 5; |
|
311 |
||
312 |
dx = dy = width >> 1; |
|
313 |
||
314 |
W = .3f; |
|
315 |
v = -4; // wave speed |
|
316 |
||
317 |
for (i = 0; i < width; ++i) { |
|
318 |
for ( j = 0; j < width; ++j) { |
|
319 |
s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy))); |
|
320 |
wt[i][j] += 0.1f; |
|
321 |
t = s / v; |
|
322 |
if (s != 0) |
|
323 |
wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2)); |
|
324 |
else |
|
325 |
wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)); |
|
326 |
} |
|
327 |
} |
|
328 |
} |