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