1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 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:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
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 |
|
---|
101 | QGLShader *vshader = new QGLShader(QGLShader::Vertex, this);
|
---|
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";
|
---|
112 | vshader->compileSourceCode(vsrc);
|
---|
113 |
|
---|
114 | QGLShader *fshader = new QGLShader(QGLShader::Fragment, this);
|
---|
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";
|
---|
122 | fshader->compileSourceCode(fsrc);
|
---|
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 |
|
---|
131 | program->bind();
|
---|
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);
|
---|
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());
|
---|
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 | }
|
---|