1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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::Vertex, 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->compileSourceCode(vsrc);
|
---|
114 |
|
---|
115 | QGLShader *fshader = new QGLShader(QGLShader::Fragment, 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->compileSourceCode(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->bind();
|
---|
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->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
|
---|
167 | program->enableAttributeArray(PROGRAM_TEXCOORD_ATTRIBUTE);
|
---|
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 | }
|
---|