| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 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: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), 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 | // 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 | p.endNativePainting(); | 
|---|
| 210 |  | 
|---|
| 211 | // draw the overlayed text using QPainter | 
|---|
| 212 | p.setPen(QColor(197, 197, 197, 157)); | 
|---|
| 213 | p.setBrush(QColor(197, 197, 197, 127)); | 
|---|
| 214 | p.drawRect(QRect(0, 0, width(), 50)); | 
|---|
| 215 | p.setPen(Qt::black); | 
|---|
| 216 | p.setBrush(Qt::NoBrush); | 
|---|
| 217 | const QString str1(tr("A simple OpenGL pbuffer example.")); | 
|---|
| 218 | const QString str2(tr("Use the mouse wheel to zoom, press buttons and move mouse to rotate, double-click to flip.")); | 
|---|
| 219 | QFontMetrics fm(p.font()); | 
|---|
| 220 | p.drawText(width()/2 - fm.width(str1)/2, 20, str1); | 
|---|
| 221 | p.drawText(width()/2 - fm.width(str2)/2, 20 + fm.lineSpacing(), str2); | 
|---|
| 222 | } | 
|---|
| 223 |  | 
|---|
| 224 | void GLWidget::mousePressEvent(QMouseEvent *e) | 
|---|
| 225 | { | 
|---|
| 226 | anchor = e->pos(); | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | void GLWidget::mouseMoveEvent(QMouseEvent *e) | 
|---|
| 230 | { | 
|---|
| 231 | QPoint diff = e->pos() - anchor; | 
|---|
| 232 | if (e->buttons() & Qt::LeftButton) { | 
|---|
| 233 | rot_x += diff.y()/5.0f; | 
|---|
| 234 | rot_y += diff.x()/5.0f; | 
|---|
| 235 | } else if (e->buttons() & Qt::RightButton) { | 
|---|
| 236 | rot_z += diff.x()/5.0f; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | anchor = e->pos(); | 
|---|
| 240 | draw(); | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | void GLWidget::wheelEvent(QWheelEvent *e) | 
|---|
| 244 | { | 
|---|
| 245 | e->delta() > 0 ? scale += scale*0.1f : scale -= scale*0.1f; | 
|---|
| 246 | draw(); | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | void GLWidget::mouseDoubleClickEvent(QMouseEvent *) | 
|---|
| 250 | { | 
|---|
| 251 | anim->start(); | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | void GLWidget::animate(qreal val) | 
|---|
| 255 | { | 
|---|
| 256 | rot_y = val * 180; | 
|---|
| 257 | draw(); | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | void GLWidget::animFinished() | 
|---|
| 261 | { | 
|---|
| 262 | if (anim->direction() == QTimeLine::Forward) | 
|---|
| 263 | anim->setDirection(QTimeLine::Backward); | 
|---|
| 264 | else | 
|---|
| 265 | anim->setDirection(QTimeLine::Forward); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | void GLWidget::saveGLState() | 
|---|
| 269 | { | 
|---|
| 270 | glPushAttrib(GL_ALL_ATTRIB_BITS); | 
|---|
| 271 | glMatrixMode(GL_PROJECTION); | 
|---|
| 272 | glPushMatrix(); | 
|---|
| 273 | glMatrixMode(GL_MODELVIEW); | 
|---|
| 274 | glPushMatrix(); | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | void GLWidget::restoreGLState() | 
|---|
| 278 | { | 
|---|
| 279 | glMatrixMode(GL_PROJECTION); | 
|---|
| 280 | glPopMatrix(); | 
|---|
| 281 | glMatrixMode(GL_MODELVIEW); | 
|---|
| 282 | glPopMatrix(); | 
|---|
| 283 | glPopAttrib(); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | #define PI 3.14159 | 
|---|
| 287 |  | 
|---|
| 288 | void GLWidget::timerEvent(QTimerEvent *) | 
|---|
| 289 | { | 
|---|
| 290 | if (QApplication::mouseButtons() != 0) | 
|---|
| 291 | return; | 
|---|
| 292 |  | 
|---|
| 293 | static bool scale_in = true; | 
|---|
| 294 |  | 
|---|
| 295 | if (scale_in && scale > 35.0f) | 
|---|
| 296 | scale_in = false; | 
|---|
| 297 | else if (!scale_in && scale < .5f) | 
|---|
| 298 | scale_in = true; | 
|---|
| 299 |  | 
|---|
| 300 | scale = scale_in ? scale + scale*0.01f : scale-scale*0.01f; | 
|---|
| 301 | rot_z += 0.3f; | 
|---|
| 302 | rot_x += 0.1f; | 
|---|
| 303 |  | 
|---|
| 304 | int dx, dy; // disturbance point | 
|---|
| 305 | float s, v, W, t; | 
|---|
| 306 | int i, j; | 
|---|
| 307 | static float wt[128][128]; | 
|---|
| 308 | const int width = logo.width(); | 
|---|
| 309 | const int AMP = 5; | 
|---|
| 310 |  | 
|---|
| 311 | dx = dy = width >> 1; | 
|---|
| 312 |  | 
|---|
| 313 | W = .3f; | 
|---|
| 314 | v = -4; // wave speed | 
|---|
| 315 |  | 
|---|
| 316 | for (i = 0; i < width; ++i) { | 
|---|
| 317 | for ( j = 0; j < width; ++j) { | 
|---|
| 318 | s = sqrt((double) ((j - dx) * (j - dx) + (i - dy) * (i - dy))); | 
|---|
| 319 | wt[i][j] += 0.1f; | 
|---|
| 320 | t = s / v; | 
|---|
| 321 | if (s != 0) | 
|---|
| 322 | wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)) / (0.2*(s + 2)); | 
|---|
| 323 | else | 
|---|
| 324 | wave[i*width + j] = AMP * sin(2 * PI * W * (wt[i][j] + t)); | 
|---|
| 325 | } | 
|---|
| 326 | } | 
|---|
| 327 | } | 
|---|