Skip to content

Commit d19631b

Browse files
committed
Improved alpha control, implemented slice based depth sorting
1 parent 2eaef44 commit d19631b

10 files changed

Lines changed: 156 additions & 63 deletions

res/shaders/VolumeDraw.frag

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ uniform vec3 selectionColor;
66
uniform bool live; // false when tracking is lost
77
uniform int selectMode;
88
uniform float selectRadius;
9-
uniform bool selectionEmpty;
9+
uniform float baseOpacity;
1010

1111
uniform sampler2D colormap;
1212
uniform bool selecting;
@@ -21,18 +21,15 @@ out vec4 fragColor;
2121

2222
void main()
2323
{
24-
25-
fragColor = vec4(.6, .6, .6, 1);
24+
fragColor = vec4(.6, .6, .6, baseOpacity);
2625

2726
if (hasColors) {
2827
vec3 color = texture(colormap, vec2(v_Color, 1-v_Color)).rgb;
29-
if(selectionEmpty){
30-
fragColor = vec4(color, 1);
31-
} else {
32-
fragColor = vec4(color, .01);
33-
}
28+
29+
fragColor = vec4(color, baseOpacity);
30+
3431

35-
//fragColor = vec4(v_Color, 0, 1-v_Color, 1); // Depth sorting test
32+
//fragColor = vec4(v_Color, 0, 1-v_Color, 1); // Test color for Depth sorting test
3633
}
3734

3835
if(selecting){
@@ -72,11 +69,18 @@ void main()
7269
}
7370
}
7471

72+
if (isCursor){
73+
fragColor = vec4(selectionColor, 1);
74+
}
75+
7576
// Highlight selected points
76-
if (isCursor || vHighlight != 0)
77+
if (vHighlight != 0)
7778
{
78-
// fragColor = vec4(selectionColor, 1);
79-
fragColor.a = 1;
79+
if(baseOpacity > 0.4){
80+
fragColor = vec4(selectionColor, 1);
81+
} else {
82+
fragColor.a = 1;
83+
}
8084
}
8185

8286
// If the position is not tracked live, display the data in dark gray

