-
Notifications
You must be signed in to change notification settings - Fork 7
/
cglwidget.cpp
518 lines (392 loc) · 13.3 KB
/
cglwidget.cpp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include "cglwidget.h"
#include <QDebug>
#include <QtOpenGL>
#include <qmath.h>
#include <locale.h>
#include <QString>
#include <QVectorIterator>
#include <QTimer>
#include "utils.h"
cGLWidget::cGLWidget(QWidget *parent) :
QOpenGLWidget(parent), mEye(QVector3D(0,0,700)), mView(QVector3D(0,0,0)), mUp(QVector3D(0,1,0))
{
setMouseTracking(true);
mTimer = new QTimer(this);
mTimer->setObjectName("mTimer");
connect(mTimer, SIGNAL(timeout()), this, SLOT(on_mTimer_timeout()));
mTimer->start(10);
}
float cGLWidget::scale(float from_min, float from_max, float to_min, float to_max, float val)
{
return (to_max - to_min)*(val - from_min) / (from_max - from_min) + to_min;
}
bool cGLWidget::worldToScreen(const QVector3D &pw, int& x, int& y)
{
QMatrix4x4 viewMatrix;
viewMatrix.lookAt(mEye, mView, mUp);
QVector4D clipSpacePos = mProjectionMatrix * (viewMatrix * QVector4D(pw, 1.0));
QVector3D ndcSpacePos = clipSpacePos.toVector3D() / clipSpacePos.w();
// Scale NDC to Screen Space
x = qRound((ndcSpacePos.x() + 1.0) * 0.5 * width());
y = qRound((-ndcSpacePos.y() + 1.0) * 0.5 * height());
return true;
}
bool cGLWidget::screenToWorld(int winx, int winy, int z_ndc, QVector3D &pw)
{
// define the plane
sPlane plane(QVector3D(0,0,1), QVector3D(0,0,0));
QMatrix4x4 viewMatrix;
QVector4D ray_clip((2.0f * winx) / width() - 1.0f, 1.0f - (2.0f * winy) / height(), z_ndc, 1.0);
QVector4D ray_eye = mProjectionMatrix.inverted() * ray_clip;
ray_eye.setZ(-1);
ray_eye.setW(0);
viewMatrix.lookAt(mEye, mView, mUp);
QVector3D rw = QVector4D(viewMatrix.inverted() * ray_eye).toVector3D();
float c, d = QVector3D::dotProduct(plane.n, plane.p);
QVector3D rayDir = (mView - mEye).normalized();
if(mPerspectiveProjection) {
c = QVector3D::dotProduct(plane.n, mEye) - d;
} else {
rw += mEye;
c = QVector3D::dotProduct(plane.n, rw) - d;
}
if(c < 0) { return false; } // we are behind the plane
float a;
if(mPerspectiveProjection) {
a = QVector3D::dotProduct(plane.n, rw);
} else {
a = QVector3D::dotProduct(plane.n, rayDir);
}
if(a==0) { return false; } // ray is parallel to the plane
float t = c / a;
if(t==0) return false;
if(mPerspectiveProjection) {
pw = mEye - rw * t;
} else {
pw = rw - rayDir * t;
}
return true;
}
void cGLWidget::initializeGL()
{
initializeOpenGLFunctions();
//glClearColor(0.2f, 0.6f, 0.2f, 1.f); // green
glClearColor(0.9608f, 0.9608f, 0.8627f, 1.f); // beige
qDebug() << "Supported shading language version" << QString((char*)glGetString(GL_VENDOR))<< QString((char*)glGetString(GL_RENDERER)) << QString((char*)glGetString(GL_VERSION)) /*<< QString((char*)glGetString(GL_EXTENSIONS))*/ << QString((char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
qDebug() << "Initializing shaders...";
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
//glEnable(GL_CULL_FACE);
//glEnable(GL_MULTISAMPLE); // qt surface format sets the number of sampling
initShaders();
glClearDepth(1.0); // Depth Buffer Setup
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
mVAO.create();
QOpenGLVertexArrayObject::Binder vaoBinder(&mVAO);
emit(glInitialized());
}
void cGLWidget::initShaders()
{
// Overriding system locale until shaders are compiled
setlocale(LC_NUMERIC, "C");
if(!mShader.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vs.glsl"))
{
qDebug() << "error compiling vertex shader";
close();
}
if (!mShader.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fs.glsl"))
{
qDebug() << "error compiling fragment shader";
close();
}
// Linking shader pipeline
if (!mShader.link())
close();
// Binding shader pipeline for use
if (!mShader.bind())
close();
mShader.release();
// Restore system locale
setlocale(LC_ALL, "");
}
void cGLWidget::clearVBOs()
{
mVBOs.clear();
}
void cGLWidget::destroyVBO(const QString &name)
{
if(mNamedVBOs.contains(name))
mNamedVBOs[name].vbo.destroy();
mNamedVBOs.remove(name);
}
void cGLWidget::addToVBO(const QString& name, const std::vector<GLfloat> &buffer, int mode, QVector4D lineColor)
{
if(!buffer.size()) return;
if(mNamedVBOs.contains(name))
mNamedVBOs[name].vbo.destroy();
sBufferWithColor b;
b.mode = mode;
b.lineColor = lineColor;
b.vbo.create();
b.vbo.bind();
b.vbo.allocate(buffer.data(), buffer.size() * sizeof(GLfloat));
b.vbo.release();
if(mNamedVBOs.contains(name))
mNamedVBOs[name] = b;
else
mNamedVBOs.insert(name, b);
}
void cGLWidget::addToVBO(const std::vector<GLfloat> &buffer, int mode, QVector4D lineColor)
{
if(!buffer.size()) return;
sBufferWithColor b;
b.mode = mode;
b.lineColor = lineColor;
b.vbo.create();
b.vbo.bind();
b.vbo.allocate(buffer.data(), buffer.size() * sizeof(GLfloat));
b.vbo.release();
mVBOs.push_back(b);
}
void cGLWidget::topView()
{
mEye = QVector3D(0,450,500);
mView = QVector3D(0,0,0);
mUp = QVector3D(0,1,0);
update();
}
void cGLWidget::leftView()
{
mEye = QVector3D(-700,0,350);
mView = QVector3D(0,0,0);
mUp = QVector3D(0,1,0);
update();
}
void cGLWidget::rightView()
{
mEye = QVector3D(700,0,350);
mView = QVector3D(0,0,0);
mUp = QVector3D(0,1,0);
update();
}
void cGLWidget::frontView()
{
mEye = QVector3D(0,0,700);
mView = QVector3D(0,0,0);
mUp = QVector3D(0,1,0);
update();
}
void cGLWidget::resizeGL(int w, int h)
{
Q_UNUSED(w)
Q_UNUSED(h)
updateProjection(mPerspectiveProjection);
}
void cGLWidget::updateProjection(bool perspectiveProjection)
{
float w=width(), h=height();
float aspect = w / h;
float os = 512.;
mProjectionMatrix.setToIdentity();
if(perspectiveProjection) {
mProjectionMatrix.perspective(45.0f, aspect, 0.1f, 2000.0f);
} else {
if(w <= h) {
mProjectionMatrix.ortho(-os, os, -os/aspect, os/aspect, 1.0f, 1000.0f);
} else {
mProjectionMatrix.ortho(-os*aspect, os*aspect, -os, os, 1.0f, 1000.0f);
}
mOrthoProjectionMatrix = mProjectionMatrix;
}
mPerspectiveProjection = perspectiveProjection;
}
void cGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
QOpenGLContext *ctx = QOpenGLContext::currentContext();
QOpenGLVertexArrayObject::Binder vaoBinder(&mVAO);
QOpenGLFunctions *f=nullptr;
QMatrix4x4 viewMatrix, modelMatrix, lightViewMatrix;
modelMatrix.setToIdentity();
viewMatrix.lookAt(mEye, mView, mUp);
for(auto& b : mVBOs) {
b.vbo.bind();
mShader.bind();
mShader.setUniformValue("qt_lineColor", b.lineColor);
mShader.setUniformValue("qt_modelViewMatrix", viewMatrix);
mShader.setUniformValue("qt_projectionMatrix", mProjectionMatrix);
f = ctx->functions();
f->glEnableVertexAttribArray(0);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
glDrawArrays(b.mode, 0, b.vbo.size() / 3 / sizeof(GLfloat));
mShader.release();
b.vbo.release();
}
// draw named vbos
for(auto k: mNamedVBOs.keys())
{
sBufferWithColor& b = mNamedVBOs[k];
b.vbo.bind();
mShader.bind();
mShader.setUniformValue("qt_lineColor", b.lineColor);
mShader.setUniformValue("qt_modelViewMatrix", viewMatrix);
mShader.setUniformValue("qt_projectionMatrix", mProjectionMatrix);
f = ctx->functions();
f->glEnableVertexAttribArray(0);
f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
glDrawArrays(b.mode, 0, b.vbo.size() / 3 / sizeof(GLfloat));
mShader.release();
b.vbo.release();
}
}
float cGLWidget::linearizeDepth(float depth)
{
float zNear = 10; // TODO: Replace by the zNear of your perspective projection
float zFar = 100.0; // TODO: Replace by the zFar of your perspective projection
return (2.0f * zNear) / (zFar + zNear - depth * (zFar - zNear));
}
void cGLWidget::keyPressEvent(QKeyEvent *ke)
{
mKeyDown[ke->key()] = true;
}
void cGLWidget::keyReleaseEvent(QKeyEvent *ke)
{
mKeyDown[ke->key()] = false;
}
void cGLWidget::on_mTimer_timeout()
{
float speed = 3;
QVector3D eyeDir = (mView-mEye).normalized();
if(mKeyDown[Qt::Key_W])
{
mEye += eyeDir * speed;
mView += eyeDir * speed;
qDebug() << "eye" << mEye << "view" << mView << "up" << mUp;
}
if(mKeyDown[Qt::Key_S])
{
mEye += -eyeDir * speed;
mView += -eyeDir * speed;
qDebug() << "eye" << mEye << "view" << mView << "up" << mUp;
}
if(mKeyDown[Qt::Key_A])
{
QVector3D xaxis = QVector3D::crossProduct(mUp, eyeDir).normalized();
mEye += xaxis * speed;
mView += xaxis * speed;
qDebug() << "eye" << mEye << "view" << mView << "up" << mUp;
}
if(mKeyDown[Qt::Key_D])
{
QVector3D xaxis = QVector3D::crossProduct(mUp, eyeDir).normalized();
mEye += -xaxis * speed;
mView += -xaxis * speed;
qDebug() << "eye" << mEye << "view" << mView << "up" << mUp;
}
if(mKeyDown[Qt::Key_Q])
{
mEye += mUp * speed;
mView += mUp * speed;
}
if(mKeyDown[Qt::Key_E])
{
mEye -= mUp * speed;
mView -= mUp * speed;
}
update();
}
void cGLWidget::mouseMoveEvent(QMouseEvent *me)
{
QVector3D pw;
if(screenToWorld(me->x(), me->y(), -1.0, pw)) {
Q_EMIT mouseMove(pw);
}
if(mLeftMouseButtonPressed) {
if(mKeyDown[Qt::Key_Control]) { // look around in the scene
QQuaternion result;
QVector3D eyeDir = (mView-mEye).normalized();
float xangle = d2r(mLeftMouseButtonPressCoordX - me->x()) * 0.1f;
float yangle = -d2r(mLeftMouseButtonPressCoordY - me->y()) * 0.1f;
mLeftMouseButtonPressCoordX = me->x();
mLeftMouseButtonPressCoordY = me->y();
QVector3D yaxis = QVector3D(0,1,0); // rotate around the world's up vector
QQuaternion r_quat(cos(xangle/2), yaxis.x()*sin(xangle/2), yaxis.y()*sin(xangle/2), yaxis.z()*sin(xangle/2));
r_quat.normalize();
QQuaternion v_quat(0, eyeDir); // create view quaternion from view vector
QQuaternion u_quat(0, mUp);
result = (r_quat * v_quat) * r_quat.conjugate();
QVector3D eyeShit = QVector3D(result.x(), result.y(), result.z()).normalized();
mView = mEye + eyeShit;
result = (r_quat * u_quat) * r_quat.conjugate();
mUp = QVector3D(result.x(), result.y(), result.z()).normalized();
qDebug() << "eye" << mEye << "view" << mView << "up" << mUp << "eyeDir" << eyeDir;
// new eyedir based on the previous rotation about the vertical camera axis
eyeDir = (mView-mEye).normalized();
// rotation axis
QVector3D xaxis = QVector3D::crossProduct(mUp, eyeDir).normalized();
QQuaternion rr_quat(cos(xangle/2), xaxis.x()*sin(yangle/2), xaxis.y()*sin(yangle/2), xaxis.z()*sin(yangle/2));
rr_quat.normalize();
QQuaternion vv_quat(0, eyeDir); // create view quaternion from view vector
QQuaternion uu_quat(0, mUp);
result = (rr_quat * vv_quat) * rr_quat.conjugate();
eyeShit = QVector3D(result.x(), result.y(), result.z()).normalized();
mView = mEye + eyeShit;
result = (rr_quat * uu_quat) * rr_quat.conjugate();
mUp = QVector3D(result.x(), result.y(), result.z()).normalized();
qd << "quat res" << result;
update();
} else { // drag started
if(!mDragState) {
mDragState = true;
Q_EMIT(dragStarted(pw));
}
}
}
update();
}
void cGLWidget::mousePressEvent(QMouseEvent *me)
{
if(me->button() == Qt::LeftButton) {
// this is for navigation
mLeftMouseButtonPressed = true;
mLeftMouseButtonPressCoordX = me->x();
mLeftMouseButtonPressCoordY = me->y();
// this is for mouse click signal
mSavedX = me->x();
mSavedY = me->y();
}
QVector3D pw;
if(screenToWorld(me->x(), me->y(), -1.0, pw)) {
if(me->button() == Qt::LeftButton)
Q_EMIT leftMouseButtonPressed(pw);
else
Q_EMIT rightMouseButtonPressed(pw);
}
update();
}
void cGLWidget::mouseReleaseEvent(QMouseEvent *me)
{
QVector3D pw;
bool res = screenToWorld(me->x(), me->y(), -1.0, pw);
if(me->button() == Qt::LeftButton) {
if(mLeftMouseButtonPressed) {
if(mSavedX == me->x() && mSavedY == me->y()) {
if(res) {
Q_EMIT mouseButtonClicked(pw);
}
}
}
mLeftMouseButtonPressed = false;
if(mDragState) {
if(res) {
Q_EMIT dropped(pw);
}
}
mDragState = false;
}
}