author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
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(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
#ifdef QT_OPENGL_ES |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
glFrustumf(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
#else |
0 | 181 |
glFrustum(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
#endif |
0 | 183 |
glMatrixMode(GL_MODELVIEW); |
184 |
} |
|
185 |
||
186 |
void GLWidget::orthographicProjection() |
|
187 |
{ |
|
188 |
glMatrixMode(GL_PROJECTION); |
|
189 |
glLoadIdentity(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
190 |
#ifdef QT_OPENGL_ES |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
191 |
glOrthof(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
192 |
#else |
0 | 193 |
glOrtho(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
194 |
#endif |
0 | 195 |
glMatrixMode(GL_MODELVIEW); |
196 |
} |
|
197 |
||
198 |
void GLWidget::resizeGL(int width, int height) |
|
199 |
{ |
|
200 |
glViewport(0, 0, width, height); |
|
201 |
aspect = (qreal)width / (qreal)(height ? height : 1); |
|
202 |
perspectiveProjection(); |
|
203 |
} |
|
204 |
||
205 |
void GLWidget::drawPbuffer() |
|
206 |
{ |
|
207 |
orthographicProjection(); |
|
208 |
||
209 |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
210 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
211 |
||
212 |
glDisable(GL_TEXTURE_2D); |
|
213 |
backdrop->draw(); |
|
214 |
glEnable(GL_TEXTURE_2D); |
|
215 |
||
216 |
glBindTexture(GL_TEXTURE_2D, cubeTexture); |
|
217 |
glDisable(GL_CULL_FACE); |
|
218 |
cube->draw(); |
|
219 |
glEnable(GL_CULL_FACE); |
|
220 |
||
221 |
glFlush(); |
|
222 |
} |
|
223 |
||
224 |
void GLWidget::initPbuffer() |
|
225 |
{ |
|
226 |
pbuffer->makeCurrent(); |
|
227 |
||
228 |
cubeTexture = bindTexture(QImage(":res/cubelogo.png")); |
|
229 |
||
230 |
initCommon(); |
|
231 |
||
232 |
// generate a texture that has the same size/format as the pbuffer |
|
233 |
dynamicTexture = pbuffer->generateDynamicTexture(); |
|
234 |
||
235 |
// bind the dynamic texture to the pbuffer - this is a no-op under X11 |
|
236 |
hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture); |
|
237 |
makeCurrent(); |
|
238 |
} |
|
239 |
||
240 |
void GLWidget::setAnimationPaused(bool enable) |
|
241 |
{ |
|
242 |
cube->setAnimationPaused(enable); |
|
243 |
for (int i = 0; i < 3; ++i) |
|
244 |
cubes[i]->setAnimationPaused(enable); |
|
245 |
} |