src/Actions/SettingsAction.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SettingsAction::SettingsAction(QObject* parent, const QString& title) :
2323
_toggleFullScreen(this, "Toggle Full Screen"),
2424
_clearSelectionAction(this, "Clear selection"),
2525
_eyeOffsetAction(this, "Eye offset", 0, 0.2, 0.03, 3),
26-
_colorAdjust(this, "Adjust COlor", 0, 1, 0, 2),
26+
_pointOpacityAction(this, "Point opacity", 0, 1, 1.0f, 2),
2727
_camDistAction(this, "Cam dist", 0, 3, 1.75, 2),
2828
_selectionColorPicker(this, "Selection Color", QColor(255,0,0))
2929
{
@@ -75,15 +75,18 @@ SettingsAction::SettingsAction(QObject* parent, const QString& title) :
7575

7676
connect(&_clearSelectionAction, &TriggerAction::triggered, this, [this]() {
7777
_plugin->clearSelection();
78+
_pointOpacityAction.setValue(1.f);
7879
});
7980

8081

8182
connect(&_startCalibAction, &TriggerAction::triggered, this, [this]() { _plugin->getOpenGLRendererWidget().openCalib(); });
8283

8384
connect(&_eyeOffsetAction, &DecimalAction::valueChanged, [this](const float& value) { _plugin->getOpenGLRendererWidget().setEyeOffset(value); });
8485

85-
connect(&_colorAdjust, &DecimalAction::valueChanged, [this](const float& value) {
86-
_plugin->getOpenGLRendererWidget().setColorPoints(value);
86+
87+
_plugin->getOpenGLRendererWidget().getVolumeRenderer().setPointOpacity(_pointOpacityAction.getValue());
88+
connect(&_pointOpacityAction, &DecimalAction::valueChanged, [this](const float& value) {
89+
_plugin->getOpenGLRendererWidget().getVolumeRenderer().setPointOpacity(value);
8790
});
8891

8992

src/Actions/SettingsAction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class SettingsAction : public GroupAction
6363
TriggerAction& getStartCalibAction() { return _startCalibAction; }
6464
TriggerAction& getToggleFullScreen() { return _toggleFullScreen; }
6565
TriggerAction& getClearSelectionAction() { return _clearSelectionAction; }
66-
DecimalAction& getColorAdjustAction() { return _colorAdjust; }
66+
DecimalAction& getPointOpacityAction() { return _pointOpacityAction; }
6767
DecimalAction& getEyeOffsetAction() { return _eyeOffsetAction; }
6868
DecimalAction& getCamDistAction() { return _camDistAction; }
6969
ColorAction& getSelectionColorPicker() { return _selectionColorPicker; }
@@ -83,7 +83,7 @@ class SettingsAction : public GroupAction
8383
TriggerAction _startCalibAction;
8484
TriggerAction _clearSelectionAction;
8585
TriggerAction _toggleFullScreen;
86-
DecimalAction _colorAdjust;
86+
DecimalAction _pointOpacityAction;
8787
DecimalAction _eyeOffsetAction;
8888
DecimalAction _camDistAction;
8989
ColorAction _selectionColorPicker;

src/Renderer/OpenGL/DepthSorter.cpp

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int Octree::debugStructure(const int& depth) const
142142
/// </summary>
143143
/// <param name="position"></param>
144144
/// <param name="indices">Vector to which the sorted indexes will be appened.</param>
145-
void Octree::getSortedIndices(const QVector3D& position, std::vector<GLuint>& indices)
145+
void Octree::getSortedIndicesUsingCubes(const QVector3D& position, std::vector<GLuint>& indices)
146146
{
147147
if (contained.size() > 0) {
148148
/*for (GLuint index : contained) {
@@ -186,7 +186,7 @@ void Octree::getSortedIndices(const QVector3D& position, std::vector<GLuint>& in
186186

187187
for (int index : orderedChildren) {
188188
if (children[index] != nullptr) {
189-
children[index]->getSortedIndices(position, indices);
189+
children[index]->getSortedIndicesUsingCubes(position, indices);
190190
}
191191
}
192192

@@ -205,12 +205,15 @@ WorkerThread::WorkerThread(
205205
QObject* parent = nullptr,
206206
std::vector<std::vector<GLuint>>* inds = nullptr,
207207
std::vector<float> pts = std::vector<float>(0),
208-
std::vector<QVector3D>* cams = nullptr
209-
) : QThread(parent) {
208+
std::vector<QMatrix4x4>* camMatrices = nullptr
209+
) : QThread(parent),
210+
previousCameraSide(std::vector<int>(camMatrices->size(), 0)),
211+
sliceSorts(std::vector<std::vector<GLuint>>(3, std::vector<GLuint>(0)))
212+
{
210213
indices = inds;
211214
points = pts;
212215
// Camera position relative to the cloud of points
213-
camPos = cams;
216+
cams = camMatrices;
214217

215218
std::vector<GLuint> allIndices = std::vector<GLuint>(points.size() / 3);
216219

@@ -228,6 +231,37 @@ WorkerThread::WorkerThread(
228231
//int treeSize = pointTree->debugStructure();
229232
//qDebug() << "Tree size " << treeSize;
230233
//qDebug() << "Indices size " << indices->size();
234+
235+
// Create the slice sorts
236+
for (int side = 0; side < 3; side++) {
237+
qDebug() << "Cretating slices for side : " << side;
238+
clock_t start = clock();
239+
int nSlices = 100;
240+
float coordmin = -1.f;
241+
float coordmax = 1.f;
242+
243+
for (int k = 0; k < points.size() / 3; k++) {
244+
if (points[3 * k + side] < coordmin) {
245+
coordmin = points[3 * k + dataAxesConversion[side]] - 0.1f;
246+
}
247+
if (points[3 * k + side] > coordmax) {
248+
coordmax = points[3 * k + dataAxesConversion[side]] + 0.1f;
249+
}
250+
}
251+
for (int slice = 0; slice < nSlices; slice++) {
252+
for (int k = 0; k < points.size() / 3; k++) {
253+
254+
if (
255+
points[3 * k + dataAxesConversion[side]] >= coordmin + (coordmax - coordmin) * slice / nSlices
256+
&& points[3 * k + dataAxesConversion[side]] < coordmin + (coordmax - coordmin) * (slice + 1) / nSlices
257+
) {
258+
sliceSorts[side].push_back(k);
259+
}
260+
261+
}
262+
}
263+
qDebug() << "Cretated slices for side : " << side << ", in " << clock() - start;
264+
}
231265
}
232266

233267
WorkerThread::~WorkerThread() {
@@ -244,7 +278,7 @@ void WorkerThread::run() {
244278
while (true) {
245279
for (int i = 0; i < 2; i++) {
246280
if (
247-
(camPos->at(i) - previousCameras[i]).length() > 0.01
281+
(getCamPos(i) - previousCameras[i]).length() > 0.01f
248282
&& clock() - start[i] > 0.05f * CLOCKS_PER_SEC
249283
) {
250284
start[i] = clock();
@@ -262,19 +296,21 @@ void WorkerThread::run() {
262296
});*/
263297
// Bruteforce method - END
264298

265-
// Octree based method
266-
localIndices[i].clear();
267-
pointTree->getSortedIndices(camPos->at(i), localIndices[i]);
268-
// Octree based method - END
299+
//// Octree based method
300+
//localIndices[i].clear();
301+
//pointTree->getSortedIndicesUsingCubes(camPos->at(i), localIndices[i]);
302+
//// Octree based method - END
269303

270-
std::mutex mtx;
271-
mtx.lock();
272-
indices->at(i).assign(localIndices[i].begin(), localIndices[i].end());
273-
mtx.unlock();
304+
//std::mutex mtx;
305+
//mtx.lock();
306+
//indices->at(i).assign(localIndices[i].begin(), localIndices[i].end());
307+
//mtx.unlock();
308+
//emit resultReady(i);
274309

275-
emit resultReady(i);
310+
sliceSort();
276311

277-
previousCameras[i] = camPos->at(i);
312+
313+
previousCameras[i] = getCamPos(i);
278314

279315
}
280316

@@ -285,3 +321,38 @@ void WorkerThread::run() {
285321
}
286322

287323

324+
325+
void WorkerThread::sliceSort() {
326+
std::vector<int> cameraSide = std::vector<int>(2, 0);
327+
//Determine f the front facing side has changed for each amera
328+
for (int cam = 0; cam < cameraSide.size(); cam++) {
329+
float biggestComponentValue = 0;
330+
for (int i = 0; i < 3; i++) {
331+
QVector3D unitVector = QVector3D(0,0,0);
332+
unitVector[i] = 1.0f;
333+
float component = QVector3D::dotProduct(getCamDir(cam), unitVector);
334+
if (abs(component) >= biggestComponentValue) {
335+
cameraSide[cam] = i+1;
336+
if (component < 0) cameraSide[cam] *= -1;
337+
biggestComponentValue = abs(component);
338+
}
339+
}
340+
if (cameraSide[cam] != previousCameraSide[cam]) {
341+
qDebug() << "Cam dir" << getCamDir(cam);
342+
qDebug() << "Detected change in face on camera : " << cam << ", setting new face : " << cameraSide[cam];
343+
std::mutex mtx;
344+
mtx.lock();
345+
qDebug() << abs(cameraSide[cam]) - 1;
346+
indices->at(cam).clear();
347+
indices->at(cam).assign(sliceSorts[abs(cameraSide[cam]) - 1].begin(), sliceSorts[abs(cameraSide[cam]) - 1].end());
348+
if (cameraSide[cam] < 0) {
349+
std::reverse(indices->at(cam).begin(), indices->at(cam).end());
350+
}
351+
mtx.unlock();
352+
353+
emit resultReady(cam);
354+
previousCameraSide[cam] = cameraSide[cam];
355+
}
356+
}
357+
358+
}

src/Renderer/OpenGL/DepthSorter.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <QThread>
66
#include <QVector3D>
7+
#include <QMatrix4x4>
78

89
#include <cmath>
910

@@ -48,7 +49,7 @@ class Octree {
4849
/// </summary>
4950
/// <param name="position"></param>
5051
/// <param name="indices"></param>
51-
void getSortedIndices(const QVector3D& position, std::vector<GLuint>& indices);
52+
void getSortedIndicesUsingCubes(const QVector3D& position, std::vector<GLuint>& indices);
5253
/*float distance(const QVector3D& a, const QVector3D& b) {
5354
float dx = a[0] - b[0];
5455
float dy = a[1] - b[1];
@@ -81,15 +82,24 @@ class WorkerThread : public QThread
8182
QObject* parent,
8283
std::vector<std::vector<GLuint>>* inds,
8384
std::vector<float> pts,
84-
std::vector<QVector3D>* cams
85+
std::vector<QMatrix4x4>* camMatrices
8586
);
8687
~WorkerThread();
8788
protected:
8889
Octree* pointTree = nullptr;
8990
std::vector<float> points;
9091
std::vector<std::vector<GLuint>>* indices;
91-
std::vector<QVector3D>* camPos;
92+
93+
std::vector<std::vector<GLuint>> sliceSorts; // Contains the 3 slice-based sortings
94+
std::vector<QMatrix4x4>* cams = nullptr;
95+
std::vector<int> previousCameraSide; // takes valeus -3,..., -1, 0 (means undefined), 1, ...,3. The coordinate 1 means axis x, 2 axis y, 3 axis z
96+
97+
std::vector<int> dataAxesConversion = {0,1,2};//{2,1,0};
98+
9299
void run() override;
100+
void sliceSort();
101+
QVector3D getCamPos(const int& eye) { return (cams->at(eye) * QVector4D(0, 0, 0, 1.0)).toVector3DAffine(); }
102+
QVector3D getCamDir(const int& eye) { return (cams->at(eye) * QVector4D(0, 0, 1.0, 1.0)).toVector3DAffine(); }
93103
/*float distance(const QVector3D& a, const QVector3D& b) {
94104
float dx = a[0] - b[0];
95105
float dy = a[1] - b[1];

src/Renderer/OpenGL/OpenGLRendererWidget.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
OpenGLRendererWidget::OpenGLRendererWidget() :
1515
QOpenGLWidget(),
1616
_tracker(nullptr),
17-
modelCameraPositions(std::vector<QVector3D>(2)),
1817
renderingOrders(std::vector<std::vector<GLuint>>(2)),
18+
cameraInModelRef(std::vector<QMatrix4x4>(2)),
1919
msgLabel(new QLabel(this))
2020
{
2121
// UI
@@ -113,7 +113,7 @@ void OpenGLRendererWidget::setData(std::vector<float>& data)
113113

114114
void OpenGLRendererWidget::startThreading() {
115115

116-
workerThread = new WorkerThread(this, &renderingOrders, points, &modelCameraPositions);
116+
workerThread = new WorkerThread(this, &renderingOrders, points, &cameraInModelRef);
117117
connect(workerThread, &WorkerThread::resultReady, this, [this](const int& i) {
118118
std::mutex mtx;
119119
mtx.lock();
@@ -162,6 +162,7 @@ void OpenGLRendererWidget::setColormap(const QImage& colormap)
162162
qDebug() << "Height : " << colormap.height();
163163
}
164164

165+
165166
void OpenGLRendererWidget::setTracker(PSTracker* tracker)
166167
{
167168
_tracker = tracker;
@@ -356,21 +357,21 @@ void OpenGLRendererWidget::paintGL()
356357

357358
pose = offset * pose;
358359
QMatrix4x4 invertedPose = pose.inverted();
359-
QVector4D homogCamPos0;
360+
cameraInModelRef[0].setToIdentity();
360361
if (!_volumeRenderer.isStereo()) {
361-
homogCamPos0 = _volumeRenderer.getHeadPosition().toVector4D();
362+
cameraInModelRef[0].lookAt(_volumeRenderer.getHeadPosition(), QVector3D(0,0,0), QVector3D(0, 1, 0));
362363
}
363364
else {
364-
QVector4D homogCamPos1;
365+
cameraInModelRef[1].setToIdentity();
366+
367+
368+
cameraInModelRef[0].lookAt(_volumeRenderer.getStereoCamera(0), QVector3D(0, 0, 0), QVector3D(0, 1, 0));
369+
cameraInModelRef[1].lookAt(_volumeRenderer.getStereoCamera(1), QVector3D(0, 0, 0), QVector3D(0, 1, 0));
365370

366-
homogCamPos0 = _volumeRenderer.getStereoCamera(0).toVector4D();
367-
homogCamPos1 = _volumeRenderer.getStereoCamera(1).toVector4D();
368371

369-
homogCamPos1[3] = 1.f;
370-
modelCameraPositions[1] = (invertedPose * homogCamPos1).toVector3DAffine();
372+
cameraInModelRef[1] = invertedPose * cameraInModelRef[1];
371373
}
372-
homogCamPos0[3] = 1.f;
373-
modelCameraPositions[0] = (invertedPose * homogCamPos0).toVector3DAffine();
374+
cameraInModelRef[0] = invertedPose * cameraInModelRef[0];
374375

375376
_volumeRenderer.render(defaultFramebufferObject(), aspect, _tracker->poseIsLive(pluginInstanceIndex), pose);
376377
}

src/Renderer/OpenGL/OpenGLRendererWidget.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,6 @@ class OpenGLRendererWidget : public QOpenGLWidget, QOpenGLFunctions_4_2_Core
9898
*/
9999
QVector3D getCamPos() const;
100100

101-
/// <summary>
102-
/// For test during dev
103-
/// </summary>
104-
void setColorPoints(const float& value) {
105-
std::vector<float> colors = std::vector<float>(points.size() / 3, value );
106-
setColors(colors);
107-
};
108101

109102
protected:
110103

@@ -175,8 +168,9 @@ private slots:
175168

176169
std::vector<float> points;
177170

178-
std::vector<std::vector<GLuint>> renderingOrders; // One for each camera
179-
std::vector<QVector3D> modelCameraPositions; // One for each camera
171+
std::vector<std::vector<GLuint>> renderingOrders; // One for each eye
172+
std::vector<QMatrix4x4> cameraInModelRef; // One for each eye
173+
180174

181175
WorkerThread* workerThread;
182176

0 commit comments

Comments
 (0)