glscope.cpp
14.8 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
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
// SPDX-License-Identifier: GPL-2.0+
#include <cmath>
#include <iostream>
#include <QColor>
#include <QDebug>
#include <QMatrix4x4>
#include <QMouseEvent>
#include <QOpenGLShaderProgram>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLFunctions_ES2>
#include "glscope.h"
#include "post/graphgenerator.h"
#include "post/ppresult.h"
#include "scopesettings.h"
#include "viewconstants.h"
#include "viewsettings.h"
#if defined(QT_OPENGL_ES_2)
typedef QOpenGLFunctions_ES2 OPENGL_VER;
#else
typedef QOpenGLFunctions_3_3_Core OPENGL_VER;
#endif
GlScope *GlScope::createNormal(DsoSettingsScope *scope, DsoSettingsView *view, QWidget *parent) {
GlScope *s = new GlScope(scope, view, parent);
s->zoomed = false;
return s;
}
GlScope *GlScope::createZoomed(DsoSettingsScope *scope, DsoSettingsView *view, QWidget *parent) {
GlScope *s = new GlScope(scope, view, parent);
s->zoomed = true;
return s;
}
GlScope::GlScope(DsoSettingsScope *scope, DsoSettingsView *view, QWidget *parent)
: QOpenGLWidget(parent), scope(scope), view(view) {
vaMarker.resize(MARKER_COUNT);
}
GlScope::~GlScope() {}
void GlScope::mousePressEvent(QMouseEvent *event) {
if (!zoomed && event->button() == Qt::LeftButton) {
double position = (double)(event->x() - width() / 2) * DIVS_TIME / (double)width();
double distance = DIVS_TIME;
selectedMarker = NO_MARKER;
// Capture nearest marker located within snap area (+/- 1% of full scale).
for (int marker = 0; marker < MARKER_COUNT; ++marker) {
if (!scope->horizontal.marker_visible[marker]) continue;
if (fabs(scope->horizontal.marker[marker] - position) < std::min(distance, DIVS_TIME / 100.0)) {
distance = fabs(scope->horizontal.marker[marker] - position);
selectedMarker = marker;
}
}
if (selectedMarker != NO_MARKER) { emit markerMoved(selectedMarker, position); }
}
event->accept();
}
void GlScope::mouseMoveEvent(QMouseEvent *event) {
if (!zoomed && (event->buttons() & Qt::LeftButton) != 0) {
double position = (double)(event->x() - width() / 2) * DIVS_TIME / (double)width();
if (selectedMarker == NO_MARKER) {
// User started draging outside the snap area of any marker:
// move all markers to current position and select last marker in the array.
for (unsigned marker = 0; marker < MARKER_COUNT; ++marker) {
emit markerMoved(marker, position);
selectedMarker = marker;
}
} else if (selectedMarker < MARKER_COUNT) {
emit markerMoved(selectedMarker, position);
}
}
event->accept();
}
void GlScope::mouseReleaseEvent(QMouseEvent *event) {
if (!zoomed && event->button() == Qt::LeftButton) {
double position = (double)(event->x() - width() / 2) * DIVS_TIME / (double)width();
if (selectedMarker < MARKER_COUNT) { emit markerMoved(selectedMarker, position); }
selectedMarker = NO_MARKER;
}
event->accept();
}
void GlScope::initializeGL() {
auto *gl = context()->versionFunctions<OPENGL_VER>();
m_program = std::unique_ptr<QOpenGLShaderProgram>(new QOpenGLShaderProgram(context()));
const char *vshaderES = R"(
#version 330
attribute highp vec3 vertex;
uniform mat4 matrix;
void main()
{
gl_Position = matrix * vec4(vertex, 1.0);
gl_PointSize = 1.0;
}
)";
const char *vshaderCore = R"(
#version 330
in highp vec3 vertex;
uniform mat4 matrix;
void main()
{
gl_Position = matrix * vec4(vertex, 1.0);
gl_PointSize = 1.0;
}
)";
const char *fshaderCore = R"(
#version 330
uniform highp vec4 colour;
void main() { gl_FragColor = colour; }
)";
qDebug() << "compile shaders";
// Compile vertex shader
bool coreShaders = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile;
if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, coreShaders ? vshaderCore : vshaderES) ||
!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fshaderCore)) {
qWarning() << "Failed to compile OpenGL shader programs.\n" << m_program->log();
return;
}
// Link shader pipeline
if (!m_program->link() || !m_program->bind()) {
qWarning() << "Failed to link/bind OpenGL shader programs";
return;
}
vertexLocation = m_program->attributeLocation("vertex");
matrixLocation = m_program->uniformLocation("matrix");
colorLocation = m_program->uniformLocation("colour");
if (vertexLocation == -1 || colorLocation == -1 || matrixLocation == -1) {
qWarning() << "Failed to locate shader variable";
return;
}
m_program->bind();
gl->glDisable(GL_DEPTH_TEST);
gl->glEnable(GL_BLEND);
// Enable depth buffer
gl->glEnable(GL_DEPTH_TEST);
// Enable back face culling
gl->glEnable(GL_CULL_FACE);
gl->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
QColor bg = view->screen.background;
gl->glClearColor((GLfloat)bg.redF(), (GLfloat)bg.greenF(), (GLfloat)bg.blueF(), (GLfloat)bg.alphaF());
generateGrid();
{
m_vaoMarker.create();
QOpenGLVertexArrayObject::Binder b(&m_vaoMarker);
m_marker.create();
m_marker.bind();
m_marker.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_marker.allocate(int(vaMarker.size() * sizeof(Line)));
m_program->enableAttributeArray(vertexLocation);
m_program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, 0);
}
m_program->release();
}
void GlScope::showData(PPresult *data) {
if (!m_program || !m_program->isLinked()) return;
makeCurrent();
// Remove too much entries
while (view->digitalPhosphorDraws() < m_GraphHistory.size()) m_GraphHistory.pop_back();
// Add if missing
if (view->digitalPhosphorDraws() > m_GraphHistory.size()) { m_GraphHistory.resize(m_GraphHistory.size() + 1); }
// Move last item to front
m_GraphHistory.splice(m_GraphHistory.begin(), m_GraphHistory, std::prev(m_GraphHistory.end()));
// Add new entry
m_GraphHistory.front().writeData(data, m_program.get(), vertexLocation);
// doneCurrent();
}
void GlScope::paintGL() {
if (!m_program->isLinked()) return;
auto *gl = context()->versionFunctions<OPENGL_VER>();
// Clear OpenGL buffer and configure settings
// TODO Don't clear if view->digitalPhosphorDraws()>1
gl->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gl->glLineWidth(1);
m_program->bind();
// Apply zoom settings via matrix transformation
if (zoomed) {
QMatrix4x4 m;
m.scale(QVector3D(DIVS_TIME / (GLfloat)fabs(scope->horizontal.marker[1] - scope->horizontal.marker[0]), 1.0f,
1.0f));
m.translate((GLfloat) - (scope->horizontal.marker[0] + scope->horizontal.marker[1]) / 2, 0.0f, 0.0f);
m_program->setUniformValue(matrixLocation, pmvMatrix * m);
}
unsigned historyIndex = 0;
for (Graph &graph : m_GraphHistory) {
for (ChannelID channel = 0; channel < scope->voltage.size(); ++channel) {
drawSpectrumChannelGraph(channel, graph, (int)historyIndex);
drawVoltageChannelGraph(channel, graph, (int)historyIndex);
}
++historyIndex;
}
if (zoomed) { m_program->setUniformValue(matrixLocation, pmvMatrix); }
if (!this->zoomed) drawMarkers();
drawGrid();
m_program->release();
}
void GlScope::resizeGL(int width, int height) {
auto *gl = context()->versionFunctions<OPENGL_VER>();
gl->glViewport(0, 0, (GLint)width, (GLint)height);
// Set axes to div-scale and apply correction for exact pixelization
float pixelizationWidthCorrection = (float)width / (width - 1);
float pixelizationHeightCorrection = (float)height / (height - 1);
pmvMatrix.setToIdentity();
pmvMatrix.ortho(-(DIVS_TIME / 2.0f) * pixelizationWidthCorrection, (DIVS_TIME / 2.0f) * pixelizationWidthCorrection,
-(DIVS_VOLTAGE / 2.0f) * pixelizationHeightCorrection,
(DIVS_VOLTAGE / 2.0f) * pixelizationHeightCorrection, -1.0f, 1.0f);
m_program->bind();
m_program->setUniformValue(matrixLocation, pmvMatrix);
m_program->release();
}
void GlScope::drawGrid() {
auto *gl = context()->versionFunctions<OPENGL_VER>();
gl->glLineWidth(1);
// Grid
m_vaoGrid[0].bind();
m_program->setUniformValue(colorLocation, view->screen.grid);
gl->glDrawArrays(GL_POINTS, 0, gridDrawCounts[0]);
m_vaoGrid[0].release();
// Axes
m_vaoGrid[1].bind();
m_program->setUniformValue(colorLocation, view->screen.axes);
gl->glDrawArrays(GL_LINES, 0, gridDrawCounts[1]);
m_vaoGrid[1].release();
// Border
m_vaoGrid[2].bind();
m_program->setUniformValue(colorLocation, view->screen.border);
gl->glDrawArrays(GL_LINE_LOOP, 0, gridDrawCounts[2]);
m_vaoGrid[2].release();
}
void GlScope::generateGrid() {
gridDrawCounts[0] = 0;
gridDrawCounts[1] = 0;
gridDrawCounts[2] = 0;
m_grid.create();
m_grid.bind();
m_grid.setUsagePattern(QOpenGLBuffer::StaticDraw);
std::vector<QVector3D> vaGrid;
{ // Bind draw vertical lines
m_vaoGrid[0].create();
QOpenGLVertexArrayObject::Binder b(&m_vaoGrid[0]);
m_grid.bind();
m_program->enableAttributeArray(vertexLocation);
m_program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, 0);
}
// Draw vertical lines
for (int div = 1; div < DIVS_TIME / 2; ++div) {
for (int dot = 1; dot < DIVS_VOLTAGE / 2 * DIVS_SUB; ++dot) {
float dotPosition = (float)dot / DIVS_SUB;
gridDrawCounts[0] += 4;
vaGrid.push_back(QVector3D(-div, -dotPosition, 0));
vaGrid.push_back(QVector3D(-div, dotPosition, 0));
vaGrid.push_back(QVector3D(div, -dotPosition, 0));
vaGrid.push_back(QVector3D(div, dotPosition, 0));
}
}
// Draw horizontal lines
for (int div = 1; div < DIVS_VOLTAGE / 2; ++div) {
for (int dot = 1; dot < DIVS_TIME / 2 * DIVS_SUB; ++dot) {
if (dot % DIVS_SUB == 0) continue; // Already done by vertical lines
float dotPosition = (float)dot / DIVS_SUB;
gridDrawCounts[0] += 4;
vaGrid.push_back(QVector3D(-dotPosition, -div, 0));
vaGrid.push_back(QVector3D(dotPosition, -div, 0));
vaGrid.push_back(QVector3D(-dotPosition, div, 0));
vaGrid.push_back(QVector3D(dotPosition, div, 0));
}
}
{ // Bind draw axes
m_vaoGrid[1].create();
QOpenGLVertexArrayObject::Binder b(&m_vaoGrid[1]);
m_grid.bind();
m_program->enableAttributeArray(vertexLocation);
m_program->setAttributeBuffer(vertexLocation, GL_FLOAT, int(vaGrid.size() * sizeof(QVector3D)), 3);
}
// Axes
// Horizontal axis
gridDrawCounts[1] += 4;
vaGrid.push_back(QVector3D(-DIVS_TIME / 2, 0, 0));
vaGrid.push_back(QVector3D(DIVS_TIME / 2, 0, 0));
// Vertical axis
vaGrid.push_back(QVector3D(0, -DIVS_VOLTAGE / 2, 0));
vaGrid.push_back(QVector3D(0, DIVS_VOLTAGE / 2, 0));
// Subdiv lines on horizontal axis
for (int line = 1; line < DIVS_TIME / 2 * DIVS_SUB; ++line) {
float linePosition = (float)line / DIVS_SUB;
gridDrawCounts[1] += 4;
vaGrid.push_back(QVector3D(linePosition, -0.05f, 0));
vaGrid.push_back(QVector3D(linePosition, 0.05f, 0));
vaGrid.push_back(QVector3D(-linePosition, -0.05f, 0));
vaGrid.push_back(QVector3D(-linePosition, 0.05f, 0));
}
// Subdiv lines on vertical axis
for (int line = 1; line < DIVS_VOLTAGE / 2 * DIVS_SUB; ++line) {
float linePosition = (float)line / DIVS_SUB;
gridDrawCounts[1] += 4;
vaGrid.push_back(QVector3D(-0.05f, linePosition, 0));
vaGrid.push_back(QVector3D(0.05f, linePosition, 0));
vaGrid.push_back(QVector3D(-0.05f, -linePosition, 0));
vaGrid.push_back(QVector3D(0.05f, -linePosition, 0));
}
{
m_vaoGrid[2].create();
QOpenGLVertexArrayObject::Binder b(&m_vaoGrid[2]);
m_grid.bind();
m_program->enableAttributeArray(vertexLocation);
m_program->setAttributeBuffer(vertexLocation, GL_FLOAT, int(vaGrid.size() * sizeof(QVector3D)), 3);
}
// Border
gridDrawCounts[2] += 4;
vaGrid.push_back(QVector3D(-DIVS_TIME / 2, -DIVS_VOLTAGE / 2, 0));
vaGrid.push_back(QVector3D(DIVS_TIME / 2, -DIVS_VOLTAGE / 2, 0));
vaGrid.push_back(QVector3D(DIVS_TIME / 2, DIVS_VOLTAGE / 2, 0));
vaGrid.push_back(QVector3D(-DIVS_TIME / 2, DIVS_VOLTAGE / 2, 0));
m_grid.allocate(&vaGrid[0], int(vaGrid.size() * sizeof(QVector3D)));
m_grid.release();
}
void GlScope::drawMarkers() {
auto *gl = context()->versionFunctions<OPENGL_VER>();
QColor trColor = view->screen.markers;
m_program->setUniformValue(colorLocation, trColor);
m_vaoMarker.bind();
for (unsigned marker = 0; marker < vaMarker.size(); ++marker) {
if (!scope->horizontal.marker_visible[marker]) continue;
vaMarker[marker] = {QVector3D((GLfloat)scope->horizontal.marker[marker], -DIVS_VOLTAGE, 0.0f),
QVector3D((GLfloat)scope->horizontal.marker[marker], DIVS_VOLTAGE, 0.0f)};
}
// Write coordinates to GPU
m_marker.bind();
m_marker.write(0, vaMarker.data(), vaMarker.size() * sizeof(Line));
// Draw all
gl->glLineWidth(1);
gl->glDrawArrays(GL_LINES, 0, (GLsizei)vaMarker.size() * 2);
m_marker.release();
// Draw selected
if (selectedMarker != NO_MARKER) {
gl->glLineWidth(3);
gl->glDrawArrays(GL_LINES, selectedMarker * 2, (GLsizei)2);
}
m_vaoMarker.release();
}
void GlScope::drawVoltageChannelGraph(ChannelID channel, Graph &graph, int historyIndex) {
if (!scope->voltage[channel].used) return;
m_program->setUniformValue(colorLocation, view->screen.voltage[channel].darker(100 + 10 * historyIndex));
Graph::VaoCount &v = graph.vaoVoltage[channel];
QOpenGLVertexArrayObject::Binder b(v.first);
const GLenum dMode = (view->interpolation == Dso::INTERPOLATION_OFF) ? GL_POINTS : GL_LINE_STRIP;
context()->versionFunctions<OPENGL_VER>()->glDrawArrays(dMode, 0, v.second);
}
void GlScope::drawSpectrumChannelGraph(ChannelID channel, Graph &graph, int historyIndex) {
if (!scope->spectrum[channel].used) return;
m_program->setUniformValue(colorLocation, view->screen.spectrum[channel].darker(100 + 10 * historyIndex));
Graph::VaoCount &v = graph.vaoSpectrum[channel];
QOpenGLVertexArrayObject::Binder b(v.first);
const GLenum dMode = (view->interpolation == Dso::INTERPOLATION_OFF) ? GL_POINTS : GL_LINE_STRIP;
context()->versionFunctions<OPENGL_VER>()->glDrawArrays(dMode, 0, v.second);
}