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