author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Mon, 19 Apr 2010 10:15:19 +0300 | |
branch | RCL_3 |
changeset 9 | b5b118452c7d |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
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 |
** |
|
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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
102 |
QGLShader *vshader = new QGLShader(QGLShader::Vertex, this); |
0 | 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"; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
vshader->compileSourceCode(vsrc); |
0 | 114 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
115 |
QGLShader *fshader = new QGLShader(QGLShader::Fragment, this); |
0 | 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"; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
123 |
fshader->compileSourceCode(fsrc); |
0 | 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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
program->bind(); |
0 | 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); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE); |
0 | 168 |
program->setAttributeArray |
169 |
(PROGRAM_VERTEX_ATTRIBUTE, vertices.constData()); |
|
170 |
program->setAttributeArray |
|
171 |
(PROGRAM_TEXCOORD_ATTRIBUTE, texCoords.constData()); |
|
172 |
||
173 |
#endif |
|
174 |
||
175 |
for (int i = 0; i < 6; ++i) { |
|
176 |
glBindTexture(GL_TEXTURE_2D, textures[i]); |
|
177 |
glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
void GLWidget::resizeGL(int width, int height) |
|
182 |
{ |
|
183 |
int side = qMin(width, height); |
|
184 |
glViewport((width - side) / 2, (height - side) / 2, side, side); |
|
185 |
||
186 |
#if !defined(QT_OPENGL_ES_2) |
|
187 |
glMatrixMode(GL_PROJECTION); |
|
188 |
glLoadIdentity(); |
|
189 |
#ifndef QT_OPENGL_ES |
|
190 |
glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); |
|
191 |
#else |
|
192 |
glOrthof(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0); |
|
193 |
#endif |
|
194 |
glMatrixMode(GL_MODELVIEW); |
|
195 |
#endif |
|
196 |
} |
|
197 |
||
198 |
void GLWidget::mousePressEvent(QMouseEvent *event) |
|
199 |
{ |
|
200 |
lastPos = event->pos(); |
|
201 |
} |
|
202 |
||
203 |
void GLWidget::mouseMoveEvent(QMouseEvent *event) |
|
204 |
{ |
|
205 |
int dx = event->x() - lastPos.x(); |
|
206 |
int dy = event->y() - lastPos.y(); |
|
207 |
||
208 |
if (event->buttons() & Qt::LeftButton) { |
|
209 |
rotateBy(8 * dy, 8 * dx, 0); |
|
210 |
} else if (event->buttons() & Qt::RightButton) { |
|
211 |
rotateBy(8 * dy, 0, 8 * dx); |
|
212 |
} |
|
213 |
lastPos = event->pos(); |
|
214 |
} |
|
215 |
||
216 |
void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */) |
|
217 |
{ |
|
218 |
emit clicked(); |
|
219 |
} |
|
220 |
||
221 |
void GLWidget::makeObject() |
|
222 |
{ |
|
223 |
static const int coords[6][4][3] = { |
|
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 |
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } }, |
|
229 |
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } } |
|
230 |
}; |
|
231 |
||
232 |
for (int j=0; j < 6; ++j) { |
|
233 |
textures[j] = bindTexture |
|
234 |
(QPixmap(QString(":/images/side%1.png").arg(j + 1)), GL_TEXTURE_2D); |
|
235 |
} |
|
236 |
||
237 |
for (int i = 0; i < 6; ++i) { |
|
238 |
for (int j = 0; j < 4; ++j) { |
|
239 |
texCoords.append |
|
240 |
(QVector2D(j == 0 || j == 3, j == 0 || j == 1)); |
|
241 |
vertices.append |
|
242 |
(QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], |
|
243 |
0.2 * coords[i][j][2])); |
|
244 |
} |
|
245 |
} |
|
246 |
} |