source: trunk/examples/opengl/pbuffers/glwidget.cpp@ 642

Last change on this file since 642 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 6.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
53static 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
60GLWidget::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
71GLWidget::~GLWidget()
72{
73 pbuffer->releaseFromDynamicTexture();
74 glDeleteTextures(1, &dynamicTexture);
75 delete pbuffer;
76
77 qDeleteAll(cubes);
78 qDeleteAll(tiles);
79 delete cube;
80}
81
82void 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
100void 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
131void 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
158void 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
174void GLWidget::perspectiveProjection()
175{
176 glMatrixMode(GL_PROJECTION);
177 glLoadIdentity();
178#ifdef QT_OPENGL_ES
179 glFrustumf(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0);
180#else
181 glFrustum(-aspect, +aspect, -1.0, +1.0, 4.0, 15.0);
182#endif
183 glMatrixMode(GL_MODELVIEW);
184}
185
186void GLWidget::orthographicProjection()
187{
188 glMatrixMode(GL_PROJECTION);
189 glLoadIdentity();
190#ifdef QT_OPENGL_ES
191 glOrthof(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0);
192#else
193 glOrtho(-1.0, +1.0, -1.0, +1.0, -90.0, +90.0);
194#endif
195 glMatrixMode(GL_MODELVIEW);
196}
197
198void 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
205void 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
224void 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
240void GLWidget::setAnimationPaused(bool enable)
241{
242 cube->setAnimationPaused(enable);
243 for (int i = 0; i < 3; ++i)
244 cubes[i]->setAnimationPaused(enable);
245}
Note: See TracBrowser for help on using the repository browser.