author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include "glwidget.h" |
|
42 |
#include <math.h> |
|
43 |
||
44 |
#include "cube.h" |
|
45 |
||
46 |
#include <QGLPixelBuffer> |
|
47 |
||
48 |
#ifndef GL_MULTISAMPLE |
|
49 |
#define GL_MULTISAMPLE 0x809D |
|
50 |
#endif |
|
51 |
||
52 |
static GLfloat colorArray[][4] = { |
|
53 |
{0.243f, 0.423f, 0.125f, 1.0f}, |
|
54 |
{0.176f, 0.31f, 0.09f, 1.0f}, |
|
55 |
{0.4f, 0.69f, 0.212f, 1.0f}, |
|
56 |
{0.317f, 0.553f, 0.161f, 1.0f} |
|
57 |
}; |
|
58 |
||
59 |
GLWidget::GLWidget(QWidget *parent) |
|
60 |
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent) |
|
61 |
, geom(0) |
|
62 |
, cube(0) |
|
63 |
{ |
|
64 |
// create the pbuffer |
|
65 |
pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this); |
|
66 |
setWindowTitle(tr("OpenGL pbuffers")); |
|
67 |
initializeGeometry(); |
|
68 |
} |
|
69 |
||
70 |
GLWidget::~GLWidget() |
|
71 |
{ |
|
72 |
pbuffer->releaseFromDynamicTexture(); |
|
73 |
glDeleteTextures(1, &dynamicTexture); |
|
74 |
delete pbuffer; |
|
75 |
||
76 |
qDeleteAll(cubes); |
|
77 |
qDeleteAll(tiles); |
|
78 |
delete cube; |
|
79 |
} |
|
80 |
||
81 |
void GLWidget::initializeGL() |
|
82 |
{ |
|
83 |
initCommon(); |
|
84 |
glShadeModel(GL_SMOOTH); |
|
85 |
glEnable(GL_LIGHTING); |
|
86 |
glEnable(GL_LIGHT0); |
|
87 |
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 }; |
|
88 |
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); |
|
89 |
initPbuffer(); |
|
90 |
cube->startAnimation(); |
|
91 |
connect(cube, SIGNAL(changed()), this, SLOT(update())); |
|
92 |
for (int i = 0; i < 3; ++i) |
|
93 |
{ |
|
94 |
cubes[i]->startAnimation(); |
|
95 |
connect(cubes[i], SIGNAL(changed()), this, SLOT(update())); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
void GLWidget::paintGL() |
|
100 |
{ |
|
101 |
pbuffer->makeCurrent(); |
|
102 |
drawPbuffer(); |
|
103 |
// On direct render platforms, drawing onto the pbuffer context above |
|
104 |
// automatically updates the dynamic texture. For cases where rendering |
|
105 |
// directly to a texture is not supported, explicitly copy. |
|
106 |
if (!hasDynamicTextureUpdate) |
|
107 |
pbuffer->updateDynamicTexture(dynamicTexture); |
|
108 |
makeCurrent(); |
|
109 |
||
110 |
// Use the pbuffer as a texture to render the scene |
|
111 |
glBindTexture(GL_TEXTURE_2D, dynamicTexture); |
|
112 |
||
113 |
// set up to render the scene |
|
114 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
115 |
glLoadIdentity(); |
|
116 |
glTranslatef(0.0f, 0.0f, -10.0f); |
|
117 |
||
118 |
// draw the background |
|
119 |
glPushMatrix(); |
|
120 |
glScalef(aspect, 1.0f, 1.0f); |
|
121 |
for (int i = 0; i < tiles.count(); ++i) |
|
122 |
tiles[i]->draw(); |
|
123 |
glPopMatrix(); |
|
124 |
||
125 |
// draw the bouncing cubes |
|
126 |
for (int i = 0; i < cubes.count(); ++i) |
|
127 |
cubes[i]->draw(); |
|
128 |
} |
|
129 |
||
130 |
void GLWidget::initializeGeometry() |
|
131 |
{ |
|
132 |
geom = new Geometry(); |
|
133 |
CubeBuilder cBuilder(geom, 0.5); |
|
134 |
cBuilder.setColor(QColor(255, 255, 255, 212)); |
|
135 |
// build the 3 bouncing, spinning cubes |
|
136 |
for (int i = 0; i < 3; ++i) |
|
137 |
cubes.append(cBuilder.newCube(QVector3D((float)(i-1), -1.5f, 5 - i))); |
|
138 |
||
139 |
// build the spinning cube which goes in the dynamic texture |
|
140 |
cube = cBuilder.newCube(); |
|
141 |
cube->removeBounce(); |
|
142 |
||
143 |
// build the background tiles |
|
144 |
TileBuilder tBuilder(geom); |
|
145 |
tBuilder.setColor(QColor(Qt::white)); |
|
146 |
for (int c = -2; c <= +2; ++c) |
|
147 |
for (int r = -2; r <= +2; ++r) |
|
148 |
tiles.append(tBuilder.newTile(QVector3D(c, r, 0))); |
|
149 |
||
150 |
// graded backdrop for the pbuffer scene |
|
151 |
TileBuilder bBuilder(geom, 0.0f, 2.0f); |
|
152 |
bBuilder.setColor(QColor(102, 176, 54, 210)); |
|
153 |
backdrop = bBuilder.newTile(QVector3D(0.0f, 0.0f, -1.5f)); |
|
154 |
backdrop->setColors(colorArray); |
|
155 |
} |
|
156 |
||
157 |
void GLWidget::initCommon() |
|
158 |
{ |
|
159 |
qglClearColor(QColor(Qt::darkGray)); |
|
160 |
||
161 |
glEnable(GL_DEPTH_TEST); |
|
162 |
glEnable(GL_CULL_FACE); |
|
163 |
glEnable(GL_MULTISAMPLE); |
|
164 |
||
165 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
166 |
glEnable(GL_BLEND); |
|
167 |
||
168 |
glEnable(GL_TEXTURE_2D); |
|
169 |
||
170 |
geom->loadArrays(); |
|
171 |
} |
|
172 |
||
173 |
void GLWidget::perspectiveProjection() |
|
174 |
{ |
|
175 |
glMatrixMode(GL_PROJECTION); |
|
176 |
glLoadIdentity(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
#ifdef QT_OPENGL_ES |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
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
|
179 |
#else |
0 | 180 |
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
|
181 |
#endif |
0 | 182 |
glMatrixMode(GL_MODELVIEW); |
183 |
} |
|
184 |
||
185 |
void GLWidget::orthographicProjection() |
|
186 |
{ |
|
187 |
glMatrixMode(GL_PROJECTION); |
|
188 |
glLoadIdentity(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
189 |
#ifdef QT_OPENGL_ES |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
190 |
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
|
191 |
#else |
0 | 192 |
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
|
193 |
#endif |
0 | 194 |
glMatrixMode(GL_MODELVIEW); |
195 |
} |
|
196 |
||
197 |
void GLWidget::resizeGL(int width, int height) |
|
198 |
{ |
|
199 |
glViewport(0, 0, width, height); |
|
200 |
aspect = (qreal)width / (qreal)(height ? height : 1); |
|
201 |
perspectiveProjection(); |
|
202 |
} |
|
203 |
||
204 |
void GLWidget::drawPbuffer() |
|
205 |
{ |
|
206 |
orthographicProjection(); |
|
207 |
||
208 |
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
209 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
210 |
||
211 |
glDisable(GL_TEXTURE_2D); |
|
212 |
backdrop->draw(); |
|
213 |
glEnable(GL_TEXTURE_2D); |
|
214 |
||
215 |
glBindTexture(GL_TEXTURE_2D, cubeTexture); |
|
216 |
glDisable(GL_CULL_FACE); |
|
217 |
cube->draw(); |
|
218 |
glEnable(GL_CULL_FACE); |
|
219 |
||
220 |
glFlush(); |
|
221 |
} |
|
222 |
||
223 |
void GLWidget::initPbuffer() |
|
224 |
{ |
|
225 |
pbuffer->makeCurrent(); |
|
226 |
||
227 |
cubeTexture = bindTexture(QImage(":res/cubelogo.png")); |
|
228 |
||
229 |
initCommon(); |
|
230 |
||
231 |
// generate a texture that has the same size/format as the pbuffer |
|
232 |
dynamicTexture = pbuffer->generateDynamicTexture(); |
|
233 |
||
234 |
// bind the dynamic texture to the pbuffer - this is a no-op under X11 |
|
235 |
hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture); |
|
236 |
makeCurrent(); |
|
237 |
} |
|
238 |
||
239 |
void GLWidget::setAnimationPaused(bool enable) |
|
240 |
{ |
|
241 |
cube->setAnimationPaused(enable); |
|
242 |
for (int i = 0; i < 3; ++i) |
|
243 |
cubes[i]->setAnimationPaused(enable); |
|
244 |
} |