-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
360 lines (299 loc) · 12.9 KB
/
widget.cpp
File metadata and controls
360 lines (299 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "widget.h"
#include <QGLShader>
#include <QGLShaderProgram>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QMatrix4x4>
#include <qmath.h>
#ifdef QT_OPENGL_ES_2
#include <GLES2/gl2extimg.h>
#endif
#if !defined(GL_BGRA) && defined(GL_BGRA_EXT)
#define GL_BGRA_EXT GL_BGRA
#endif
#ifdef USE_FILE_SHADERS
#define CUBE_VERTEX_SHADER_PATH "z-bounce.vsh"
#define CUBE_FRAGMENT_SHADER_PATH "basic-texture.fsh"
#define ENV_VERTEX_SHADER_PATH "simple-camera-projection.vsh"
#define ENV_FRAGMENT_SHADER_PATH "cubemap-texture.fsh"
#else
#define CUBE_VERTEX_SHADER_PATH ":/shaders/vertex/z-bounce"
#define CUBE_FRAGMENT_SHADER_PATH ":/shaders/fragment/basic-texture"
#define ENV_VERTEX_SHADER_PATH ":/shaders/vertex/simple-camera-projection"
#define ENV_FRAGMENT_SHADER_PATH ":/shaders/fragment/cubemap-texture"
#endif
// Windows lacks the cubemap definitions in its headers, but the drivers handle those fine
#ifndef GL_TEXTURE_CUBE_MAP
#define GL_TEXTURE_CUBE_MAP 0x8513
#define GL_TEXTURE_BINDING_CUBE_MAP 0x8514
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
#endif
namespace {
const int numCubeFaces = 6;
const int numCubeFaceVertices = 6;
const int numCubemapFaceVertices = 4;
const int cubeSideLength = 50;
const qreal xPanningMultiplier = 0.3;
const qreal yPanningMultiplier = 0.3;
qreal deg2rad( qreal deg ) {
qreal adjustedDeg = deg;
if ( qAbs(deg) >= 360.0 ) {
adjustedDeg += 360.0 * (deg > 0.0 ? -1: 1);
}
return adjustedDeg * M_PI / 180;
}
}
GLSLTestWidget::GLSLTestWidget( const QGLFormat& glFormat, QWidget *parent)
: QGLWidget(glFormat, parent),
m_cubeShaderProgram(0),
m_envShaderProgram(0),
m_envVertices(),
m_cubemapTexture(0),
m_cubeVertices(),
m_cubeTexCoords(),
m_mallowTextures(),
m_bounceRatio(1.0),
m_hRotation(0.0),
m_vRotation(0.0),
m_zOffset(-4.0),
m_lastMousePosition(),
m_secondLastMousePosition(),
m_kineticAnimation(0),
m_spinMallow(false),
m_mallowRotationMatrix()
{
QPropertyAnimation* pressAnimation = new QPropertyAnimation(this, "bounceRatio", this);
pressAnimation->setEndValue( (GLfloat) 0.5);
pressAnimation->setDuration(250);
pressAnimation->setEasingCurve(QEasingCurve::OutExpo);
connect(this, SIGNAL(pressed(QPoint)), pressAnimation, SLOT(start()));
connect(pressAnimation, SIGNAL(valueChanged(QVariant)), this, SLOT(updateGL()));
QPropertyAnimation* releaseAnimation = new QPropertyAnimation(this, "bounceRatio", this);
releaseAnimation->setEndValue( (GLfloat) 1.0);
releaseAnimation->setDuration(2000);
releaseAnimation->setEasingCurve(QEasingCurve::OutElastic);
connect(this, SIGNAL(released()), releaseAnimation, SLOT(start()));
connect(releaseAnimation, SIGNAL(valueChanged(QVariant)), this, SLOT(updateGL()));
m_kineticAnimation = new QPropertyAnimation(this, "", this);
m_kineticAnimation->setEndValue( QPointF(0,0) );
m_kineticAnimation->setDuration(5000);
m_kineticAnimation->setEasingCurve(QEasingCurve::OutExpo);
connect(m_kineticAnimation, SIGNAL(valueChanged(QVariant)), this, SLOT(updateKineticScrolling(QVariant)));
}
GLSLTestWidget::~GLSLTestWidget()
{
Q_FOREACH( GLuint tex, m_mallowTextures ) {
deleteTexture(tex);
}
}
void GLSLTestWidget::initializeGL()
{
qglClearColor(Qt::gray);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
initEnvironmentData();
initCubeData();
const bool useGLSL = QGLShader::hasOpenGLShaders(QGLShader::Fragment) &&
QGLShader::hasOpenGLShaders(QGLShader::Vertex) &&
QGLShaderProgram::hasOpenGLShaderPrograms();
qDebug() << "Support for OpenGL Shaders:" << (useGLSL ? "yes" : "no");
if (!useGLSL)
return;
m_cubeShaderProgram = new QGLShaderProgram(this);
bool ok = false;
ok = m_cubeShaderProgram->addShaderFromSourceFile(QGLShader::Vertex, CUBE_VERTEX_SHADER_PATH);
if (!ok) {
qDebug() << "Vertex shader compiling failed:" << m_cubeShaderProgram->log();
return;
}
ok = m_cubeShaderProgram->addShaderFromSourceFile(QGLShader::Fragment, CUBE_FRAGMENT_SHADER_PATH);
if (!ok) {
qDebug() << "Fragment shader compiling failed:" << m_cubeShaderProgram->log();
return;
}
ok = m_cubeShaderProgram->link();
if (!ok) {
qDebug() << "Shader program linking failed:" << m_cubeShaderProgram->log();
return;
}
m_envShaderProgram = new QGLShaderProgram(this);
ok = m_envShaderProgram->addShaderFromSourceFile(QGLShader::Vertex, ENV_VERTEX_SHADER_PATH);
if (!ok) {
qDebug() << "Vertex shader compiling failed:" << m_envShaderProgram->log();
return;
}
ok = m_envShaderProgram->addShaderFromSourceFile(QGLShader::Fragment, ENV_FRAGMENT_SHADER_PATH);
if (!ok) {
qDebug() << "Fragment shader compiling failed:" << m_envShaderProgram->log();
return;
}
ok = m_envShaderProgram->link();
if (!ok) {
qDebug() << "Shader program linking failed:" << m_envShaderProgram->log();
return;
}
}
void GLSLTestWidget::paintGL()
{
const QVector3D cameraPos( qSin( deg2rad(m_hRotation) ) * qCos( deg2rad(m_vRotation) ) * m_zOffset,
qSin( deg2rad(m_vRotation) ) * m_zOffset,
qCos( deg2rad(m_hRotation) ) * qCos( deg2rad(m_vRotation) ) * m_zOffset) ;
QMatrix4x4 cameraMatrix;
cameraMatrix.lookAt( cameraPos, QVector3D(), QVector3D(0, 1, 0) );
#ifdef DEBUG_CAMERA
qDebug() << "Camera position:" << cameraPos << "x-rotation=" << m_hRotation << "y-rotation=" << m_vRotation;
#endif
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_envShaderProgram->bind();
m_envShaderProgram->enableAttributeArray("aVertex");
m_envShaderProgram->setAttributeArray("aVertex", m_envVertices.constData());
QMatrix4x4 projectionMatrix;
projectionMatrix.frustum(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1000.0f);
m_envShaderProgram->setUniformValue("projectionMatrix", projectionMatrix);
m_envShaderProgram->setUniformValue("cameraMatrix", cameraMatrix);
glBindTexture( GL_TEXTURE_CUBE_MAP, m_cubemapTexture );
for (int face = 0; face < numCubeFaces; ++face) {
glDrawArrays(GL_TRIANGLE_FAN, face * numCubemapFaceVertices, numCubemapFaceVertices);
}
m_cubeShaderProgram->bind();
m_cubeShaderProgram->enableAttributeArray("aVertex");
m_cubeShaderProgram->setAttributeArray("aVertex", m_cubeVertices.constData());
m_cubeShaderProgram->enableAttributeArray("aTexCoord");
m_cubeShaderProgram->setAttributeArray("aTexCoord", m_cubeTexCoords.constData());
m_cubeShaderProgram->setAttributeValue("vBounceRatio", (GLfloat) bounceRatio());
m_cubeShaderProgram->setUniformValue("projectionMatrix", projectionMatrix);
m_cubeShaderProgram->setUniformValue("cameraMatrix", cameraMatrix);
m_cubeShaderProgram->setUniformValue("rotationMatrix", m_mallowRotationMatrix);
for (int face = 0; face < numCubeFaces; ++face) {
glBindTexture( GL_TEXTURE_2D, m_mallowTextures.at( face % m_mallowTextures.size() ) );
glDrawArrays(GL_TRIANGLE_FAN, face * numCubeFaceVertices, numCubeFaceVertices);
}
}
void GLSLTestWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void GLSLTestWidget::initCubeData()
{
if ( !m_cubeVertices.empty() || !m_cubeTexCoords.empty() ) {
// already initialized
return;
}
static const int coords[numCubeFaces][numCubeFaceVertices][3] = {
{ { 0, 0, -1 }, { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 }, { +1, -1, -1 } },
{ { 0, +1, 0 }, { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 }, { +1, +1, -1 } },
{ { +1, 0, 0 }, { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 }, { +1, -1, +1 } },
{ { -1, 0, 0 }, { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 }, { -1, -1, -1 } },
{ { 0, -1, 0 }, { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 }, { +1, -1, +1 } },
{ { 0, 0, +1 }, { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 }, { -1, -1, +1 } }
};
for (int i = 0; i < numCubeFaces; ++i) {
for (int j = 0; j < numCubeFaceVertices; ++j) {
m_cubeVertices << QVector3D(coords[i][j][0], coords[i][j][1], coords[i][j][2]);
if ( !j ) {
m_cubeTexCoords << QVector2D(0.5, 0.5);
} else {
// (1,0) (0,0) (0,1) (1,1) (1,0)
m_cubeTexCoords << QVector2D((j == 1 || j == 4 || j == 5) ? 0.0 : 1.0, (j == 3 || j == 4) ? 0.0 : 1.0);
}
}
}
m_mallowTextures << bindTexture( QImage( ":/images/mallow-happy" ), GL_TEXTURE_2D, GL_BGRA, QGLContext::LinearFilteringBindOption );
}
void GLSLTestWidget::initEnvironmentData()
{
if ( !m_envVertices.empty() ) {
// already initialized
return;
}
static const int coords[numCubeFaces][numCubemapFaceVertices][3] = {
{ { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
{ { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
{ { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
};
static const int mapFaces[numCubeFaces] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y
};
glGenTextures(1, &m_cubemapTexture);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_cubemapTexture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
for (int i = 0; i < numCubeFaces; ++i) {
for (int j = 0; j < numCubemapFaceVertices; ++j) {
m_envVertices << QVector3D(coords[i][j][0], coords[i][j][1], coords[i][j][2]) * cubeSideLength;
}
const QImage faceImage( QString(":/cubemaps/mountain/%1").arg(i+1) );
glTexImage2D( mapFaces[i], 0, GL_BGRA, faceImage.width(), faceImage.height(), 0, GL_BGRA, GL_UNSIGNED_BYTE, faceImage.bits() );
}
}
void GLSLTestWidget::mousePressEvent(QMouseEvent *e)
{
m_secondLastMousePosition = QPoint();
m_lastMousePosition = e->pos();
m_kineticAnimation->stop();
// TODO: add real hit detection
const QRect mallowRect( rect().topLeft() + rect().center() * 0.5, rect().bottomRight() - rect().center() * 0.5);
m_spinMallow = mallowRect.contains(e->pos());
if ( m_spinMallow ) {
emit pressed(e->pos());
}
}
void GLSLTestWidget::mouseReleaseEvent(QMouseEvent *e)
{
if ( !m_secondLastMousePosition.isNull() ) {
const QPointF delta = m_lastMousePosition - m_secondLastMousePosition;
if ( delta.manhattanLength() > 5.0 ) {
m_kineticAnimation->setStartValue( QPointF(delta.x() * xPanningMultiplier, delta.y() * yPanningMultiplier) );
m_kineticAnimation->start();
}
}
if ( m_spinMallow ) {
emit released();
}
}
void GLSLTestWidget::mouseMoveEvent(QMouseEvent *e)
{
QPoint delta = e->pos() - m_lastMousePosition;
const QPoint prevDelta = m_lastMousePosition - m_secondLastMousePosition;
if ( delta.manhattanLength() > prevDelta.manhattanLength() * 2 )
delta = prevDelta;
if ( m_spinMallow ) {
m_mallowRotationMatrix.rotate( delta.manhattanLength(), -delta.y(), -delta.x());
} else {
m_hRotation += delta.x() * xPanningMultiplier;
m_vRotation = qBound(-89.0f, m_vRotation + delta.y() * yPanningMultiplier, 89.0f);
}
m_secondLastMousePosition = m_lastMousePosition;
m_lastMousePosition = e->pos();
updateGL();
}
void GLSLTestWidget::wheelEvent(QWheelEvent *e)
{
m_zOffset = qBound(cubeSideLength * -1.0f, m_zOffset + (( e->delta() > 0 ) ? 0.1f : -0.1f), -3.0f);
updateGL();
}
void GLSLTestWidget::updateKineticScrolling(const QVariant &value)
{
if ( m_spinMallow ) {
m_mallowRotationMatrix.rotate( value.toPointF().manhattanLength(), -value.toPointF().y(), -value.toPointF().x());
} else {
m_hRotation += value.toPointF().x();
m_vRotation = qBound(-89.0f, m_vRotation + value.toPointF().y(), 89.0f);
updateGL();
}
}