| 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 <QtGui/QImage> |
| 53 |
#include <qmath.h> |
| 54 |
|
| 55 |
static GLint cubeArray[][3] = { |
| 56 |
{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, |
| 57 |
{0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}, |
| 58 |
{0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}, |
| 59 |
{0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0}, |
| 60 |
{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}, |
| 61 |
{1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1} |
| 62 |
}; |
| 63 |
|
| 64 |
static GLint cubeTextureArray[][2] = { |
| 65 |
{0, 0}, {1, 0}, {1, 1}, {0, 1}, |
| 66 |
{0, 0}, {0, 1}, {1, 1}, {1, 0}, |
| 67 |
{0, 0}, {1, 0}, {1, 1}, {0, 1}, |
| 68 |
{1, 0}, {0, 0}, {0, 1}, {1, 1}, |
| 69 |
{0, 0}, {1, 0}, {1, 1}, {0, 1}, |
| 70 |
{1, 0}, {0, 0}, {0, 1}, {1, 1} |
| 71 |
}; |
| 72 |
|
| 73 |
static GLint faceArray[][2] = { |
| 74 |
{1, -1}, {1, 1}, {-1, 1}, {-1, -1} |
| 75 |
}; |
| 76 |
|
| 77 |
static GLubyte colorArray[][4] = { |
| 78 |
{102, 176, 54, 255}, |
| 79 |
{81, 141, 41, 255}, |
| 80 |
{62, 108, 32, 255}, |
| 81 |
{45, 79, 23, 255} |
| 82 |
}; |
| 83 |
|
| 84 |
GLWidget::GLWidget(QWidget *parent) |
| 85 |
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent) |
| 86 |
{ |
| 87 |
// create the framebuffer object - make sure to have a current |
| 88 |
// context before creating it |
| 89 |
makeCurrent(); |
| 90 |
fbo = new QGLFramebufferObject(512, 512); |
| 91 |
timerId = startTimer(20); |
| 92 |
setWindowTitle(tr("OpenGL framebuffer objects 2")); |
| 93 |
} |
| 94 |
|
| 95 |
GLWidget::~GLWidget() |
| 96 |
{ |
| 97 |
glDeleteLists(pbufferList, 1); |
| 98 |
delete fbo; |
| 99 |
} |
| 100 |
|
| 101 |
void GLWidget::initializeGL() |
| 102 |
{ |
| 103 |
glMatrixMode(GL_MODELVIEW); |
| 104 |
|
| 105 |
glEnable(GL_CULL_FACE); |
| 106 |
|
| 107 |
glEnableClientState(GL_VERTEX_ARRAY); |
| 108 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 109 |
glVertexPointer(3, GL_INT, 0, cubeArray); |
| 110 |
glTexCoordPointer(2, GL_INT, 0, cubeTextureArray); |
| 111 |
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray); |
| 112 |
|
| 113 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 114 |
glEnable(GL_BLEND); |
| 115 |
glEnable(GL_TEXTURE_2D); |
| 116 |
glEnable(GL_DEPTH_TEST); |
| 117 |
|
| 118 |
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); |
| 119 |
pbufferList = glGenLists(1); |
| 120 |
glNewList(pbufferList, GL_COMPILE); |
| 121 |
{ |
| 122 |
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 123 |
|
| 124 |
// draw cube background |
| 125 |
glPushMatrix(); |
| 126 |
glLoadIdentity(); |
| 127 |
glTranslatef(0.5f, 0.5f, -2.0f); |
| 128 |
glDisable(GL_TEXTURE_2D); |
| 129 |
glEnableClientState(GL_COLOR_ARRAY); |
| 130 |
glVertexPointer(2, GL_INT, 0, faceArray); |
| 131 |
glDrawArrays(GL_QUADS, 0, 4); |
| 132 |
glVertexPointer(3, GL_INT, 0, cubeArray); |
| 133 |
glDisableClientState(GL_COLOR_ARRAY); |
| 134 |
glEnable(GL_TEXTURE_2D); |
| 135 |
glPopMatrix(); |
| 136 |
|
| 137 |
// draw cube |
| 138 |
glTranslatef(0.5f, 0.5f, 0.5f); |
| 139 |
glRotatef(3.0f, 1.0f, 1.0f, 1.0f); |
| 140 |
glTranslatef(-0.5f, -0.5f, -0.5f); |
| 141 |
glColor4f(0.9f, 0.9f, 0.9f, 1.0f); |
| 142 |
glDrawArrays(GL_QUADS, 0, 24); |
| 143 |
|
| 144 |
glPushMatrix(); // this state is popped back in the paintGL() function |
| 145 |
} |
| 146 |
glEndList(); |
| 147 |
|
| 148 |
for (int i = 0; i < 3; ++i) { |
| 149 |
yOffs[i] = 0.0f; |
| 150 |
xInc[i] = 0.005f; |
| 151 |
rot[i] = 0.0f; |
| 152 |
} |
| 153 |
xOffs[0]= 0.0f; |
| 154 |
xOffs[1]= 0.5f; |
| 155 |
xOffs[2]= 1.0f; |
| 156 |
|
| 157 |
cubeTexture = bindTexture(QImage(":res/cubelogo.png")); |
| 158 |
|
| 159 |
glPushMatrix(); // push to avoid stack underflow in the first paintGL() call |
| 160 |
} |
| 161 |
|
| 162 |
void GLWidget::resizeGL(int w, int h) |
| 163 |
{ |
| 164 |
glViewport(0, 0, w, h); |
| 165 |
glMatrixMode(GL_PROJECTION); |
| 166 |
glLoadIdentity(); |
| 167 |
float aspect = w/(float)(h ? h : 1); |
| 168 |
glFrustum(-aspect, aspect, -1, 1, 10, 100); |
| 169 |
glTranslatef(-0.5f, -0.5f, -0.5f); |
| 170 |
glTranslatef(0.0f, 0.0f, -15.0f); |
| 171 |
} |
| 172 |
|
| 173 |
void GLWidget::paintGL() |
| 174 |
{ |
| 175 |
glPopMatrix(); // pop the matrix pushed in the pbuffer list |
| 176 |
|
| 177 |
// push the projection matrix and the entire GL state before |
| 178 |
// doing any rendering into our framebuffer object |
| 179 |
glPushAttrib(GL_ALL_ATTRIB_BITS); |
| 180 |
glMatrixMode(GL_PROJECTION); |
| 181 |
glPushMatrix(); |
| 182 |
|
| 183 |
glViewport(0, 0, fbo->size().width(), fbo->size().height()); |
| 184 |
glMatrixMode(GL_PROJECTION); |
| 185 |
glLoadIdentity(); |
| 186 |
glOrtho(-1, 1, -1, 1, -99, 99); |
| 187 |
glTranslatef(-0.5f, -0.5f, 0.0f); |
| 188 |
glMatrixMode(GL_MODELVIEW); |
| 189 |
|
| 190 |
// render to the framebuffer object |
| 191 |
fbo->bind(); |
| 192 |
glBindTexture(GL_TEXTURE_2D, cubeTexture); |
| 193 |
glCallList(pbufferList); |
| 194 |
fbo->release(); |
| 195 |
|
| 196 |
// pop the projection matrix and GL state back for rendering |
| 197 |
// to the actual widget |
| 198 |
glPopAttrib(); |
| 199 |
glMatrixMode(GL_PROJECTION); |
| 200 |
glPopMatrix(); |
| 201 |
|
| 202 |
glBindTexture(GL_TEXTURE_2D, fbo->texture()); |
| 203 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 204 |
|
| 205 |
// draw the background |
| 206 |
glMatrixMode(GL_MODELVIEW); |
| 207 |
glPushMatrix(); |
| 208 |
glLoadIdentity(); |
| 209 |
glMatrixMode(GL_PROJECTION); |
| 210 |
glPushMatrix(); |
| 211 |
glLoadIdentity(); |
| 212 |
|
| 213 |
glVertexPointer(2, GL_INT, 0, faceArray); |
| 214 |
glTranslatef(-1.2f, -0.8f, 0.0f); |
| 215 |
glScalef(0.2f, 0.2f, 0.2f); |
| 216 |
for (int y = 0; y < 5; ++y) { |
| 217 |
for (int x = 0; x < 5; ++x) { |
| 218 |
glTranslatef(2.0f, 0, 0); |
| 219 |
glColor4f(0.8f, 0.8f, 0.8f, 1.0f); |
| 220 |
glDrawArrays(GL_QUADS, 0, 4); |
| 221 |
} |
| 222 |
glTranslatef(-10.0f, 2.0f, 0); |
| 223 |
} |
| 224 |
glVertexPointer(3, GL_INT, 0, cubeArray); |
| 225 |
|
| 226 |
glPopMatrix(); |
| 227 |
glMatrixMode(GL_MODELVIEW); |
| 228 |
|
| 229 |
// draw the bouncing cubes |
| 230 |
drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f); |
| 231 |
drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f); |
| 232 |
drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f); |
| 233 |
glPopMatrix(); |
| 234 |
} |
| 235 |
|
| 236 |
void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp) |
| 237 |
{ |
| 238 |
glMatrixMode(GL_MODELVIEW); |
| 239 |
glLoadIdentity(); |
| 240 |
glTranslatef(xOffs[i], yOffs[i], z); |
| 241 |
glTranslatef(0.5f, 0.5f, 0.5f); |
| 242 |
GLfloat scale = 0.75 + i*(0.25f/2); |
| 243 |
glScalef(scale, scale, scale); |
| 244 |
glRotatef(rot[i], 1.0f, 1.0f, 1.0f); |
| 245 |
glTranslatef(-0.5f, -0.5f, -0.5f); |
| 246 |
|
| 247 |
glColor4f(1.0f, 1.0f, 1.0f, 0.8f); |
| 248 |
glDrawArrays(GL_QUADS, 0, 24); |
| 249 |
|
| 250 |
if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) { |
| 251 |
xInc[i] = -xInc[i]; |
| 252 |
xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f; |
| 253 |
} |
| 254 |
xOffs[i] += xInc[i]; |
| 255 |
yOffs[i] = qAbs(cos((-GLfloat(M_PI) * jmp) * xOffs[i]) * amp) - 1; |
| 256 |
rot[i] += rotation; |
| 257 |
} |