1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "glwidget.h"
#include <QtGui/QImage>
#include <math.h>
static GLint cubeArray[][3] = {
{0, 0, 0}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0},
{0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1},
{0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1},
{0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0},
{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1},
{1, 0, 0}, {1, 1, 0}, {1, 1, 1}, {1, 0, 1}
};
static GLint cubeTextureArray[][2] = {
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{0, 0}, {0, 1}, {1, 1}, {1, 0},
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{1, 0}, {0, 0}, {0, 1}, {1, 1},
{0, 0}, {1, 0}, {1, 1}, {0, 1},
{1, 0}, {0, 0}, {0, 1}, {1, 1}
};
static GLint faceArray[][2] = {
{1, -1}, {1, 1}, {-1, 1}, {-1, -1}
};
static GLubyte colorArray[][4] = {
{102, 176, 54, 255},
{81, 141, 41, 255},
{62, 108, 32, 255},
{45, 79, 23, 255}
};
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
// create the pbuffer
pbuffer = new QGLPixelBuffer(QSize(512, 512), format(), this);
timerId = startTimer(20);
setWindowTitle(tr("OpenGL pbuffers"));
}
GLWidget::~GLWidget()
{
pbuffer->releaseFromDynamicTexture();
glDeleteTextures(1, &dynamicTexture);
glDeleteLists(pbufferList, 1);
delete pbuffer;
}
void GLWidget::initializeGL()
{
glMatrixMode(GL_MODELVIEW);
glEnable(GL_CULL_FACE);
initCommon();
initPbuffer();
for (int i = 0; i < 3; ++i) {
yOffs[i] = 0.0f;
xInc[i] = 0.005f;
rot[i] = 0.0f;
}
xOffs[0]= 0.0f;
xOffs[1]= 0.5f;
xOffs[2]= 1.0f;
cubeTexture = bindTexture(QImage(":res/cubelogo.png"));
}
void GLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = w/(float)(h ? h : 1);
glFrustum(-aspect, aspect, -1, 1, 10, 100);
glTranslatef(-0.5f, -0.5f, -0.5f);
glTranslatef(0.0f, 0.0f, -15.0f);
}
void GLWidget::paintGL()
{
// draw a spinning cube into the pbuffer..
pbuffer->makeCurrent();
glBindTexture(GL_TEXTURE_2D, cubeTexture);
glCallList(pbufferList);
glFlush();
// rendering directly to a texture is not supported on X11 and
// some Windows implementations, unfortunately
if (!hasDynamicTextureUpdate)
pbuffer->updateDynamicTexture(dynamicTexture);
// ..and use the pbuffer contents as a texture when rendering the
// background and the bouncing cubes
makeCurrent();
glBindTexture(GL_TEXTURE_2D, dynamicTexture);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw the background
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glVertexPointer(2, GL_INT, 0, faceArray);
glTranslatef(-1.2f, -0.8f, 0.0f);
glScalef(0.2f, 0.2f, 0.2f);
for (int y = 0; y < 5; ++y) {
for (int x = 0; x < 5; ++x) {
glTranslatef(2.0f, 0, 0);
glColor4f(0.8f, 0.8f, 0.8f, 1.0f);
glDrawArrays(GL_QUADS, 0, 4);
}
glTranslatef(-10.0f, 2.0f, 0);
}
glVertexPointer(3, GL_INT, 0, cubeArray);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
// draw the bouncing cubes
drawCube(0, 0.0f, 1.5f, 2.5f, 1.5f);
drawCube(1, 1.0f, 2.0f, 2.5f, 2.0f);
drawCube(2, 2.0f, 3.5f, 2.5f, 2.5f);
}
void GLWidget::drawCube(int i, GLfloat z, GLfloat rotation, GLfloat jmp, GLfloat amp)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(xOffs[i], yOffs[i], z);
glTranslatef(0.5f, 0.5f, 0.5f);
GLfloat scale = 0.75 + i*(0.25f/2);
glScalef(scale, scale, scale);
glRotatef(rot[i], 1.0f, 1.0f, 1.0f);
glTranslatef(-0.5f, -0.5f, -0.5f);
glColor4f(1.0f, 1.0f, 1.0f, 0.8f);
glDrawArrays(GL_QUADS, 0, 24);
if (xOffs[i] > 1.0f || xOffs[i] < -1.0f) {
xInc[i] = -xInc[i];
xOffs[i] = xOffs[i] > 1.0f ? 1.0f : -1.0f;
}
xOffs[i] += xInc[i];
yOffs[i] = qAbs(cos((-3.141592f * jmp) * xOffs[i]) * amp) - 1;
rot[i] += rotation;
}
void GLWidget::initCommon()
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_INT, 0, cubeArray);
glTexCoordPointer(2, GL_INT, 0, cubeTextureArray);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colorArray);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
void GLWidget::initPbuffer()
{
// set up the pbuffer context
pbuffer->makeCurrent();
initCommon();
glViewport(0, 0, pbuffer->size().width(), pbuffer->size().height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -99, 99);
glTranslatef(-0.5f, -0.5f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
pbufferList = glGenLists(1);
glNewList(pbufferList, GL_COMPILE);
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
// draw cube background
glPushMatrix();
glLoadIdentity();
glTranslatef(0.5f, 0.5f, -2.0f);
glDisable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_INT, 0, faceArray);
glDrawArrays(GL_QUADS, 0, 4);
glVertexPointer(3, GL_INT, 0, cubeArray);
glDisableClientState(GL_COLOR_ARRAY);
glEnable(GL_TEXTURE_2D);
glPopMatrix();
// draw cube
glTranslatef(0.5f, 0.5f, 0.5f);
glRotatef(3.0f, 1.0f, 1.0f, 1.0f);
glTranslatef(-0.5f, -0.5f, -0.5f);
glColor4f(0.9f, 0.9f, 0.9f, 1.0f);
glDrawArrays(GL_QUADS, 0, 24);
}
glEndList();
// generate a texture that has the same size/format as the pbuffer
dynamicTexture = pbuffer->generateDynamicTexture();
// bind the dynamic texture to the pbuffer - this is a no-op under X11
hasDynamicTextureUpdate = pbuffer->bindToDynamicTexture(dynamicTexture);
makeCurrent();
}
|