| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "glwidget.h"
|
|---|
| 43 | #include <QtGui/QImage>
|
|---|
| 44 |
|
|---|
| 45 | #include <math.h>
|
|---|
| 46 |
|
|---|
| 47 | static GLint cubeArray[][3] = {
|
|---|
| 48 | {0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
|
|---|
| 49 | {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
|
|---|
| 50 | {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
|
|---|
| 51 | {0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
|
|---|
| 52 | {0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
|
|---|
| 53 | {1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | static GLint cubeTextureArray[][2] = {
|
|---|
| 57 | {0, 0}, {1, 0}, {1, 1}, {0, 1},
|
|---|
| 58 | {0, 0}, {0, 1}, {1, 1}, {1, 0},
|
|---|
| 59 | {0, 0}, {1, 0}, {1, 1}, {0, 1},
|
|---|
| 60 | {1, 0}, {0, 0}, {0, 1}, {1, 1},
|
|---|
| 61 | {0, 0}, {1, 0}, {1, 1}, {0, 1},
|
|---|
| 62 | {1, 0}, {0, 0}, {0, 1}, {1, 1}
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | static GLint faceArray[][2] = {
|
|---|
| 66 | {1, -1}, {1, 1}, {-1, 1}, {-1, -1}
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | static GLubyte colorArray[][4] = {
|
|---|
| 70 | {102, 176, 54, 255},
|
|---|
| 71 | {81, 141, 41, 255},
|
|---|
| 72 | {62, 108, 32, 255},
|
|---|
| 73 | {45, 79, 23, 255}
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | GLWidget::GLWidget(QWidget *parent)
|
|---|
| 77 | : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
|
|---|
| 78 | {
|
|---|
| 79 | // create the pbuffer
|
|---|
| 80 | pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
|
|---|
| 81 | timerId = startTimer(20);
|
|---|
| 82 | setWindowTitle(tr("OpenGL pbuffers"));
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | GLWidget::~GLWidget()
|
|---|
| 86 | {
|
|---|
| 87 | pbuffer->releaseFromDynamicTexture();
|
|---|
| 88 | glDeleteTextures(1, &dynamicTexture);
|
|---|
| 89 | glDeleteLists(pbufferList, 1);
|
|---|
| 90 | delete pbuffer;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void GLWidget::initializeGL()
|
|---|
| 94 | {
|
|---|
| 95 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 96 |
|
|---|
| 97 | glEnable(GL_CULL_FACE);
|
|---|
| 98 | initCommon();
|
|---|
| 99 | initPbuffer();
|
|---|
| 100 |
|
|---|
| 101 | for (int i = 0; i < 3; ++i) {
|
|---|
| 102 | yOffs[i] = 0.0f;
|
|---|
| 103 | xInc[i] = 0.005f;
|
|---|
| 104 | rot[i] = 0.0f;
|
|---|
| 105 | }
|
|---|
| 106 | xOffs[0]= 0.0f;
|
|---|
| 107 | xOffs[1]= 0.5f;
|
|---|
| 108 | xOffs[2]= 1.0f;
|
|---|
| 109 |
|
|---|
| 110 | cubeTexture = bindTexture(QImage(":res/cubelogo.png"));
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void GLWidget::resizeGL(int w, int h)
|
|---|
| 114 | {
|
|---|
| 115 | glViewport(0, 0, w, h);
|
|---|
| 116 | glMatrixMode(GL_PROJECTION);
|
|---|
| 117 | glLoadIdentity();
|
|---|
| 118 | float aspect = w/(float)(h ? h : 1);
|
|---|
| 119 | glFrustum(-aspect, aspect, -1, 1, 10, 100);
|
|---|
| 120 | glTranslatef(-0.5f, -0.5f, -0.5f);
|
|---|
| 121 | glTranslatef(0.0f, 0.0f, -15.0f);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | void GLWidget::paintGL()
|
|---|
| 125 | {
|
|---|
| 126 | // draw a spinning cube into the pbuffer..
|
|---|
| 127 | pbuffer->makeCurrent();
|
|---|
| 128 | glBindTexture(GL_TEXTURE_2D, cubeTexture);
|
|---|
| 129 | glCallList(pbufferList);
|
|---|
| 130 | glFlush();
|
|---|
| 131 |
|
|---|
| 132 | // rendering directly to a texture is not supported on X11 and
|
|---|
| 133 | // some Windows implementations, unfortunately
|
|---|
| 134 | if (!hasDynamicTextureUpdate)
|
|---|
| 135 | pbuffer->updateDynamicTexture(dynamicTexture);
|
|---|
| 136 |
|
|---|
| 137 | // ..and use the pbuffer contents as a texture when rendering the
|
|---|
| 138 | // background and the bouncing cubes
|
|---|
| 139 | makeCurrent();
|
|---|
| 140 | glBindTexture(GL_TEXTURE_2D, dynamicTexture);
|
|---|
| 141 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|---|
| 142 |
|
|---|
| 143 | // draw the background
|
|---|
| 144 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 145 | glPushMatrix();
|
|---|
| 146 | glLoadIdentity();
|
|---|
| 147 | glMatrixMode(GL_PROJECTION);
|
|---|
| 148 | glPushMatrix();
|
|---|
| 149 | glLoadIdentity();
|
|---|
| 150 |
|
|---|
| 151 | glVertexPointer(2, GL_INT, 0, faceArray);
|
|---|
| 152 | glTranslatef(-1.2f, -0.8f, 0.0f);
|
|---|
| 153 | glScalef(0.2f, 0.2f, 0.2f);
|
|---|
| 154 | for (int y = 0; y < 5; ++y) {
|
|---|
| 155 | for (int x = 0; x < 5; ++x) {
|
|---|
| 156 | glTranslatef(2.0f, 0, 0);
|
|---|
| 157 | glColor4f(0.8, 0.8, 0.8, 1.0);
|
|---|
| 158 | glDrawArrays(GL_QUADS, 0, 4);
|
|---|
| 159 | }
|
|---|
| 160 | glTranslatef(-10.0f, 2.0f, 0);
|
|---|
| 161 | }
|
|---|
| 162 | glVertexPointer(3, GL_INT, 0, cubeArray);
|
|---|
| 163 |
|
|---|
| 164 | glPopMatrix();
|
|---|
| 165 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 166 | glPopMatrix();
|
|---|
| 167 |
|
|---|
| 168 | // draw the bouncing cubes
|
|---|
| 169 | drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f);
|
|---|
| 170 | drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f);
|
|---|
| 171 | drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp)
|
|---|
| 175 | {
|
|---|
| 176 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 177 | glLoadIdentity();
|
|---|
| 178 | glTranslatef(xOffs[i], yOffs[i], z);
|
|---|
| 179 | glTranslatef(0.5f, 0.5f, 0.5f);
|
|---|
| 180 | GLfloat scale = 0.75 + i*(0.25f/2);
|
|---|
| 181 | glScalef(scale, scale, scale);
|
|---|
| 182 | glRotatef(rot[i], 1.0f, 1.0f, 1.0f);
|
|---|
| 183 | glTranslatef(-0.5f, -0.5f, -0.5f);
|
|---|
| 184 |
|
|---|
| 185 | glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
|
|---|
| 186 | glDrawArrays(GL_QUADS, 0, 24);
|
|---|
| 187 |
|
|---|
| 188 | if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) {
|
|---|
| 189 | xInc[i] = -xInc[i];
|
|---|
| 190 | xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f;
|
|---|
| 191 | }
|
|---|
| 192 | xOffs[i] += xInc[i];
|
|---|
| 193 | yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1;
|
|---|
| 194 | rot[i] += rotation;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | void GLWidget::initCommon()
|
|---|
| 198 | {
|
|---|
| 199 | glEnableClientState(GL_VERTEX_ARRAY);
|
|---|
| 200 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|---|
| 201 | glVertexPointer(3, GL_INT, 0, cubeArray);
|
|---|
| 202 | glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
|
|---|
| 203 | glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray);
|
|---|
| 204 |
|
|---|
| 205 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|---|
| 206 | glEnable(GL_BLEND);
|
|---|
| 207 | glEnable(GL_TEXTURE_2D);
|
|---|
| 208 | glEnable(GL_DEPTH_TEST);
|
|---|
| 209 |
|
|---|
| 210 | glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void GLWidget::initPbuffer()
|
|---|
| 214 | {
|
|---|
| 215 | // set up the pbuffer context
|
|---|
| 216 | pbuffer->makeCurrent();
|
|---|
| 217 | initCommon();
|
|---|
| 218 |
|
|---|
| 219 | glViewport(0, 0, pbuffer->size().width(), pbuffer->size().height());
|
|---|
| 220 | glMatrixMode(GL_PROJECTION);
|
|---|
| 221 | glLoadIdentity();
|
|---|
| 222 | glOrtho(-1, 1, -1, 1, -99, 99);
|
|---|
| 223 | glTranslatef(-0.5f, -0.5f, 0.0f);
|
|---|
| 224 | glMatrixMode(GL_MODELVIEW);
|
|---|
| 225 | glLoadIdentity();
|
|---|
| 226 |
|
|---|
| 227 | pbufferList = glGenLists(1);
|
|---|
| 228 | glNewList(pbufferList, GL_COMPILE);
|
|---|
| 229 | {
|
|---|
| 230 | glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|---|
| 231 |
|
|---|
| 232 | // draw cube background
|
|---|
| 233 | glPushMatrix();
|
|---|
| 234 | glLoadIdentity();
|
|---|
| 235 | glTranslatef(0.5f, 0.5f, -2.0f);
|
|---|
| 236 | glDisable(GL_TEXTURE_2D);
|
|---|
| 237 | glEnableClientState(GL_COLOR_ARRAY);
|
|---|
| 238 | glVertexPointer(2, GL_INT, 0, faceArray);
|
|---|
| 239 | glDrawArrays(GL_QUADS, 0, 4);
|
|---|
| 240 | glVertexPointer(3, GL_INT, 0, cubeArray);
|
|---|
| 241 | glDisableClientState(GL_COLOR_ARRAY);
|
|---|
| 242 | glEnable(GL_TEXTURE_2D);
|
|---|
| 243 | glPopMatrix();
|
|---|
| 244 |
|
|---|
| 245 | // draw cube
|
|---|
| 246 | glTranslatef(0.5f, 0.5f, 0.5f);
|
|---|
| 247 | glRotatef(3.0f, 1.0f, 1.0f, 1.0f);
|
|---|
| 248 | glTranslatef(-0.5f, -0.5f, -0.5f);
|
|---|
| 249 | glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
|
|---|
| 250 | glDrawArrays(GL_QUADS, 0, 24);
|
|---|
| 251 | }
|
|---|
| 252 | glEndList();
|
|---|
| 253 | // generate a texture that has the same size/format as the pbuffer
|
|---|
| 254 | dynamicTexture = pbuffer->generateDynamicTexture();
|
|---|
| 255 |
|
|---|
| 256 | // bind the dynamic texture to the pbuffer - this is a no-op under X11
|
|---|
| 257 | hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture);
|
|---|
| 258 | makeCurrent();
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|