1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the examples of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:BSD$
9
** You may use this file under the terms of the BSD license as follows:
11
** "Redistribution and use in source and binary forms, with or without
12
** modification, are permitted provided that the following conditions are
14
** * Redistributions of source code must retain the above copyright
15
** notice, this list of conditions and the following disclaimer.
16
** * Redistributions in binary form must reproduce the above copyright
17
** notice, this list of conditions and the following disclaimer in
18
** the documentation and/or other materials provided with the
20
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21
** of its contributors may be used to endorse or promote products derived
22
** from this software without specific prior written permission.
25
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39
****************************************************************************/
46
GLWidget::GLWidget(QWidget *parent, QGLWidget *shareWidget)
47
: QGLWidget(parent, shareWidget)
49
clearColor = Qt::black;
62
QSize GLWidget::minimumSizeHint() const
67
QSize GLWidget::sizeHint() const
69
return QSize(200, 200);
72
void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle)
80
void GLWidget::setClearColor(const QColor &color)
86
void GLWidget::initializeGL()
90
glEnable(GL_DEPTH_TEST);
91
glEnable(GL_CULL_FACE);
92
#ifndef QT_OPENGL_ES_2
93
glEnable(GL_TEXTURE_2D);
98
#define PROGRAM_VERTEX_ATTRIBUTE 0
99
#define PROGRAM_TEXCOORD_ATTRIBUTE 1
101
QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
103
"attribute highp vec4 vertex;\n"
104
"attribute mediump vec4 texCoord;\n"
105
"varying mediump vec4 texc;\n"
106
"uniform mediump mat4 matrix;\n"
109
" gl_Position = matrix * vertex;\n"
110
" texc = texCoord;\n"
112
vshader->compileSourceCode(vsrc);
114
QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
116
"uniform sampler2D texture;\n"
117
"varying mediump vec4 texc;\n"
120
" gl_FragColor = texture2D(texture, texc.st);\n"
122
fshader->compileSourceCode(fsrc);
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);
132
program->setUniformValue("texture", 0);
137
void GLWidget::paintGL()
139
qglClearColor(clearColor);
140
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
142
#if !defined(QT_OPENGL_ES_2)
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);
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);
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);
164
program->setUniformValue("matrix", m);
165
program->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
166
program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
167
program->setAttributeArray
168
(PROGRAM_VERTEX_ATTRIBUTE, vertices.constData());
169
program->setAttributeArray
170
(PROGRAM_TEXCOORD_ATTRIBUTE, texCoords.constData());
174
for (int i = 0; i < 6; ++i) {
175
glBindTexture(GL_TEXTURE_2D, textures[i]);
176
glDrawArrays(GL_TRIANGLE_FAN, i * 4, 4);
180
void GLWidget::resizeGL(int width, int height)
182
int side = qMin(width, height);
183
glViewport((width - side) / 2, (height - side) / 2, side, side);
185
#if !defined(QT_OPENGL_ES_2)
186
glMatrixMode(GL_PROJECTION);
189
glOrtho(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
191
glOrthof(-0.5, +0.5, +0.5, -0.5, 4.0, 15.0);
193
glMatrixMode(GL_MODELVIEW);
197
void GLWidget::mousePressEvent(QMouseEvent *event)
199
lastPos = event->pos();
202
void GLWidget::mouseMoveEvent(QMouseEvent *event)
204
int dx = event->x() - lastPos.x();
205
int dy = event->y() - lastPos.y();
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);
212
lastPos = event->pos();
215
void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */)
220
void GLWidget::makeObject()
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 } }
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);
236
for (int i = 0; i < 6; ++i) {
237
for (int j = 0; j < 4; ++j) {
239
(QVector2D(j == 0 || j == 3, j == 0 || j == 1));
241
(QVector3D(0.2 * coords[i][j][0], 0.2 * coords[i][j][1],
242
0.2 * coords[i][j][2]));