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 <QtGui>
|
|
43 |
#include <QtOpenGL>
|
|
44 |
|
|
45 |
#include "glwidget.h"
|
|
46 |
|
|
47 |
GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget)
|
|
48 |
: QGLWidget(parent, shareWidget)
|
|
49 |
{
|
|
50 |
clearColor = Qt::black;
|
|
51 |
xRot = 0;
|
|
52 |
yRot = 0;
|
|
53 |
zRot = 0;
|
|
54 |
#ifdef QT_OPENGL_ES_2
|
|
55 |
program = 0;
|
|
56 |
#endif
|
|
57 |
}
|
|
58 |
|
|
59 |
GLWidget::~GLWidget()
|
|
60 |
{
|
|
61 |
}
|
|
62 |
|
|
63 |
QSize GLWidget::minimumSizeHint() const
|
|
64 |
{
|
|
65 |
return QSize(50, 50);
|
|
66 |
}
|
|
67 |
|
|
68 |
QSize GLWidget::sizeHint() const
|
|
69 |
{
|
|
70 |
return QSize(200, 200);
|
|
71 |
}
|
|
72 |
|
|
73 |
void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle)
|
|
74 |
{
|
|
75 |
xRot += xAngle;
|
|
76 |
yRot += yAngle;
|
|
77 |
zRot += zAngle;
|
|
78 |
updateGL();
|
|
79 |
}
|
|
80 |
|
|
81 |
void GLWidget::setClearColor(const QColor &color)
|
|
82 |
{
|
|
83 |
clearColor = color;
|
|
84 |
updateGL();
|
|
85 |
}
|
|
86 |
|
|
87 |
void GLWidget::initializeGL()
|
|
88 |
{
|
|
89 |
makeObject();
|
|
90 |
|
|
91 |
glEnable(GL_DEPTH_TEST);
|
|
92 |
glEnable(GL_CULL_FACE);
|
|
93 |
#ifndef QT_OPENGL_ES_2
|
|
94 |
glEnable(GL_TEXTURE_2D);
|
|
95 |
#endif
|
|
96 |
|
|
97 |
#ifdef QT_OPENGL_ES_2
|
|
98 |
|
|
99 |
#define PROGRAM_VERTEX_ATTRIBUTE 0
|
|
100 |
#define PROGRAM_TEXCOORD_ATTRIBUTE 1
|
|
101 |
|
|
102 |
QGLShader *vshader = new QGLShader(QGLShader::VertexShader, this);
|
|
103 |
const char *vsrc =
|
|
104 |
"attribute highp vec4 vertex;\n"
|
|
105 |
"attribute mediump vec4 texCoord;\n"
|
|
106 |
"varying mediump vec4 texc;\n"
|
|
107 |
"uniform mediump mat4 matrix;\n"
|
|
108 |
"void main(void)\n"
|
|
109 |
"{\n"
|
|
110 |
" gl_Position = matrix * vertex;\n"
|
|
111 |
" texc = texCoord;\n"
|
|
112 |
"}\n";
|
|
113 |
vshader->compile(vsrc);
|
|
114 |
|
|
115 |
QGLShader *fshader = new QGLShader(QGLShader::FragmentShader, this);
|
|
116 |
const char *fsrc =
|
|
117 |
"uniform sampler2D texture;\n"
|
|
118 |
"varying mediump vec4 texc;\n"
|
|
119 |
"void main(void)\n"
|
|
120 |
"{\n"
|
|
121 |
" gl_FragColor = texture2D(texture, texc.st);\n"
|
|
122 |
"}\n";
|
|
123 |
fshader->compile(fsrc);
|
|
124 |
|
|
125 |
program = new QGLShaderProgram(this);
|
|
126 |
program->addShader(vshader);
|
|
127 |
program->addShader(fshader);
|
|
128 |
program->bindAttributeLocation("vertex", PROGRAM_VERTEX_ATTRIBUTE);
|
|
129 |
program->bindAttributeLocation("texCoord", PROGRAM_TEXCOORD_ATTRIBUTE);
|
|
130 |
program->link();
|
|
131 |
|
|
132 |
program->enable();
|
|
133 |
program->setUniformValue("texture", 0);
|
|
134 |
|
|
135 |
#endif
|
|
136 |
}
|
|
137 |
|
|
138 |
void GLWidget::paintGL()
|
|
139 |
{
|
|
140 |
qglClearColor(clearColor);
|
|
141 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
142 |
|
|
143 |
#if !defined(QT_OPENGL_ES_2)
|
|
144 |
|
|
145 |
glLoadIdentity();
|
|
146 |
glTranslatef(0.0f, 0.0f, -10.0f);
|
|
147 |
glRotatef(xRot / 16.0f, 1.0f, 0.0f, 0.0f);
|
|
148 |
glRotatef(yRot / 16.0f, 0.0f, 1.0f, 0.0f);
|
|
149 |
glRotatef(zRot / 16.0f, 0.0f, 0.0f, 1.0f);
|
|
150 |
|
|
151 |
glVertexPointer(3, GL_FLOAT, 0, vertices.constData());
|
|
152 |
glTexCoordPointer(2, GL_FLOAT, 0, texCoords.constData());
|
|
153 |
glEnableClientState(GL_VERTEX_ARRAY);
|
|
154 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
155 |
|
|
156 |
#else
|
|
157 |
|
|
158 |
QMatrix4x4 m;
|
|
159 |
m.ortho(-0.5f, +0.5f, +0.5f, -0.5f, 4.0f, 15.0f);
|
|
160 |
m.translate(0.0f, 0.0f, -10.0f);
|
|
161 |
m.rotate(xRot / 16.0f, 1.0f, 0.0f, 0.0f);
|
|
162 |
m.rotate(yRot / 16.0f, 0.0f, 1.0f, 0.0f);
|
|
163 |
m.rotate(zRot / 16.0f, 0.0f, 0.0f, 1.0f);
|
|
164 |
|
|
165 |
program->setUniformValue("matrix", m);
|
|
166 |
program->setAttributeArray
|
|
167 |
(PROGRAM_VERTEX_ATTRIBUTE, vertices.constData());
|
|
168 |
program->setAttributeArray
|
|
169 |
(PROGRAM_TEXCOORD_ATTRIBUTE, texCoords.constData());
|
|
170 |
|
|
171 |
#endif
|
|
172 |
|
|
173 |
for (int i = 0; i < 6; ++i) {
|
|
174 |
glBindTexture(GL_TEXTURE_2D, textures[i]);
|
|
175 |
glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4);
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
void GLWidget::resizeGL(int width, int height)
|
|
180 |
{
|
|
181 |
int side = qMin(width, height);
|
|
182 |
glViewport((width - side) / 2, (height - side) / 2, side, side);
|
|
183 |
|
|
184 |
#if !defined(QT_OPENGL_ES_2)
|
|
185 |
glMatrixMode(GL_PROJECTION);
|
|
186 |
glLoadIdentity();
|
|
187 |
#ifndef QT_OPENGL_ES
|
|
188 |
glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
|
|
189 |
#else
|
|
190 |
glOrthof(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
|
|
191 |
#endif
|
|
192 |
glMatrixMode(GL_MODELVIEW);
|
|
193 |
#endif
|
|
194 |
}
|
|
195 |
|
|
196 |
void GLWidget::mousePressEvent(QMouseEvent *event)
|
|
197 |
{
|
|
198 |
lastPos = event->pos();
|
|
199 |
}
|
|
200 |
|
|
201 |
void GLWidget::mouseMoveEvent(QMouseEvent *event)
|
|
202 |
{
|
|
203 |
int dx = event->x() - lastPos.x();
|
|
204 |
int dy = event->y() - lastPos.y();
|
|
205 |
|
|
206 |
if (event->buttons() & Qt::LeftButton) {
|
|
207 |
rotateBy(8 * dy, 8 * dx, 0);
|
|
208 |
} else if (event->buttons() & Qt::RightButton) {
|
|
209 |
rotateBy(8 * dy, 0, 8 * dx);
|
|
210 |
}
|
|
211 |
lastPos = event->pos();
|
|
212 |
}
|
|
213 |
|
|
214 |
void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */)
|
|
215 |
{
|
|
216 |
emit clicked();
|
|
217 |
}
|
|
218 |
|
|
219 |
void GLWidget::makeObject()
|
|
220 |
{
|
|
221 |
static const int coords[6][4][3] = {
|
|
222 |
{ { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
|
|
223 |
{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
|
|
224 |
{ { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
|
|
225 |
{ { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
|
|
226 |
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
|
|
227 |
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
|
|
228 |
};
|
|
229 |
|
|
230 |
for (int j=0; j < 6; ++j) {
|
|
231 |
textures[j] = bindTexture
|
|
232 |
(QPixmap(QString(":/images/side%1.png").arg(j + 1)), GL_TEXTURE_2D);
|
|
233 |
}
|
|
234 |
|
|
235 |
for (int i = 0; i < 6; ++i) {
|
|
236 |
for (int j = 0; j < 4; ++j) {
|
|
237 |
texCoords.append
|
|
238 |
(QVector2D(j == 0 || j == 3, j == 0 || j == 1));
|
|
239 |
vertices.append
|
|
240 |
(QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1],
|
|
241 |
0.2 * coords[i][j][2]));
|
|
242 |
}
|
|
243 |
}
|
|
244 |
}
|