[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 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.
|
---|
[2] | 25 | **
|
---|
[846] | 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."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "glwidget.h"
|
---|
| 42 | #include <math.h>
|
---|
| 43 |
|
---|
[561] | 44 | #include "cube.h"
|
---|
[2] | 45 |
|
---|
[561] | 46 | #include <QGLPixelBuffer>
|
---|
[2] | 47 |
|
---|
[561] | 48 | #ifndef GL_MULTISAMPLE
|
---|
| 49 | #define GL_MULTISAMPLE 0x809D
|
---|
| 50 | #endif
|
---|
[2] | 51 |
|
---|
[561] | 52 | static GLfloat colorArray[][4] = {
|
---|
| 53 | {0.243f, 0.423f, 0.125f, 1.0f},
|
---|
| 54 | {0.176f, 0.31f, 0.09f, 1.0f},
|
---|
| 55 | {0.4f, 0.69f, 0.212f, 1.0f},
|
---|
| 56 | {0.317f, 0.553f, 0.161f, 1.0f}
|
---|
[2] | 57 | };
|
---|
| 58 |
|
---|
| 59 | GLWidget::GLWidget(QWidget *parent)
|
---|
[561] | 60 | : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
|
---|
| 61 | , geom(0)
|
---|
| 62 | , cube(0)
|
---|
[2] | 63 | {
|
---|
| 64 | // create the pbuffer
|
---|
[846] | 65 | QGLFormat pbufferFormat = format();
|
---|
| 66 | pbufferFormat.setSampleBuffers(false);
|
---|
| 67 | pbuffer = new QGLPixelBuffer(QSize(512, 512), pbufferFormat, this);
|
---|
[2] | 68 | setWindowTitle(tr("OpenGL pbuffers"));
|
---|
[561] | 69 | initializeGeometry();
|
---|
[2] | 70 | }
|
---|
| 71 |
|
---|
| 72 | GLWidget::~GLWidget()
|
---|
| 73 | {
|
---|
| 74 | pbuffer->releaseFromDynamicTexture();
|
---|
| 75 | glDeleteTextures(1, &dynamicTexture);
|
---|
| 76 | delete pbuffer;
|
---|
[561] | 77 |
|
---|
| 78 | qDeleteAll(cubes);
|
---|
| 79 | qDeleteAll(tiles);
|
---|
| 80 | delete cube;
|
---|
[2] | 81 | }
|
---|
| 82 |
|
---|
| 83 | void GLWidget::initializeGL()
|
---|
| 84 | {
|
---|
| 85 | initCommon();
|
---|
[561] | 86 | glShadeModel(GL_SMOOTH);
|
---|
| 87 | glEnable(GL_LIGHTING);
|
---|
| 88 | glEnable(GL_LIGHT0);
|
---|
| 89 | static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
|
---|
| 90 | glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
---|
[2] | 91 | initPbuffer();
|
---|
[561] | 92 | cube->startAnimation();
|
---|
| 93 | connect(cube, SIGNAL(changed()), this, SLOT(update()));
|
---|
| 94 | for (int i = 0; i < 3; ++i)
|
---|
| 95 | {
|
---|
| 96 | cubes[i]->startAnimation();
|
---|
| 97 | connect(cubes[i], SIGNAL(changed()), this, SLOT(update()));
|
---|
[2] | 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | void GLWidget::paintGL()
|
---|
| 102 | {
|
---|
| 103 | pbuffer->makeCurrent();
|
---|
[561] | 104 | drawPbuffer();
|
---|
| 105 | // On direct render platforms, drawing onto the pbuffer context above
|
---|
| 106 | // automatically updates the dynamic texture. For cases where rendering
|
---|
| 107 | // directly to a texture is not supported, explicitly copy.
|
---|
[2] | 108 | if (!hasDynamicTextureUpdate)
|
---|
| 109 | pbuffer->updateDynamicTexture(dynamicTexture);
|
---|
[561] | 110 | makeCurrent();
|
---|
[2] | 111 |
|
---|
[561] | 112 | // Use the pbuffer as a texture to render the scene
|
---|
[2] | 113 | glBindTexture(GL_TEXTURE_2D, dynamicTexture);
|
---|
[561] | 114 |
|
---|
| 115 | // set up to render the scene
|
---|
[2] | 116 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
[561] | 117 | glLoadIdentity();
|
---|
| 118 | glTranslatef(0.0f, 0.0f, -10.0f);
|
---|
[2] | 119 |
|
---|
| 120 | // draw the background
|
---|
| 121 | glPushMatrix();
|
---|
[561] | 122 | glScalef(aspect, 1.0f, 1.0f);
|
---|
| 123 | for (int i = 0; i < tiles.count(); ++i)
|
---|
| 124 | tiles[i]->draw();
|
---|
[2] | 125 | glPopMatrix();
|
---|
| 126 |
|
---|
| 127 | // draw the bouncing cubes
|
---|
[561] | 128 | for (int i = 0; i < cubes.count(); ++i)
|
---|
| 129 | cubes[i]->draw();
|
---|
[2] | 130 | }
|
---|
| 131 |
|
---|
[561] | 132 | void GLWidget::initializeGeometry()
|
---|
[2] | 133 | {
|
---|
[561] | 134 | geom = new Geometry();
|
---|
| 135 | CubeBuilder cBuilder(geom, 0.5);
|
---|
| 136 | cBuilder.setColor(QColor(255, 255, 255, 212));
|
---|
| 137 | // build the 3 bouncing, spinning cubes
|
---|
| 138 | for (int i = 0; i < 3; ++i)
|
---|
| 139 | cubes.append(cBuilder.newCube(QVector3D((float)(i-1), -1.5f, 5 - i)));
|
---|
[2] | 140 |
|
---|
[561] | 141 | // build the spinning cube which goes in the dynamic texture
|
---|
| 142 | cube = cBuilder.newCube();
|
---|
| 143 | cube->removeBounce();
|
---|
[2] | 144 |
|
---|
[561] | 145 | // build the background tiles
|
---|
| 146 | TileBuilder tBuilder(geom);
|
---|
| 147 | tBuilder.setColor(QColor(Qt::white));
|
---|
| 148 | for (int c = -2; c <= +2; ++c)
|
---|
| 149 | for (int r = -2; r <= +2; ++r)
|
---|
| 150 | tiles.append(tBuilder.newTile(QVector3D(c, r, 0)));
|
---|
| 151 |
|
---|
| 152 | // graded backdrop for the pbuffer scene
|
---|
| 153 | TileBuilder bBuilder(geom, 0.0f, 2.0f);
|
---|
| 154 | bBuilder.setColor(QColor(102, 176, 54, 210));
|
---|
| 155 | backdrop = bBuilder.newTile(QVector3D(0.0f, 0.0f, -1.5f));
|
---|
| 156 | backdrop->setColors(colorArray);
|
---|
[2] | 157 | }
|
---|
| 158 |
|
---|
| 159 | void GLWidget::initCommon()
|
---|
| 160 | {
|
---|
[561] | 161 | qglClearColor(QColor(Qt::darkGray));
|
---|
[2] | 162 |
|
---|
[561] | 163 | glEnable(GL_DEPTH_TEST);
|
---|
| 164 | glEnable(GL_CULL_FACE);
|
---|
| 165 | glEnable(GL_MULTISAMPLE);
|
---|
| 166 |
|
---|
[2] | 167 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
| 168 | glEnable(GL_BLEND);
|
---|
[561] | 169 |
|
---|
[2] | 170 | glEnable(GL_TEXTURE_2D);
|
---|
| 171 |
|
---|
[561] | 172 | geom->loadArrays();
|
---|
[2] | 173 | }
|
---|
| 174 |
|
---|
[561] | 175 | void GLWidget::perspectiveProjection()
|
---|
[2] | 176 | {
|
---|
[561] | 177 | glMatrixMode(GL_PROJECTION);
|
---|
| 178 | glLoadIdentity();
|
---|
| 179 | #ifdef QT_OPENGL_ES
|
---|
| 180 | glFrustumf(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0);
|
---|
| 181 | #else
|
---|
| 182 | glFrustum(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0);
|
---|
| 183 | #endif
|
---|
| 184 | glMatrixMode(GL_MODELVIEW);
|
---|
| 185 | }
|
---|
[2] | 186 |
|
---|
[561] | 187 | void GLWidget::orthographicProjection()
|
---|
| 188 | {
|
---|
[2] | 189 | glMatrixMode(GL_PROJECTION);
|
---|
| 190 | glLoadIdentity();
|
---|
[561] | 191 | #ifdef QT_OPENGL_ES
|
---|
| 192 | glOrthof(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0);
|
---|
| 193 | #else
|
---|
| 194 | glOrtho(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0);
|
---|
| 195 | #endif
|
---|
[2] | 196 | glMatrixMode(GL_MODELVIEW);
|
---|
[561] | 197 | }
|
---|
[2] | 198 |
|
---|
[561] | 199 | void GLWidget::resizeGL(int width, int height)
|
---|
| 200 | {
|
---|
| 201 | glViewport(0, 0, width, height);
|
---|
| 202 | aspect = (qreal)width / (qreal)(height ? height : 1);
|
---|
| 203 | perspectiveProjection();
|
---|
| 204 | }
|
---|
[2] | 205 |
|
---|
[561] | 206 | void GLWidget::drawPbuffer()
|
---|
| 207 | {
|
---|
| 208 | orthographicProjection();
|
---|
[2] | 209 |
|
---|
[561] | 210 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
---|
| 211 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
| 212 |
|
---|
| 213 | glDisable(GL_TEXTURE_2D);
|
---|
| 214 | backdrop->draw();
|
---|
| 215 | glEnable(GL_TEXTURE_2D);
|
---|
| 216 |
|
---|
| 217 | glBindTexture(GL_TEXTURE_2D, cubeTexture);
|
---|
| 218 | glDisable(GL_CULL_FACE);
|
---|
| 219 | cube->draw();
|
---|
| 220 | glEnable(GL_CULL_FACE);
|
---|
| 221 |
|
---|
| 222 | glFlush();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | void GLWidget::initPbuffer()
|
---|
| 226 | {
|
---|
| 227 | pbuffer->makeCurrent();
|
---|
| 228 |
|
---|
| 229 | cubeTexture = bindTexture(QImage(":res/cubelogo.png"));
|
---|
| 230 |
|
---|
| 231 | initCommon();
|
---|
| 232 |
|
---|
[2] | 233 | // generate a texture that has the same size/format as the pbuffer
|
---|
| 234 | dynamicTexture = pbuffer->generateDynamicTexture();
|
---|
| 235 |
|
---|
| 236 | // bind the dynamic texture to the pbuffer - this is a no-op under X11
|
---|
| 237 | hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture);
|
---|
| 238 | makeCurrent();
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[561] | 241 | void GLWidget::setAnimationPaused(bool enable)
|
---|
| 242 | {
|
---|
| 243 | cube->setAnimationPaused(enable);
|
---|
| 244 | for (int i = 0; i < 3; ++i)
|
---|
| 245 | cubes[i]->setAnimationPaused(enable);
|
---|
| 246 | }
|
---|