-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathglwidget.cpp
548 lines (433 loc) · 13.1 KB
/
glwidget.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// This file is a part of "grblControl" application.
// Copyright 2015 Hayrullin Denis Ravilevich
#include "glwidget.h"
#include "tooldrawer.h"
#include <QDebug>
#include <QtWidgets>
#include <QPainter>
#include <QEasingCurve>
#ifdef GLES
#include <GLES/gl.h>
#endif
#define ZOOMSTEP 1.1
#ifdef GLES
GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent), m_shaderProgram(0)
#else
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent), m_shaderProgram(0)
#endif
{
m_animateView = false;
m_updatesEnabled = false;
m_xRot = 90;
m_yRot = 0;
m_xRotTarget = 90;
m_yRotTarget = 0;
m_zoom = 1;
m_xPan = 0;
m_yPan = 0;
m_distance = 100;
m_xLookAt = 0;
m_yLookAt = 0;
m_zLookAt = 0;
m_xMin = 0;
m_xMax = 0;
m_yMin = 0;
m_yMax = 0;
m_zMin = 0;
m_zMax = 0;
m_xSize = 0;
m_ySize = 0;
m_zSize = 0;
updateProjection();
updateView();
m_spendTime.setHMS(0, 0, 0);
m_estimatedTime.setHMS(0, 0, 0);
QTimer::singleShot(1000, this, SLOT(onFramesTimer()));
setFps(60);
}
GLWidget::~GLWidget()
{
if (m_shaderProgram) {
delete m_shaderProgram;
}
}
double GLWidget::calculateVolume(QVector3D size) {
return size.x() * size.y() * size.z();
}
void GLWidget::addDrawable(ShaderDrawable *drawable)
{
m_shaderDrawables.append(drawable);
}
void GLWidget::fitDrawable(ShaderDrawable *drawable)
{
stopViewAnimation();
if (drawable != NULL) {
updateExtremes(drawable);
double a = m_ySize / 2 / 0.25 * 1.3
+ (m_zMax - m_zMin) / 2;
double b = m_xSize / 2 / 0.25 * 1.3
/ ((double)this->width() / this->height())
+ (m_zMax - m_zMin) / 2;
m_distance = qMax(a, b);
if (m_distance == 0) m_distance = 200;
m_xLookAt = (m_xMax - m_xMin) / 2 + m_xMin;
m_zLookAt = -((m_yMax - m_yMin) / 2 + m_yMin);
m_yLookAt = (m_zMax - m_zMin) / 2 + m_zMin;
} else {
m_distance = 200;
m_xLookAt = 0;
m_yLookAt = 0;
m_zLookAt = 0;
m_xMin = 0;
m_xMax = 0;
m_yMin = 0;
m_yMax = 0;
m_zMin = 0;
m_zMax = 0;
m_xSize = 0;
m_ySize = 0;
m_zSize = 0;
}
m_xPan = 0;
m_yPan = 0;
m_zoom = 1;
updateProjection();
updateView();
}
void GLWidget::updateExtremes(ShaderDrawable *drawable)
{
if (!std::isnan(drawable->getMinimumExtremes().x())) m_xMin = drawable->getMinimumExtremes().x(); else m_xMin = 0;
if (!std::isnan(drawable->getMaximumExtremes().x())) m_xMax = drawable->getMaximumExtremes().x(); else m_xMax = 0;
if (!std::isnan(drawable->getMinimumExtremes().y())) m_yMin = drawable->getMinimumExtremes().y(); else m_yMin = 0;
if (!std::isnan(drawable->getMaximumExtremes().y())) m_yMax = drawable->getMaximumExtremes().y(); else m_yMax = 0;
if (!std::isnan(drawable->getMinimumExtremes().z())) m_zMin = drawable->getMinimumExtremes().z(); else m_zMin = 0;
if (!std::isnan(drawable->getMaximumExtremes().z())) m_zMax = drawable->getMaximumExtremes().z(); else m_zMax = 0;
m_xSize = m_xMax - m_xMin;
m_ySize = m_yMax - m_yMin;
m_zSize = m_zMax - m_zMin;
}
bool GLWidget::antialiasing() const
{
return m_antialiasing;
}
void GLWidget::setAntialiasing(bool antialiasing)
{
m_antialiasing = antialiasing;
}
void GLWidget::onFramesTimer()
{
m_fps = m_frames;
m_frames = 0;
QTimer::singleShot(1000, this, SLOT(onFramesTimer()));
}
void GLWidget::viewAnimation()
{
double t = (double)m_animationFrame++ / (m_targetFps * 0.2);
if (t == 1) stopViewAnimation();
QEasingCurve ec(QEasingCurve::OutExpo);
double val = ec.valueForProgress(t);
m_xRot = m_xRotStored + double(m_xRotTarget - m_xRotStored) * val;
m_yRot = m_yRotStored + double(m_yRotTarget - m_yRotStored) * val;
updateView();
}
bool GLWidget::msaa() const
{
return m_msaa;
}
void GLWidget::setMsaa(bool msaa)
{
m_msaa = msaa;
}
bool GLWidget::updatesEnabled() const
{
return m_updatesEnabled;
}
void GLWidget::setUpdatesEnabled(bool updatesEnabled)
{
m_updatesEnabled = updatesEnabled;
}
bool GLWidget::zBuffer() const
{
return m_zBuffer;
}
void GLWidget::setZBuffer(bool zBuffer)
{
m_zBuffer = zBuffer;
}
QString GLWidget::bufferState() const
{
return m_bufferState;
}
void GLWidget::setBufferState(const QString &bufferState)
{
m_bufferState = bufferState;
}
QString GLWidget::parserStatus() const
{
return m_parserStatus;
}
void GLWidget::setParserStatus(const QString &parserStatus)
{
m_parserStatus = parserStatus;
}
double GLWidget::lineWidth() const
{
return m_lineWidth;
}
void GLWidget::setLineWidth(double lineWidth)
{
m_lineWidth = lineWidth;
}
void GLWidget::setTopView()
{
m_xRotTarget = 90;
m_yRotTarget = m_yRot > 180 ? 360 : 0;
beginViewAnimation();
}
void GLWidget::setFrontView()
{
m_xRotTarget = 0;
m_yRotTarget = m_yRot > 180 ? 360 : 0;
beginViewAnimation();
}
void GLWidget::setLeftView()
{
m_xRotTarget = 0;
m_yRotTarget = m_yRot > 270 ? 450 : 90;
beginViewAnimation();
}
int GLWidget::fps()
{
return m_targetFps;
}
void GLWidget::setIsometricView()
{
m_xRotTarget = 45;
m_yRotTarget = m_yRot > 180 ? 405 : 45;
beginViewAnimation();
}
void GLWidget::beginViewAnimation() {
m_xRotStored = m_xRot;
m_yRotStored = m_yRot;
m_animationFrame = 0;
m_animateView = true;
}
void GLWidget::stopViewAnimation() {
m_animateView = false;
}
QColor GLWidget::colorText() const
{
return m_colorText;
}
void GLWidget::setColorText(const QColor &colorText)
{
m_colorText = colorText;
}
QColor GLWidget::colorBackground() const
{
return m_colorBackground;
}
void GLWidget::setColorBackground(const QColor &colorBackground)
{
m_colorBackground = colorBackground;
}
void GLWidget::setFps(int fps)
{
if (fps <= 0) return;
m_targetFps = fps;
m_timerAnimation.stop();
m_timerAnimation.start(1000 / fps, Qt::PreciseTimer, this);
}
QTime GLWidget::estimatedTime() const
{
return m_estimatedTime;
}
void GLWidget::setEstimatedTime(const QTime &estimatedTime)
{
m_estimatedTime = estimatedTime;
}
QTime GLWidget::spendTime() const
{
return m_spendTime;
}
void GLWidget::setSpendTime(const QTime &spendTime)
{
m_spendTime = spendTime;
}
void GLWidget::initializeGL()
{
// Create shader program
m_shaderProgram = new QOpenGLShaderProgram();
if (m_shaderProgram) {
// Compile vertex shader
m_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl");
// Compile fragment shader
m_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl");
// Link shader pipeline
m_shaderProgram->link();
qDebug() << "shader program created";
}
}
void GLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
updateProjection();
emit resized();
}
void GLWidget::updateProjection()
{
// Reset projection
m_projectionMatrix.setToIdentity();
double asp = (double)width() / height();
m_projectionMatrix.frustum((-0.5 + m_xPan) * asp, (0.5 + m_xPan) * asp, -0.5 + m_yPan, 0.5 + m_yPan, 2, m_distance * 2);
}
void GLWidget::updateView()
{
// Set view matrix
m_viewMatrix.setToIdentity();
double r = m_distance;
double angY = M_PI / 180 * m_yRot;
double angX = M_PI / 180 * m_xRot;
QVector3D eye(r * cos(angX) * sin(angY) + m_xLookAt, r * sin(angX) + m_yLookAt, r * cos(angX) * cos(angY) + m_zLookAt);
QVector3D center(m_xLookAt, m_yLookAt, m_zLookAt);
QVector3D up(fabs(m_xRot) == 90 ? -sin(angY + (m_xRot < 0 ? M_PI : 0)) : 0, cos(angX), fabs(m_xRot) == 90 ? -cos(angY + (m_xRot < 0 ? M_PI : 0)) : 0);
m_viewMatrix.lookAt(eye, center, up.normalized());
m_viewMatrix.translate(m_xLookAt, m_yLookAt, m_zLookAt);
m_viewMatrix.scale(m_zoom, m_zoom, m_zoom);
m_viewMatrix.translate(-m_xLookAt, -m_yLookAt, -m_zLookAt);
m_viewMatrix.rotate(-90, 1.0, 0.0, 0.0);
}
#ifdef GLES
void GLWidget::paintGL()
#else
void GLWidget::paintEvent(QPaintEvent *pe)
#endif
{
QPainter painter(this);
// Segment counter
int vertices = 0;
painter.beginNativePainting();
// Clear viewport
glClearColor(m_colorBackground.redF(), m_colorBackground.greenF(), m_colorBackground.blueF(), 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update settings
if (m_antialiasing) {
if (m_msaa) glEnable(GL_MULTISAMPLE); else {
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
}
if (m_zBuffer) glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
if (m_shaderProgram) {
// Draw 3d
m_shaderProgram->bind();
// Set modelview-projection matrix
m_shaderProgram->setUniformValue("mvp_matrix", m_projectionMatrix * m_viewMatrix);
m_shaderProgram->setUniformValue("mv_matrix", m_viewMatrix);
// Update geometries in current opengl context
foreach (ShaderDrawable *drawable, m_shaderDrawables)
if (drawable->needsUpdateGeometry()) drawable->updateGeometry(m_shaderProgram);
// Draw geometries
foreach (ShaderDrawable *drawable, m_shaderDrawables) {
drawable->draw(m_shaderProgram);
if (drawable->visible()) vertices += drawable->getVertexCount();
}
m_shaderProgram->release();
}
// Draw 2D
glShadeModel(GL_FLAT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_MULTISAMPLE);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_BLEND);
painter.endNativePainting();
QPen pen(m_colorText);
painter.setPen(pen);
double x = 10;
double y = this->height() - 60;
painter.drawText(QPoint(x, y), QString("X: %1 ... %2").arg(m_xMin, 0, 'f', 3).arg(m_xMax, 0, 'f', 3));
painter.drawText(QPoint(x, y + 15), QString("Y: %1 ... %2").arg(m_yMin, 0, 'f', 3).arg(m_yMax, 0, 'f', 3));
painter.drawText(QPoint(x, y + 30), QString("Z: %1 ... %2").arg(m_zMin, 0, 'f', 3).arg(m_zMax, 0, 'f', 3));
painter.drawText(QPoint(x, y + 45), QString("%1 / %2 / %3").arg(m_xSize, 0, 'f', 3).arg(m_ySize, 0, 'f', 3).arg(m_zSize, 0, 'f', 3));
QFontMetrics fm(painter.font());
painter.drawText(QPoint(x, fm.height() + 10), m_parserStatus);
QString str = QString(tr("Vertices: %1")).arg(vertices);
painter.drawText(QPoint(this->width() - fm.width(str) - 10, y + 30), str);
str = QString("FPS: %1").arg(m_fps);
painter.drawText(QPoint(this->width() - fm.width(str) - 10, y + 45), str);
str = m_spendTime.toString("hh:mm:ss") + " / " + m_estimatedTime.toString("hh:mm:ss");
painter.drawText(QPoint(this->width() - fm.width(str) - 10, y), str);
str = m_bufferState;
painter.drawText(QPoint(this->width() - fm.width(str) - 10, y + 15), str);
m_frames++;
#ifdef GLES
update();
#endif
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
m_lastPos = event->pos();
m_xLastRot = m_xRot;
m_yLastRot = m_yRot;
m_xLastPan = m_xPan;
m_yLastPan = m_yPan;
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
if ((event->buttons() & Qt::MiddleButton && !(event->modifiers() & Qt::ShiftModifier)) || event->buttons() & Qt::LeftButton) {
stopViewAnimation();
m_yRot = normalizeAngle(m_yLastRot - (event->pos().x() - m_lastPos.x()) * 0.5);
m_xRot = m_xLastRot + (event->pos().y() - m_lastPos.y()) * 0.5;
if (m_xRot < -90) m_xRot = -90;
if (m_xRot > 90) m_xRot = 90;
updateView();
emit rotationChanged();
}
if ((event->buttons() & Qt::MiddleButton && event->modifiers() & Qt::ShiftModifier) || event->buttons() & Qt::RightButton) {
m_xPan = m_xLastPan - (event->pos().x() - m_lastPos.x()) * 1 / (double)width();
m_yPan = m_yLastPan + (event->pos().y() - m_lastPos.y()) * 1 / (double)height();
updateProjection();
}
}
void GLWidget::wheelEvent(QWheelEvent *we)
{
if (m_zoom > 0.1 && we->delta() < 0) {
m_xPan -= ((double)we->pos().x() / width() - 0.5 + m_xPan) * (1 - 1 / ZOOMSTEP);
m_yPan += ((double)we->pos().y() / height() - 0.5 - m_yPan) * (1 - 1 / ZOOMSTEP);
m_zoom /= ZOOMSTEP;
}
else if (m_zoom < 10 && we->delta() > 0)
{
m_xPan -= ((double)we->pos().x() / width() - 0.5 + m_xPan) * (1 - ZOOMSTEP);
m_yPan += ((double)we->pos().y() / height() - 0.5 - m_yPan) * (1 - ZOOMSTEP);
m_zoom *= ZOOMSTEP;
}
updateProjection();
updateView();
}
void GLWidget::timerEvent(QTimerEvent *te)
{
if (te->timerId() == m_timerAnimation.timerId()) {
if (m_animateView) viewAnimation();
#ifndef GLES
if (m_updatesEnabled) update();
#endif
} else {
#ifdef GLES
QOpenGLWidget::timerEvent(te);
#else
QGLWidget::timerEvent(te);
#endif
}
}
double GLWidget::normalizeAngle(double angle)
{
while (angle < 0) angle += 360;
while (angle > 360) angle -= 360;
return angle;
}