-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleGLWidget.cpp
More file actions
150 lines (114 loc) · 4.09 KB
/
ExampleGLWidget.cpp
File metadata and controls
150 lines (114 loc) · 4.09 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
#include "ExampleGLWidget.h"
#include <util/Exception.h>
#include <QPainter>
#include <QRectF>
ExampleGLWidget::ExampleGLWidget() :
QOpenGLWidget(),
_isInitialized(false),
_backgroundColor(235, 235, 235, 255),
_pointRenderer(this),
_pixelRatio(1.0f),
_points(),
_colors()
{
setAcceptDrops(true);
QSurfaceFormat surfaceFormat;
surfaceFormat.setRenderableType(QSurfaceFormat::OpenGL);
// Ask for an different OpenGL versions depending on OS
#if defined(__APPLE__)
surfaceFormat.setVersion(4, 1); // https://support.apple.com/en-us/101525
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
#elif defined(__linux__ )
surfaceFormat.setVersion(4, 2); // glxinfo | grep "OpenGL version"
surfaceFormat.setProfile(QSurfaceFormat::CompatibilityProfile);
#else
surfaceFormat.setVersion(4, 3);
surfaceFormat.setProfile(QSurfaceFormat::CoreProfile);
#endif
#ifdef _DEBUG
surfaceFormat.setOption(QSurfaceFormat::DebugContext);
#endif
surfaceFormat.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
surfaceFormat.setSamples(16);
setFormat(surfaceFormat);
}
ExampleGLWidget::~ExampleGLWidget()
{
cleanup();
}
void ExampleGLWidget::setData(const std::vector<mv::Vector2f>& points, float pointSize, float pointOpacity)
{
const auto numPoints = points.size();
_points = points;
_colors.clear();
_colors.reserve(numPoints);
constexpr mv::Vector3f pointColor = {0.f, 0.f, 0.f};
for(unsigned long i = 0; i < numPoints; i++)
_colors.push_back(pointColor);
Bounds bounds = Bounds::Max;
for (const Vector2f& point : points)
{
bounds.setLeft(std::min(point.x, bounds.getLeft()));
bounds.setRight(std::max(point.x, bounds.getRight()));
bounds.setBottom(std::min(point.y, bounds.getBottom()));
bounds.setTop(std::max(point.y, bounds.getTop()));
}
bounds.makeSquare();
bounds.expand(0.1f);
// Send the data to the renderer
_pointRenderer.setDataBounds(QRectF(QPointF(bounds.getLeft(), bounds.getBottom()), QSizeF(bounds.getWidth(), bounds.getHeight())));
_pointRenderer.setData(_points);
_pointRenderer.setColors(_colors);
_pointRenderer.setPointSize(pointSize);
_pointRenderer.setAlpha(pointOpacity);
_pointRenderer.initView();
// Calls paintGL()
update();
}
void ExampleGLWidget::initializeGL()
{
initializeOpenGLFunctions();
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &ExampleGLWidget::cleanup);
// Initialize renderers
_pointRenderer.init();
// Set a default color map for both renderers
_pointRenderer.setScalarEffect(PointEffect::None);
_pointRenderer.setPointScaling(Absolute);
_pointRenderer.setPointSize(1.f);
_pointRenderer.setAlpha(0.5f);
_pointRenderer.setSelectionOutlineColor(Vector3f(1, 0, 0));
// OpenGL is initialized
_isInitialized = true;
emit initialized();
}
void ExampleGLWidget::resizeGL(int w, int h)
{
// we need this here as we do not have the screen yet to get the actual devicePixelRatio when the view is created
_pixelRatio = devicePixelRatio();
// Pixel ration tells us how many pixels map to a point
// That is needed as macOS calculates in points and we do in pixels
// On macOS high dpi displays pixel ration is 2
w *= _pixelRatio;
h *= _pixelRatio;
_pointRenderer.resize(QSize(w, h));
}
void ExampleGLWidget::paintGL()
{
initializeOpenGLFunctions();
// Bind the framebuffer belonging to the widget
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject());
// Clear the widget to the background color
glClearColor(_backgroundColor.redF(), _backgroundColor.greenF(), _backgroundColor.blueF(), _backgroundColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset the blending function
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
_pointRenderer.render();
}
void ExampleGLWidget::cleanup()
{
qDebug() << "Deleting widget, performing clean up...";
_isInitialized = false;
makeCurrent();
_pointRenderer.destroy();
}