| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the examples of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** BSD License Usage |
| 18 |
** Alternatively, you may use this file under the terms of the BSD license |
| 19 |
** as follows: |
| 20 |
** |
| 21 |
** "Redistribution and use in source and binary forms, with or without |
| 22 |
** modification, are permitted provided that the following conditions are |
| 23 |
** met: |
| 24 |
** * Redistributions of source code must retain the above copyright |
| 25 |
** notice, this list of conditions and the following disclaimer. |
| 26 |
** * Redistributions in binary form must reproduce the above copyright |
| 27 |
** notice, this list of conditions and the following disclaimer in |
| 28 |
** the documentation and/or other materials provided with the |
| 29 |
** distribution. |
| 30 |
** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 |
** contributors may be used to endorse or promote products derived |
| 32 |
** from this software without specific prior written permission. |
| 33 |
** |
| 34 |
** |
| 35 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 |
** |
| 47 |
** $QT_END_LICENSE$ |
| 48 |
** |
| 49 |
****************************************************************************/ |
| 50 |
|
| 51 |
#include "glwidget.h" |
| 52 |
#include <math.h> |
| 53 |
|
| 54 |
#include "cube.h" |
| 55 |
|
| 56 |
#include <QGLPixelBuffer> |
| 57 |
|
| 58 |
#ifndef GL_MULTISAMPLE |
| 59 |
#define GL_MULTISAMPLE 0x809D |
| 60 |
#endif |
| 61 |
|
| 62 |
static GLfloat colorArray[][4] = { |
| 63 |
{0.243f, 0.423f, 0.125f, 1.0f}, |
| 64 |
{0.176f, 0.31f, 0.09f, 1.0f}, |
| 65 |
{0.4f, 0.69f, 0.212f, 1.0f}, |
| 66 |
{0.317f, 0.553f, 0.161f, 1.0f} |
| 67 |
}; |
| 68 |
|
| 69 |
GLWidget::GLWidget(QWidget *parent) |
| 70 |
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent) |
| 71 |
, geom(0) |
| 72 |
, cube(0) |
| 73 |
{ |
| 74 |
// create the pbuffer |
| 75 |
QGLFormat pbufferFormat = format(); |
| 76 |
pbufferFormat.setSampleBuffers(false); |
| 77 |
pbuffer = new QGLPixelBuffer(QSize(512, 512), pbufferFormat, this); |
| 78 |
setWindowTitle(tr("OpenGL pbuffers")); |
| 79 |
initializeGeometry(); |
| 80 |
} |
| 81 |
|
| 82 |
GLWidget::~GLWidget() |
| 83 |
{ |
| 84 |
pbuffer->releaseFromDynamicTexture(); |
| 85 |
glDeleteTextures(1, &dynamicTexture); |
| 86 |
delete pbuffer; |
| 87 |
|
| 88 |
qDeleteAll(cubes); |
| 89 |
qDeleteAll(tiles); |
| 90 |
delete cube; |
| 91 |
} |
| 92 |
|
| 93 |
void GLWidget::initializeGL() |
| 94 |
{ |
| 95 |
initCommon(); |
| 96 |
glShadeModel(GL_SMOOTH); |
| 97 |
glEnable(GL_LIGHTING); |
| 98 |
glEnable(GL_LIGHT0); |
| 99 |
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 }; |
| 100 |
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
| 101 |
initPbuffer(); |
| 102 |
cube->startAnimation(); |
| 103 |
connect(cube, SIGNAL(changed()), this, SLOT(update())); |
| 104 |
for (int i = 0; i < 3; ++i) |
| 105 |
{ |
| 106 |
cubes[i]->startAnimation(); |
| 107 |
connect(cubes[i], SIGNAL(changed()), this, SLOT(update())); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void GLWidget::paintGL() |
| 112 |
{ |
| 113 |
pbuffer->makeCurrent(); |
| 114 |
drawPbuffer(); |
| 115 |
// On direct render platforms, drawing onto the pbuffer context above |
| 116 |
// automatically updates the dynamic texture. For cases where rendering |
| 117 |
// directly to a texture is not supported, explicitly copy. |
| 118 |
if (!hasDynamicTextureUpdate) |
| 119 |
pbuffer->updateDynamicTexture(dynamicTexture); |
| 120 |
makeCurrent(); |
| 121 |
|
| 122 |
// Use the pbuffer as a texture to render the scene |
| 123 |
glBindTexture(GL_TEXTURE_2D, dynamicTexture); |
| 124 |
|
| 125 |
// set up to render the scene |
| 126 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 127 |
glLoadIdentity(); |
| 128 |
glTranslatef(0.0f, 0.0f, -10.0f); |
| 129 |
|
| 130 |
// draw the background |
| 131 |
glPushMatrix(); |
| 132 |
glScalef(aspect, 1.0f, 1.0f); |
| 133 |
for (int i = 0; i < tiles.count(); ++i) |
| 134 |
tiles[i]->draw(); |
| 135 |
glPopMatrix(); |
| 136 |
|
| 137 |
// draw the bouncing cubes |
| 138 |
for (int i = 0; i < cubes.count(); ++i) |
| 139 |
cubes[i]->draw(); |
| 140 |
} |
| 141 |
|
| 142 |
void GLWidget::initializeGeometry() |
| 143 |
{ |
| 144 |
geom = new Geometry(); |
| 145 |
CubeBuilder cBuilder(geom, 0.5); |
| 146 |
cBuilder.setColor(QColor(255, 255, 255, 212)); |
| 147 |
// build the 3 bouncing, spinning cubes |
| 148 |
for (int i = 3; i > 0; --i) |
| 149 |
cubes.append(cBuilder.newCube(QVector3D((float)(i-1), -1.5f, 5 - i))); |
| 150 |
|
| 151 |
// build the spinning cube which goes in the dynamic texture |
| 152 |
cube = cBuilder.newCube(); |
| 153 |
cube->removeBounce(); |
| 154 |
|
| 155 |
// build the background tiles |
| 156 |
TileBuilder tBuilder(geom); |
| 157 |
tBuilder.setColor(QColor(Qt::white)); |
| 158 |
for (int c = -2; c <= +2; ++c) |
| 159 |
for (int r = -2; r <= +2; ++r) |
| 160 |
tiles.append(tBuilder.newTile(QVector3D(c, r, 0))); |
| 161 |
|
| 162 |
// graded backdrop for the pbuffer scene |
| 163 |
TileBuilder bBuilder(geom, 0.0f, 2.0f); |
| 164 |
bBuilder.setColor(QColor(102, 176, 54, 210)); |
| 165 |
backdrop = bBuilder.newTile(QVector3D(0.0f, 0.0f, -1.5f)); |
| 166 |
backdrop->setColors(colorArray); |
| 167 |
} |
| 168 |
|
| 169 |
void GLWidget::initCommon() |
| 170 |
{ |
| 171 |
qglClearColor(QColor(Qt::darkGray)); |
| 172 |
|
| 173 |
glEnable(GL_DEPTH_TEST); |
| 174 |
glEnable(GL_CULL_FACE); |
| 175 |
glEnable(GL_MULTISAMPLE); |
| 176 |
|
| 177 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 178 |
glEnable(GL_BLEND); |
| 179 |
|
| 180 |
glEnable(GL_TEXTURE_2D); |
| 181 |
|
| 182 |
geom->loadArrays(); |
| 183 |
} |
| 184 |
|
| 185 |
void GLWidget::perspectiveProjection() |
| 186 |
{ |
| 187 |
glMatrixMode(GL_PROJECTION); |
| 188 |
glLoadIdentity(); |
| 189 |
#ifdef QT_OPENGL_ES |
| 190 |
glFrustumf(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0); |
| 191 |
#else |
| 192 |
glFrustum(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0); |
| 193 |
#endif |
| 194 |
glMatrixMode(GL_MODELVIEW); |
| 195 |
} |
| 196 |
|
| 197 |
void GLWidget::orthographicProjection() |
| 198 |
{ |
| 199 |
glMatrixMode(GL_PROJECTION); |
| 200 |
glLoadIdentity(); |
| 201 |
#ifdef QT_OPENGL_ES |
| 202 |
glOrthof(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0); |
| 203 |
#else |
| 204 |
glOrtho(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0); |
| 205 |
#endif |
| 206 |
glMatrixMode(GL_MODELVIEW); |
| 207 |
} |
| 208 |
|
| 209 |
void GLWidget::resizeGL(int width, int height) |
| 210 |
{ |
| 211 |
glViewport(0, 0, width * devicePixelRatio(), height * devicePixelRatio()); |
| 212 |
aspect = (qreal)width / (qreal)(height ? height : 1); |
| 213 |
perspectiveProjection(); |
| 214 |
} |
| 215 |
|
| 216 |
void GLWidget::drawPbuffer() |
| 217 |
{ |
| 218 |
orthographicProjection(); |
| 219 |
|
| 220 |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 221 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 222 |
|
| 223 |
glDisable(GL_TEXTURE_2D); |
| 224 |
backdrop->draw(); |
| 225 |
glEnable(GL_TEXTURE_2D); |
| 226 |
|
| 227 |
glBindTexture(GL_TEXTURE_2D, cubeTexture); |
| 228 |
glDisable(GL_CULL_FACE); |
| 229 |
cube->draw(); |
| 230 |
glEnable(GL_CULL_FACE); |
| 231 |
|
| 232 |
glFlush(); |
| 233 |
} |
| 234 |
|
| 235 |
void GLWidget::initPbuffer() |
| 236 |
{ |
| 237 |
pbuffer->makeCurrent(); |
| 238 |
|
| 239 |
cubeTexture = bindTexture(QImage(":res/cubelogo.png")); |
| 240 |
|
| 241 |
initCommon(); |
| 242 |
|
| 243 |
// generate a texture that has the same size/format as the pbuffer |
| 244 |
dynamicTexture = pbuffer->generateDynamicTexture(); |
| 245 |
|
| 246 |
// bind the dynamic texture to the pbuffer - this is a no-op under X11 |
| 247 |
hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture); |
| 248 |
makeCurrent(); |
| 249 |
} |
| 250 |
|
| 251 |
void GLWidget::setAnimationPaused(bool enable) |
| 252 |
{ |
| 253 |
cube->setAnimationPaused(enable); |
| 254 |
for (int i = 0; i < 3; ++i) |
| 255 |
cubes[i]->setAnimationPaused(enable); |
| 256 |
} |