~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to examples/opengl/pbuffers2/glwidget.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the examples of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:BSD$
 
9
** You may use this file under the terms of the BSD license as follows:
 
10
**
 
11
** "Redistribution and use in source and binary forms, with or without
 
12
** modification, are permitted provided that the following conditions are
 
13
** met:
 
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
 
19
**     distribution.
 
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.
 
23
**
 
24
**
 
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."
 
36
**
 
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), parent)
 
52
{
 
53
    setWindowTitle(tr("OpenGL pbuffers 2"));
 
54
 
 
55
    pbuffer = new QGLPixelBuffer(1024, 1024, format(), this);
 
56
 
 
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
    makeCurrent(); // need a current context to create the display list
 
71
    tile_list = glGenLists(1);
 
72
    glNewList(tile_list, GL_COMPILE);
 
73
    glBegin(GL_QUADS);
 
74
    {
 
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);
 
79
 
 
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);
 
84
 
 
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);
 
89
 
 
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);
 
94
 
 
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);
 
99
 
 
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);
 
104
}
 
105
    glEnd();
 
106
    glEndList();
 
107
 
 
108
    wave = new GLfloat[logo.width()*logo.height()];
 
109
    memset(wave, 0, logo.width()*logo.height());
 
110
    startTimer(30); // wave timer
 
111
 
 
112
    pbuffer->makeCurrent();
 
113
    dynamicTexture = pbuffer->generateDynamicTexture();
 
114
 
 
115
    // bind the dynamic texture to the pbuffer - this is a no-op under X11
 
116
    hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture);
 
117
}
 
118
 
 
119
GLWidget::~GLWidget()
 
120
{
 
121
    delete[] wave;
 
122
    glDeleteLists(tile_list, 1);
 
123
    pbuffer->releaseFromDynamicTexture();
 
124
    glDeleteTextures(1, &dynamicTexture);
 
125
    delete pbuffer;
 
126
}
 
127
 
 
128
void GLWidget::paintEvent(QPaintEvent *)
 
129
{
 
130
    draw();
 
131
}
 
132
 
 
133
void GLWidget::draw()
 
134
{
 
135
    QPainter p(this); // used for text overlay
 
136
 
 
137
    // save the GL state set for QPainter
 
138
    p.beginNativePainting();
 
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
    if (!hasDynamicTextureUpdate)
 
148
        pbuffer->updateDynamicTexture(dynamicTexture);
 
149
 
 
150
    makeCurrent();
 
151
    // draw into the GL widget
 
152
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
153
    glMatrixMode(GL_PROJECTION);
 
154
    glLoadIdentity();
 
155
    glFrustum(-1, 1, -1, 1, 10, 100);
 
156
    glTranslatef(0.0f, 0.0f, -15.0f);
 
157
    glMatrixMode(GL_MODELVIEW);
 
158
    glLoadIdentity();
 
159
    glViewport(0, 0, width(), height());
 
160
 
 
161
    glBindTexture(GL_TEXTURE_2D, dynamicTexture);
 
162
    glEnable(GL_TEXTURE_2D);
 
163
    glEnable(GL_MULTISAMPLE);
 
164
    glEnable(GL_CULL_FACE);
 
165
    glEnable(GL_BLEND);
 
166
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 
167
 
 
168
    // draw background
 
169
    glPushMatrix();
 
170
    glScalef(1.7f, 1.7f, 1.7f);
 
171
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
 
172
    glCallList(tile_list);
 
173
    glPopMatrix();
 
174
 
 
175
    const int w = logo.width();
 
176
    const int h = logo.height();
 
177
 
 
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);
 
182
 
 
183
    glDepthFunc(GL_LESS);
 
184
    glEnable(GL_DEPTH_TEST);
 
185
 
 
186
    // draw the Qt icon
 
187
    glTranslatef(-w+1, -h+1, 0.0f);
 
188
    for (int y=h-1; y>=0; --y) {
 
189
        uint *p = (uint*) logo.scanLine(y);
 
190
        uint *end = p + w;
 
191
        int  x = 0;
 
192
        while (p < end) {
 
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);
 
199
            ++x;
 
200
            ++p;
 
201
        }
 
202
        glTranslatef(-w*2.0f, 2.0f, 0.0f);
 
203
    }
 
204
 
 
205
    // restore the GL state that QPainter expects
 
206
    restoreGLState();
 
207
    p.endNativePainting();
 
208
 
 
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));
 
213
    p.setPen(Qt::black);
 
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);
 
220
}
 
221
 
 
222
void GLWidget::mousePressEvent(QMouseEvent *e)
 
223
{
 
224
    anchor = e->pos();
 
225
}
 
226
 
 
227
void GLWidget::mouseMoveEvent(QMouseEvent *e)
 
228
{
 
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;
 
235
    }
 
236
 
 
237
    anchor = e->pos();
 
238
    draw();
 
239
}
 
240
 
 
241
void GLWidget::wheelEvent(QWheelEvent *e)
 
242
{
 
243
    e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f;
 
244
    draw();
 
245
}
 
246
 
 
247
void GLWidget::mouseDoubleClickEvent(QMouseEvent *)
 
248
{
 
249
    anim->start();
 
250
}
 
251
 
 
252
void GLWidget::animate(qreal val)
 
253
{
 
254
    rot_y = val * 180;
 
255
    draw();
 
256
}
 
257
 
 
258
void GLWidget::animFinished()
 
259
{
 
260
    if (anim->direction() == QTimeLine::Forward)
 
261
        anim->setDirection(QTimeLine::Backward);
 
262
    else
 
263
        anim->setDirection(QTimeLine::Forward);
 
264
}
 
265
 
 
266
void GLWidget::saveGLState()
 
267
{
 
268
    glPushAttrib(GL_ALL_ATTRIB_BITS);
 
269
    glMatrixMode(GL_PROJECTION);
 
270
    glPushMatrix();
 
271
    glMatrixMode(GL_MODELVIEW);
 
272
    glPushMatrix();
 
273
}
 
274
 
 
275
void GLWidget::restoreGLState()
 
276
{
 
277
    glMatrixMode(GL_PROJECTION);
 
278
    glPopMatrix();
 
279
    glMatrixMode(GL_MODELVIEW);
 
280
    glPopMatrix();
 
281
    glPopAttrib();
 
282
}
 
283
 
 
284
#define PI 3.14159
 
285
 
 
286
void GLWidget::timerEvent(QTimerEvent *)
 
287
{
 
288
    if (QApplication::mouseButtons() != 0)
 
289
        return;
 
290
 
 
291
    static bool scale_in = true;
 
292
 
 
293
    if (scale_in && scale > 35.0f)
 
294
        scale_in = false;
 
295
    else if (!scale_in && scale < .5f)
 
296
        scale_in = true;
 
297
 
 
298
    scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f;
 
299
    rot_z += 0.3f;
 
300
    rot_x += 0.1f;
 
301
 
 
302
    int dx, dy; // disturbance point
 
303
    float s, v, W, t;
 
304
    int i, j;
 
305
    static float wt[128][128];
 
306
    const int width = logo.width();
 
307
    const int AMP = 5;
 
308
 
 
309
    dx = dy = width >> 1;
 
310
 
 
311
    W = .3f;
 
312
    v = -4; // wave speed
 
313
 
 
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)));
 
317
            wt[i][j] += 0.1f;
 
318
            t = s / v;
 
319
            if (s != 0)
 
320
                wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2));
 
321
            else
 
322
                wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t));
 
323
        }
 
324
    }
 
325
}