1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
41
#include <QtGui/QImage>
46
#ifndef GL_MULTISAMPLE
47
#define GL_MULTISAMPLE 0x809D
50
GLWidget::GLWidget(QWidget *parent)
51
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
53
setWindowTitle(tr("OpenGL pbuffers 2"));
55
pbuffer = new QGLPixelBuffer(1024, 1024, format(), this);
57
rot_x = rot_y = rot_z = 0.0f;
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()));
64
svg_renderer = new QSvgRenderer(QLatin1String(":/res/bubbles.svg"), this);
65
connect(svg_renderer, SIGNAL(repaintNeeded()), this, SLOT(draw()));
67
logo = QImage(":/res/designer.png");
68
logo = logo.convertToFormat(QImage::Format_ARGB32);
70
makeCurrent(); // need a current context to create the display list
71
tile_list = glGenLists(1);
72
glNewList(tile_list, GL_COMPILE);
75
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
76
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
77
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
78
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
80
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
81
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
82
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
83
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
85
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
86
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
87
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
88
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
90
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
91
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
92
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
93
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
95
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
96
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
97
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
98
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
100
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
101
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
102
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
103
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
108
wave = new GLfloat[logo.width()*logo.height()];
109
memset(wave, 0, logo.width()*logo.height());
110
startTimer(30); // wave timer
112
pbuffer->makeCurrent();
113
dynamicTexture = pbuffer->generateDynamicTexture();
115
// bind the dynamic texture to the pbuffer - this is a no-op under X11
116
hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture);
119
GLWidget::~GLWidget()
122
glDeleteLists(tile_list, 1);
123
pbuffer->releaseFromDynamicTexture();
124
glDeleteTextures(1, &dynamicTexture);
128
void GLWidget::paintEvent(QPaintEvent *)
133
void GLWidget::draw()
135
QPainter p(this); // used for text overlay
137
// save the GL state set for QPainter
138
p.beginNativePainting();
141
// render the 'bubbles.svg' file into our pbuffer
142
QPainter pbuffer_painter(pbuffer);
143
svg_renderer->render(&pbuffer_painter);
144
pbuffer_painter.end();
147
if (!hasDynamicTextureUpdate)
148
pbuffer->updateDynamicTexture(dynamicTexture);
151
// draw into the GL widget
152
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
153
glMatrixMode(GL_PROJECTION);
155
glFrustum(-1, 1, -1, 1, 10, 100);
156
glTranslatef(0.0f, 0.0f, -15.0f);
157
glMatrixMode(GL_MODELVIEW);
159
glViewport(0, 0, width(), height());
161
glBindTexture(GL_TEXTURE_2D, dynamicTexture);
162
glEnable(GL_TEXTURE_2D);
163
glEnable(GL_MULTISAMPLE);
164
glEnable(GL_CULL_FACE);
166
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
170
glScalef(1.7f, 1.7f, 1.7f);
171
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
172
glCallList(tile_list);
175
const int w = logo.width();
176
const int h = logo.height();
178
glRotatef(rot_x, 1.0f, 0.0f, 0.0f);
179
glRotatef(rot_y, 0.0f, 1.0f, 0.0f);
180
glRotatef(rot_z, 0.0f, 0.0f, 1.0f);
181
glScalef(scale/w, scale/w, scale/w);
183
glDepthFunc(GL_LESS);
184
glEnable(GL_DEPTH_TEST);
187
glTranslatef(-w+1, -h+1, 0.0f);
188
for (int y=h-1; y>=0; --y) {
189
uint *p = (uint*) logo.scanLine(y);
193
glColor4ub(qRed(*p), qGreen(*p), qBlue(*p), uchar(qAlpha(*p)*.9));
194
glTranslatef(0.0f, 0.0f, wave[y*w+x]);
195
if (qAlpha(*p) > 128)
196
glCallList(tile_list);
197
glTranslatef(0.0f, 0.0f, -wave[y*w+x]);
198
glTranslatef(2.0f, 0.0f, 0.0f);
202
glTranslatef(-w*2.0f, 2.0f, 0.0f);
205
// restore the GL state that QPainter expects
207
p.endNativePainting();
209
// draw the overlayed text using QPainter
210
p.setPen(QColor(197, 197, 197, 157));
211
p.setBrush(QColor(197, 197, 197, 127));
212
p.drawRect(QRect(0, 0, width(), 50));
214
p.setBrush(Qt::NoBrush);
215
const QString str1(tr("A simple OpenGL pbuffer example."));
216
const QString str2(tr("Use the mouse wheel to zoom, press buttons and move mouse to rotate, double-click to flip."));
217
QFontMetrics fm(p.font());
218
p.drawText(width()/2 - fm.width(str1)/2, 20, str1);
219
p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2);
222
void GLWidget::mousePressEvent(QMouseEvent *e)
227
void GLWidget::mouseMoveEvent(QMouseEvent *e)
229
QPoint diff = e->pos() - anchor;
230
if (e->buttons() & Qt::LeftButton) {
231
rot_x += diff.y()/5.0f;
232
rot_y += diff.x()/5.0f;
233
} else if (e->buttons() & Qt::RightButton) {
234
rot_z += diff.x()/5.0f;
241
void GLWidget::wheelEvent(QWheelEvent *e)
243
e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f;
247
void GLWidget::mouseDoubleClickEvent(QMouseEvent *)
252
void GLWidget::animate(qreal val)
258
void GLWidget::animFinished()
260
if (anim->direction() == QTimeLine::Forward)
261
anim->setDirection(QTimeLine::Backward);
263
anim->setDirection(QTimeLine::Forward);
266
void GLWidget::saveGLState()
268
glPushAttrib(GL_ALL_ATTRIB_BITS);
269
glMatrixMode(GL_PROJECTION);
271
glMatrixMode(GL_MODELVIEW);
275
void GLWidget::restoreGLState()
277
glMatrixMode(GL_PROJECTION);
279
glMatrixMode(GL_MODELVIEW);
286
void GLWidget::timerEvent(QTimerEvent *)
288
if (QApplication::mouseButtons() != 0)
291
static bool scale_in = true;
293
if (scale_in && scale > 35.0f)
295
else if (!scale_in && scale < .5f)
298
scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f;
302
int dx, dy; // disturbance point
305
static float wt[128][128];
306
const int width = logo.width();
309
dx = dy = width >> 1;
312
v = -4; // wave speed
314
for (i = 0; i < width; ++i) {
315
for ( j = 0; j < width; ++j) {
316
s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy)));
320
wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2));
322
wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t));