0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the examples of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include "glwidget.h"
|
|
43 |
#include <math.h>
|
|
44 |
|
|
45 |
#include "cube.h"
|
|
46 |
|
|
47 |
#include <QGLPixelBuffer>
|
|
48 |
|
|
49 |
#ifndef GL_MULTISAMPLE
|
|
50 |
#define GL_MULTISAMPLE 0x809D
|
|
51 |
#endif
|
|
52 |
|
|
53 |
static GLfloat colorArray[][4] = {
|
|
54 |
{0.243f, 0.423f, 0.125f, 1.0f},
|
|
55 |
{0.176f, 0.31f, 0.09f, 1.0f},
|
|
56 |
{0.4f, 0.69f, 0.212f, 1.0f},
|
|
57 |
{0.317f, 0.553f, 0.161f, 1.0f}
|
|
58 |
};
|
|
59 |
|
|
60 |
GLWidget::GLWidget(QWidget *parent)
|
|
61 |
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
|
|
62 |
, geom(0)
|
|
63 |
, cube(0)
|
|
64 |
{
|
|
65 |
// create the pbuffer
|
|
66 |
pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
|
|
67 |
setWindowTitle(tr("OpenGL pbuffers"));
|
|
68 |
initializeGeometry();
|
|
69 |
}
|
|
70 |
|
|
71 |
GLWidget::~GLWidget()
|
|
72 |
{
|
|
73 |
pbuffer->releaseFromDynamicTexture();
|
|
74 |
glDeleteTextures(1, &dynamicTexture);
|
|
75 |
delete pbuffer;
|
|
76 |
|
|
77 |
qDeleteAll(cubes);
|
|
78 |
qDeleteAll(tiles);
|
|
79 |
delete cube;
|
|
80 |
}
|
|
81 |
|
|
82 |
void GLWidget::initializeGL()
|
|
83 |
{
|
|
84 |
initCommon();
|
|
85 |
glShadeModel(GL_SMOOTH);
|
|
86 |
glEnable(GL_LIGHTING);
|
|
87 |
glEnable(GL_LIGHT0);
|
|
88 |
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
|
|
89 |
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
|
90 |
initPbuffer();
|
|
91 |
cube->startAnimation();
|
|
92 |
connect(cube, SIGNAL(changed()), this, SLOT(update()));
|
|
93 |
for (int i = 0; i < 3; ++i)
|
|
94 |
{
|
|
95 |
cubes[i]->startAnimation();
|
|
96 |
connect(cubes[i], SIGNAL(changed()), this, SLOT(update()));
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
void GLWidget::paintGL()
|
|
101 |
{
|
|
102 |
pbuffer->makeCurrent();
|
|
103 |
drawPbuffer();
|
|
104 |
// On direct render platforms, drawing onto the pbuffer context above
|
|
105 |
// automatically updates the dynamic texture. For cases where rendering
|
|
106 |
// directly to a texture is not supported, explicitly copy.
|
|
107 |
if (!hasDynamicTextureUpdate)
|
|
108 |
pbuffer->updateDynamicTexture(dynamicTexture);
|
|
109 |
makeCurrent();
|
|
110 |
|
|
111 |
// Use the pbuffer as a texture to render the scene
|
|
112 |
glBindTexture(GL_TEXTURE_2D, dynamicTexture);
|
|
113 |
|
|
114 |
// set up to render the scene
|
|
115 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
116 |
glLoadIdentity();
|
|
117 |
glTranslatef(0.0f, 0.0f, -10.0f);
|
|
118 |
|
|
119 |
// draw the background
|
|
120 |
glPushMatrix();
|
|
121 |
glScalef(aspect, 1.0f, 1.0f);
|
|
122 |
for (int i = 0; i < tiles.count(); ++i)
|
|
123 |
tiles[i]->draw();
|
|
124 |
glPopMatrix();
|
|
125 |
|
|
126 |
// draw the bouncing cubes
|
|
127 |
for (int i = 0; i < cubes.count(); ++i)
|
|
128 |
cubes[i]->draw();
|
|
129 |
}
|
|
130 |
|
|
131 |
void GLWidget::initializeGeometry()
|
|
132 |
{
|
|
133 |
geom = new Geometry();
|
|
134 |
CubeBuilder cBuilder(geom, 0.5);
|
|
135 |
cBuilder.setColor(QColor(255, 255, 255, 212));
|
|
136 |
// build the 3 bouncing, spinning cubes
|
|
137 |
for (int i = 0; i < 3; ++i)
|
|
138 |
cubes.append(cBuilder.newCube(QVector3D((float)(i-1), -1.5f, 5 - i)));
|
|
139 |
|
|
140 |
// build the spinning cube which goes in the dynamic texture
|
|
141 |
cube = cBuilder.newCube();
|
|
142 |
cube->removeBounce();
|
|
143 |
|
|
144 |
// build the background tiles
|
|
145 |
TileBuilder tBuilder(geom);
|
|
146 |
tBuilder.setColor(QColor(Qt::white));
|
|
147 |
for (int c = -2; c <= +2; ++c)
|
|
148 |
for (int r = -2; r <= +2; ++r)
|
|
149 |
tiles.append(tBuilder.newTile(QVector3D(c, r, 0)));
|
|
150 |
|
|
151 |
// graded backdrop for the pbuffer scene
|
|
152 |
TileBuilder bBuilder(geom, 0.0f, 2.0f);
|
|
153 |
bBuilder.setColor(QColor(102, 176, 54, 210));
|
|
154 |
backdrop = bBuilder.newTile(QVector3D(0.0f, 0.0f, -1.5f));
|
|
155 |
backdrop->setColors(colorArray);
|
|
156 |
}
|
|
157 |
|
|
158 |
void GLWidget::initCommon()
|
|
159 |
{
|
|
160 |
qglClearColor(QColor(Qt::darkGray));
|
|
161 |
|
|
162 |
glEnable(GL_DEPTH_TEST);
|
|
163 |
glEnable(GL_CULL_FACE);
|
|
164 |
glEnable(GL_MULTISAMPLE);
|
|
165 |
|
|
166 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
167 |
glEnable(GL_BLEND);
|
|
168 |
|
|
169 |
glEnable(GL_TEXTURE_2D);
|
|
170 |
|
|
171 |
geom->loadArrays();
|
|
172 |
}
|
|
173 |
|
|
174 |
void GLWidget::perspectiveProjection()
|
|
175 |
{
|
|
176 |
glMatrixMode(GL_PROJECTION);
|
|
177 |
glLoadIdentity();
|
|
178 |
glFrustum(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0);
|
|
179 |
glMatrixMode(GL_MODELVIEW);
|
|
180 |
}
|
|
181 |
|
|
182 |
void GLWidget::orthographicProjection()
|
|
183 |
{
|
|
184 |
glMatrixMode(GL_PROJECTION);
|
|
185 |
glLoadIdentity();
|
|
186 |
glOrtho(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0);
|
|
187 |
glMatrixMode(GL_MODELVIEW);
|
|
188 |
}
|
|
189 |
|
|
190 |
void GLWidget::resizeGL(int width, int height)
|
|
191 |
{
|
|
192 |
glViewport(0, 0, width, height);
|
|
193 |
aspect = (qreal)width / (qreal)(height ? height : 1);
|
|
194 |
perspectiveProjection();
|
|
195 |
}
|
|
196 |
|
|
197 |
void GLWidget::drawPbuffer()
|
|
198 |
{
|
|
199 |
orthographicProjection();
|
|
200 |
|
|
201 |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
202 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
203 |
|
|
204 |
glDisable(GL_TEXTURE_2D);
|
|
205 |
backdrop->draw();
|
|
206 |
glEnable(GL_TEXTURE_2D);
|
|
207 |
|
|
208 |
glBindTexture(GL_TEXTURE_2D, cubeTexture);
|
|
209 |
glDisable(GL_CULL_FACE);
|
|
210 |
cube->draw();
|
|
211 |
glEnable(GL_CULL_FACE);
|
|
212 |
|
|
213 |
glFlush();
|
|
214 |
}
|
|
215 |
|
|
216 |
void GLWidget::initPbuffer()
|
|
217 |
{
|
|
218 |
pbuffer->makeCurrent();
|
|
219 |
|
|
220 |
cubeTexture = bindTexture(QImage(":res/cubelogo.png"));
|
|
221 |
|
|
222 |
initCommon();
|
|
223 |
|
|
224 |
// generate a texture that has the same size/format as the pbuffer
|
|
225 |
dynamicTexture = pbuffer->generateDynamicTexture();
|
|
226 |
|
|
227 |
// bind the dynamic texture to the pbuffer - this is a no-op under X11
|
|
228 |
hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture);
|
|
229 |
makeCurrent();
|
|
230 |
}
|
|
231 |
|
|
232 |
void GLWidget::setAnimationPaused(bool enable)
|
|
233 |
{
|
|
234 |
cube->setAnimationPaused(enable);
|
|
235 |
for (int i = 0; i < 3; ++i)
|
|
236 |
cubes[i]->setAnimationPaused(enable);
|
|
237 |
}
|