-
Notifications
You must be signed in to change notification settings - Fork 1
/
opengl_widget.cpp
1369 lines (1143 loc) · 49.4 KB
/
opengl_widget.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
@brief Implementation
@date 2018/04/27
@author
Eric Wescott
<wescotte@gmail.com>
This is a modification of the OSVR-Distortionizer application found at:
https://github.com/OSVR/distortionizer
*/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "opengl_widget.h"
#include <QtGui>
#include <QtOpenGL>
#include <QColor>
#include <QFileDialog>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <QJsonDocument>
#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE 0x809D
#endif
#define CONFIG_FILE "HMD_Config.json"
//----------------------------------------------------------------------
// Helper functions
OpenGL_Widget::OpenGL_Widget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
, d_cop_l(QPoint(0, 0))
, d_cop_r(QPoint(0, 0))
, d_cop(QPoint(0, 0))
, d_cop_l_Prev(QPoint(0, 0))
, d_cop_r_Prev(QPoint(0, 0))
{
using namespace std;
cout << "Distortion estimation for SteamVR HMDs" << endl
<< endl
<< "Keyboard controls:" << endl
<< "SPACEBAR - Toggles stauts overlay ON/OFF" << endl
<< "ENTER KEY - Toggle Linear Transforms ON/OFF/Aspect Ratio Only/Center Only" << endl
<< endl
<< "Z/X: Toggle the LEFT and RIGHT eye ON/OFF when applying values" << endl
<< "1/2/3: Toggle on/off the 1st, 2nd, and 3rd coeffiecent" << endl
<< "Q/W/E: Toggle on/off the GREEN, BLUE, and RED colors" << endl
<< "LEFT and RIGHT arrow key: DECREASE and INCREASE the offset value to be applied" << endl
<< "UP and DOWN arrow keys: INCREASE and DECREASE all active options values" << endl
<< endl
<< "SHIFT + arrow keys: Move the center of projection by one pixel" << endl
<< "CONTROL + arrow keys: Aspect Ratio (LEFT/RIGHT hortizinal UP/DOWN vertical)" << endl
<< "I - Apply center correction to Intrensics" << endl
<< "NOTE: You can adjust the center without changing the Intrensics so you have to use \"I\" to actually apply these values" << endl
<< endl
<< "G - Reset recenter (DOES NOT affect intrensics) for active eye" << endl
<< "H - Reset coeffiecents to 0.0 for all active eyes/colors/coefficents" << endl
<< "J - Reset aspect ratio to default for all active eyes" << endl
<< "K - Reset recenter (DOES affect intrensics) for active eye" << endl
<< endl
<< "S/L: Save/Load state from JSON config file (" << CONFIG_FILE << ")" << endl
<< "ESCAPE: Quit the application" << endl
<< endl;
displayOverValues = false;
for (int eye = 0; eye < 2; eye++)
for (int color = 0; color < 3; color++)
for (int term = 0; term < 3; term++)
NLT_Coeffecients[eye][color][term] = 0.0;
coeffecientOffset = 0.001;
// Set default settings
// TODO: The Intrinsics isn't quite working right yet so it's disabled by default
status = LEFT_EYE | RIGHT_EYE | GREEN | BLUE | RED | FIRST_COEFFICIENT | SECOND_COEFFICIENT | THIRD_COEFFICIENT; // | APPLY_LINEAR_TRANSFORM;
// Load inital values from JSON file
QTimer::singleShot(0, [=] { loadInitalValues(); });
}
OpenGL_Widget::~OpenGL_Widget()
{
}
void OpenGL_Widget::initializeGL()
{
qglClearColor(Qt::black);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_MULTISAMPLE);
static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// Makes the colors for the primitives be what we want.
glDisable(GL_LIGHTING);
}
// The distortion is with respect to a center of projection, which
// is common for all three colors. The first-order correction that
// can be applied assumes that there is an additional radial shift
// that is proportionaly to the square of the distance of a pixel
// from the center of projection, with the coefficient called K1:
// RcorrR = Rinit + K1R * (Rinit * Rinit)
// RcorrG = Rinit + K1G * (Rinit * Rinit)
// RcorrB = Rinit + K1B * (Rinit * Rinit)
// 0 <= K1R <= K1G <= K1B
//
// We can solve for a pre-distorted position that would produce
// a desired end location after passing through the optical system
// by solving for Rinit in the above equations (here done for a
// single color):
// K1 * Rinit^2 + Rinit - Rcorr = 0
// Solving for the positive root of the quadratic equation
// A = K1, B = 1, C = -Rcorr
// Rinit = (-B + sqrt(B^2 - 4AC)) / 2A
// = (-1 + sqrt(1 + 4*K1*Rcorr)) / (2*K1)
// K1 > 0
QPointF OpenGL_Widget::transformPoint(QPointF p, QPointF cop, unsigned color, StatusValues eye, bool debug)
{
QPointF ret = p;
// SteamVR has three distortion components
// Two linear (Intrinsics, and Extrinsics) and the non linear inverse radial distortion.
// The Intrinsics allows you to adjust the center and aspect ratio of each dimention.
// We adjust the center in setDeftCOPVals() but here is where we adjust the aspect ratios
// TODO: Impliment the Extrinsics
if ((status & APPLY_LINEAR_TRANSFORM) == APPLY_LINEAR_TRANSFORM || (status & ONLY_ASEPECT_RATIO) == ONLY_ASEPECT_RATIO) {
double x, y;
double rX, rY;
x = p.x();
y = p.y();
rX = cop.x() - x;
rY = cop.y() - y;
if (eye == LEFT_EYE) {
x = cop.x() - (rX*Intrinsics[0][0][0]);
y = cop.y() - (rY*Intrinsics[0][1][1]);
}
else {
x = cop.x() - (rX*Intrinsics[1][0][0]);
y = cop.y() - (rY*Intrinsics[1][1][1]);
}
ret.setX(x);
ret.setY(y);
}
// Non linear transform
// Formula for reversing the lens distortion obtained form Wikipeida as it is slightly different than what the
// original OSVR Distortionizer application was using.
// https://en.wikipedia.org/wiki/Distortion_(optics)#Software_correction
// TODO: SteamVR has a "type": "DISTORT_DPOLY3" paramater for each eye's "distortion", "distortion_blue", "distortion_red" section
// and I believe this informs SteamVR to use a different algorithm... So in the future might need to check this value
// to ensure the proper algorithm is being used.
QPointF offset = ret - cop;
double r2 = offset.x() * offset.x() + offset.y() * offset.y();
double r = sqrt(r2);
double k1, k2, k3;
switch (color) {
case 0:
if (eye == LEFT_EYE) {
k1 = NLT_Coeffecients[0][2][0];
k2 = NLT_Coeffecients[0][2][1];
k3 = NLT_Coeffecients[0][2][2];
}
else {
k1 = NLT_Coeffecients[1][2][0];
k2 = NLT_Coeffecients[1][2][1];
k3 = NLT_Coeffecients[1][2][2];
}
break;
case 1:
if (eye == LEFT_EYE) {
k1 = NLT_Coeffecients[0][0][0];
k2 = NLT_Coeffecients[0][0][1];
k3 = NLT_Coeffecients[0][0][2];
}
else {
k1 = NLT_Coeffecients[1][0][0];
k2 = NLT_Coeffecients[1][0][1];
k3 = NLT_Coeffecients[1][0][2];
}
break;
case 2:
if (eye == LEFT_EYE) {
k1 = NLT_Coeffecients[0][1][0];
k2 = NLT_Coeffecients[0][1][1];
k3 = NLT_Coeffecients[0][1][2];
}
else {
k1 = NLT_Coeffecients[1][1][0];
k2 = NLT_Coeffecients[1][1][1];
k3 = NLT_Coeffecients[1][1][2];
}
break;
}
// Normalized fix
// SteamVR seems to require these coeffiecnts fall in the range of -1 < X < 1
// The original tool was not properly normalizing for non square screens like the Vive.
// Also, I believe it was incorrectly scaling them by a factor of 16 so I dropped it from the equation.
double maxRadius = sqrt(d_width / 4 * d_width / 4 + d_height / 2 * d_height / 2);
k1 = k1 / pow(maxRadius, 2);
k2 = k2 / pow(maxRadius, 4);
k3 = k3 / pow(maxRadius, 6);
double c1, c2, c3;
c1 = k1 * pow(r, 2);
c2 = k2 * pow(r, 4);
c3 = k3 * pow(r, 6);
double k = 1 / (1 + c1 + c2 + c3);
ret = cop + (k * offset);
// Cull the two eyes so any drawings on one doesn't overlap with the other.
// Not very inteligent and justs draws the point outside the screen boundry for now.
if (eye == LEFT_EYE) {
if (ret.x() < 0 || ret.x() > d_width / 2)
ret.setX(-1);
if (ret.y() < 0 || ret.y() > d_height)
ret.setY(-1);
}
if (eye == RIGHT_EYE) {
if (ret.x() > d_width || ret.x() < d_width / 2)
ret.setX(d_width + 1);
if (ret.y() < 0 || ret.y() > d_height)
ret.setY(-1);
}
return ret;
}
void OpenGL_Widget::drawCorrectedLine(QPoint begin, QPoint end,
QPointF cop, unsigned color, StatusValues eye)
{
QPointF offset = end - begin;
float len = sqrt(offset.x() * offset.x() + offset.y() * offset.y());
QPointF offset_dir = offset / len;
glBegin(GL_LINE_STRIP);
for (float s = 0; s <= len; s++) {
QPointF p = begin + s * offset_dir;
QPointF tp = transformPoint(p, cop, color, eye);
// Enable culling... Don't draw any vertices outside of the screen area
if (tp.x() > -1 && tp.x() < d_width + 1 && tp.y() > -1 && tp.y() < d_height + 1)
glVertex2f(tp.x(), tp.y());
}
glEnd();
}
void OpenGL_Widget::drawCorrectedCircle(QPointF center, float radius,
QPointF cop, unsigned color, StatusValues eye)
{
glBegin(GL_LINE_STRIP);
float step = 1 / radius;
for (float r = 0; r <= 2 * M_PI; r += step) {
QPointF p(center.x() + radius * cos(r),
center.y() + radius * sin(r));
QPointF tp = transformPoint(p, cop, color, eye);
// Enable culling... Don't draw any vertices outside of the screen area
if (tp.x() > -1 && tp.x() < d_width + 1 && tp.y() > -1 && tp.y() < d_height + 1)
glVertex2f(tp.x(), tp.y());
}
glEnd();
}
void OpenGL_Widget::drawCorrectedLines(QPoint begin, QPoint end, QPointF cop, StatusValues eye)
{
float bright = 0.5f;
glColor3f(bright, 0.0, 0.0);
drawCorrectedLine(begin, end, cop, 0, eye);
glColor3f(0.0, bright, 0.0);
drawCorrectedLine(begin, end, cop, 1, eye);
glColor3f(0.0, 0.0, bright);
drawCorrectedLine(begin, end, cop, 2, eye);
}
void OpenGL_Widget::drawCorrectedCircles(QPointF center, float radius, QPointF cop, StatusValues eye)
{
float bright = 0.5f;
glColor3f(bright, 0.0, 0.0);
drawCorrectedCircle(center, radius, cop, 0, eye);
glColor3f(0.0, bright, 0.0);
drawCorrectedCircle(center, radius, cop, 1, eye);
glColor3f(0.0, 0.0, bright);
drawCorrectedCircle(center, radius, cop, 2, eye);
}
void OpenGL_Widget::drawCrossHairs()
{
// Draw two perpendicular lines through the center of
// projection on the left eye, and the right eye.
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_LINES);
glVertex2f(0, d_cop_l.y());
glVertex2f(d_width / 2, d_cop_l.y());
glVertex2f(d_cop_l.x(), 0);
glVertex2f(d_cop_l.x(), d_height);
glVertex2f(d_width / 2, d_cop_r.y());
glVertex2f(d_width, d_cop_r.y());
glVertex2f(d_cop_r.x(), 0);
glVertex2f(d_cop_r.x(), d_height);
glEnd();
}
void OpenGL_Widget::drawGrid()
{
// Draw a set of vertical grid lines to the right and left
// of the center of projection for each eye. Draw a red,
// green, and blue line at each location with less than
// full brightness. Draw from the top of the screen to
// the bottom.
int spacing = 40;
// Vertical lines
// Left Eye - Right side of mid point
for (int r = spacing; (d_cop_l.x() + r) < (d_width / 2); r += spacing) {
QPoint begin(d_cop_l.x() + r, 0);
QPoint end(d_cop_l.x() + r, d_height - 1);
drawCorrectedLines(begin, end, d_cop_l, LEFT_EYE);
}
// Left Eye - Left side of mid point
for (int r = spacing; (d_cop_l.x() - r) > 0; r += spacing) {
QPoint begin(d_cop_l.x() - r, 0);
QPoint end(d_cop_l.x() - r, d_height - 1);
drawCorrectedLines(begin, end, d_cop_l, LEFT_EYE);
}
// Right Eye - Right side of mid point
for (int r = spacing; (d_cop_r.x() + r) < (d_width); r += spacing) {
QPoint begin(d_cop_r.x() + r, 0);
QPoint end(d_cop_r.x() + r, d_height - 1);
drawCorrectedLines(begin, end, d_cop_r, RIGHT_EYE);
}
// Right Eye - Left side of mid point
for (int r = spacing; (d_cop_r.x() - r) > (d_width / 2); r += spacing) {
QPoint begin(d_cop_r.x() - r, 0);
QPoint end(d_cop_r.x() - r, d_height - 1);
drawCorrectedLines(begin, end, d_cop_r, RIGHT_EYE);
}
// Horizontal lines
// Left Eye - Top of mid point
for (int r = spacing; (d_cop_l.y() - r) > 0; r += spacing) {
QPoint begin(0, d_cop_l.y() - r);
QPoint end(d_width / 2, d_cop_l.y() - r);
drawCorrectedLines(begin, end, d_cop_l, LEFT_EYE);
}
// Left Eye - Bottom of mid point
for (int r = spacing; (d_cop_l.y() + r) < d_height; r += spacing) {
QPoint begin(0, d_cop_l.y() + r);
QPoint end(d_width / 2, d_cop_l.y() + r);
drawCorrectedLines(begin, end, d_cop_l, LEFT_EYE);
}
// Right Eye - Top of mid point
for (int r = spacing; (d_cop_r.y() - r) > 0; r += spacing) {
QPoint begin(d_width / 2, d_cop_r.y() - r);
QPoint end(d_width, d_cop_r.y() - r);
drawCorrectedLines(begin, end, d_cop_r, RIGHT_EYE);
}
// Right Eye - Bottom of mid point
for (int r = spacing; (d_cop_r.y() + r) < d_height; r += spacing) {
QPoint begin(d_width / 2, d_cop_r.y() + r);
QPoint end(d_width, d_cop_r.y() + r);
drawCorrectedLines(begin, end, d_cop_r, RIGHT_EYE);
}
}
void OpenGL_Widget::drawCircles()
{
drawCorrectedCircles(d_cop_l, 0.1 * d_width / 4, d_cop_l, LEFT_EYE);
drawCorrectedCircles(d_cop_r, 0.1 * d_width / 4, d_cop_r, RIGHT_EYE);
drawCorrectedCircles(d_cop_l, 0.3 * d_width / 4, d_cop_l, LEFT_EYE);
drawCorrectedCircles(d_cop_r, 0.3 * d_width / 4, d_cop_r, RIGHT_EYE);
drawCorrectedCircles(d_cop_l, 0.7 * d_width / 4, d_cop_l, LEFT_EYE);
drawCorrectedCircles(d_cop_r, 0.7 * d_width / 4, d_cop_r, RIGHT_EYE);
}
void OpenGL_Widget::paintGL()
{
qglClearColor(Qt::black);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
// Set up rendering state.
// Turn on blending, so that we'll get white
// lines when we draw three different-colored lines
// in the same location. Also turn off Z-buffer
// test so we get all of the lines drawn. Also,
// turn off texture-mapping.
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);
drawCrossHairs();
drawGrid();
drawCircles();
//if (!IntrensicsMode) {
// printf("here");
// drawCrossHairs();
// drawGrid();
// drawCircles();
//}
//else {
// drawImages();
// drawImagesOverlay();
//}
// Draw text overlays
// TODO: Figure out why the hell the QPainter is filling the screen with white making the overlay aspect not really work...
if (displayOverValues) {
QPainter painter(this);
painter.setPen(Qt::black);
painter.setFont(QFont("Helvetica", 20));
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
int ltX = 0, ltY = 0;
int rtX = (d_width / 2) + 1;
int rtY = 0;
ltX = d_cop_l.x(); ltY = d_cop_l.y();
rtX = d_cop_r.x(); rtY = d_cop_r.y();
int xOffset = -250;
int yOffset = 0;
char msg[1024];
// Applying to Eyes:
if ((status & LEFT_EYE) == LEFT_EYE && (status & RIGHT_EYE) == RIGHT_EYE)
sprintf(msg, "APPLYING TO: BOTH EYES");
else if ((status & LEFT_EYE) == LEFT_EYE && (status & RIGHT_EYE) != RIGHT_EYE)
sprintf(msg, "APPLYING TO: LEFT EYE ONLY");
else if ((status & LEFT_EYE) != LEFT_EYE && (status & RIGHT_EYE) == RIGHT_EYE)
sprintf(msg, "APPLYING TO: RIGHT EYE ONLY");
else
sprintf(msg, "APPLYING TO: NO EYES");
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
// Applying to Cofficients:
sprintf(msg, "Offset Amount: %11.10f", coeffecientOffset);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, "Modifying Coeffiecients: ");
if ((status & FIRST_COEFFICIENT) == FIRST_COEFFICIENT)
sprintf(msg, "%sFIRST\t", msg);
if ((status & SECOND_COEFFICIENT) == SECOND_COEFFICIENT)
sprintf(msg, "%sSECOND\t", msg);
if ((status & THIRD_COEFFICIENT) == THIRD_COEFFICIENT)
sprintf(msg, "%sTHIRD\t", msg);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
// Applying to colors:
sprintf(msg, "Modifying Color: ");
if ((status & GREEN) == GREEN)
sprintf(msg, "%sGREEN\t", msg);
if ((status & BLUE) == BLUE)
sprintf(msg, "%sBLUE\t", msg);
if ((status & RED) == RED)
sprintf(msg, "%sRED\t", msg);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
// Performing linear transform?
sprintf(msg, "Linear Transform Applied: ");
if ((status & APPLY_LINEAR_TRANSFORM) == APPLY_LINEAR_TRANSFORM)
sprintf(msg, "%s\tBOTH", msg);
else if ((status & ONLY_CENTER_CORRECT) == ONLY_CENTER_CORRECT)
sprintf(msg, "%s\tCenter Only", msg);
else if ((status & ONLY_ASEPECT_RATIO) == ONLY_ASEPECT_RATIO)
sprintf(msg, "%s\tAspect Ratio Only", msg);
else
sprintf(msg, "%s\tNone", msg);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, "------------- LEFT EYE ------------- ------------- RIGHT EYE -------------\n");
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, " %-13s%-13s%-13s%-13s%-13s%-13s\n", "GREEN", "BLUE", "RED", "GREEN", "BLUE", "RED");
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, "coeff1: %-13.8g%-13.8g%-13.8g%-13.8g%-13.8g%-13.8g\n", NLT_Coeffecients[0][0][0], NLT_Coeffecients[0][1][0],
NLT_Coeffecients[0][2][0], NLT_Coeffecients[1][0][0], NLT_Coeffecients[1][1][0], NLT_Coeffecients[1][2][0]);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, "coeff1: %-13.8g%-13.8g%-13.8g%-13.8g%-13.8g%-13.8g\n", NLT_Coeffecients[0][0][1], NLT_Coeffecients[0][1][1],
NLT_Coeffecients[0][2][1], NLT_Coeffecients[1][0][1], NLT_Coeffecients[1][1][1], NLT_Coeffecients[1][2][1]);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, "coeff1: %-13.8g%-13.8g%-13.8g%-13.8g%-13.8g%-13.8g\n", NLT_Coeffecients[0][0][2], NLT_Coeffecients[0][1][2],
NLT_Coeffecients[0][2][2], NLT_Coeffecients[1][0][2], NLT_Coeffecients[1][1][2], NLT_Coeffecients[1][2][2]);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, "Center X: %-13.8g Center Y: %-13.8g Center X: %-13.8g Center Y: %-13.8g"
, Intrinsics[0][0][2], Intrinsics[0][1][2], Intrinsics[1][0][2], Intrinsics[1][1][2]);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
sprintf(msg, "Aspect X: %-13.8g Aspect Y: %-13.8g Aspect X: %-13.8g Aspect Y: %-13.8g"
, Intrinsics[0][0][0], Intrinsics[0][1][1], Intrinsics[1][0][0], Intrinsics[1][1][1]);
painter.drawText(ltX + xOffset, ltY + yOffset, msg);
painter.drawText(rtX + xOffset, rtY + yOffset, msg);
yOffset = yOffset + 50;
painter.end();
}
}
void OpenGL_Widget::resizeGL(int width, int height)
{
d_width = width;
d_height = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Make the window one unit high (-0.5 to 0.5) and have an aspect ratio that matches
// the aspect ratio of the window. We also make the left side of the window be at
// the origin.
float aspect;
if ((height <= 0) || (width < 0)) {
aspect = 1.0;
}
else {
aspect = static_cast<float>(width) / height;
}
glOrtho(0, d_width - 1, 0, d_height - 1, 5.0, 15.0);
glMatrixMode(GL_MODELVIEW);
// Hack... For some reasons on some systems this is being called for each monitor instead of just the one we specify...
// Now for debugging purposes I still want this to work on a regular machine w/o a Vive.
// However, if that monitor is detected first (before the Vive) then it screws up the center positions for the Vive screen...
// So if we detect a Vive resolution then we just force it to use that one instead.
// This way we can debug on a regular screen and still ensure the Vive works
if (width == 2160 && height == 1200) {
d_cop_l_Prev = QPointF(0, 0);
d_cop_r_Prev = QPointF(0, 0);
}
setDeftCOPVals();
}
void OpenGL_Widget::setDeftCOPVals() {
// TODO: This is VERY clunky... Figure out a better way to handle the center shifts.
// Set intial center values
if (d_cop_l_Prev == QPointF(0, 0) && d_cop_r_Prev == QPointF(0, 0)) {
// Default center of projection is the center of the left half
// of the screen.
d_cop_l.setX(d_width / 4);
d_cop_l.setY(d_height / 2);
// Find the mirror of the left-eye's center of projection
// around the screen center to find the right eye's COP.
d_cop_r = QPoint(d_width - d_cop_l.x(), d_cop_l.y());
d_cop_l_Prev = d_cop_l;
d_cop_r_Prev = d_cop_r;
}
else if ((status & APPLY_LINEAR_TRANSFORM) == APPLY_LINEAR_TRANSFORM || (status & ONLY_CENTER_CORRECT) == ONLY_CENTER_CORRECT) {
// Backup previous center value because now we're going to use the linear transform center
// Only backup during the first transition though...
if ((status & ONLY_CENTER_CORRECT) != ONLY_CENTER_CORRECT) {
d_cop_l_Prev = d_cop_l;
d_cop_r_Prev = d_cop_r;
}
double CxL, CxR, Cy;
CxL = d_width / 4;
CxR = d_width / 2 + CxL;
Cy = d_height / 2;
d_cop_l.setX(CxL + (CxL * Intrinsics[0][0][2]));
d_cop_l.setY(Cy + (Cy * Intrinsics[0][1][2]));
d_cop_r.setX(CxR + (CxR * Intrinsics[1][0][2]));
d_cop_r.setY(Cy + (Cy * Intrinsics[1][1][2]));
}
else {
// Restore previous center value because we are no longer using linear transform for center
d_cop_l = d_cop_l_Prev;
d_cop_r = d_cop_r_Prev;
}
}
void OpenGL_Widget::keyPressEvent(QKeyEvent *event)
{
StatusValues toggle = NO_VALUE;
switch (event->key()) {
case Qt::Key_Escape:
QApplication::quit();
break;
case Qt::Key_S: // Save the state to an output file.
// XXX Would like to throw a dialog box, but it shows in HMD
// and cannot be moved.
saveConfigToJson(CONFIG_FILE);
break;
case Qt::Key_L: // Load the state from an output file.
// XXX Would like to throw a dialog box, but it shows in HMD
// and cannot be moved.
loadConfigFromJson(CONFIG_FILE);
break;
// Toggle coeffiecents
// TODO: Broken... For some reason this also is toggling the APPLY_LINEAR_TRANSFORM and I don't know why yet... Investigate
//case '`': // Can't find the damn Qt::Key_XXXX for a Tilde!
// toggle = FIRST_COEFFICIENT | SECOND_COEFFICIENT | THIRD_COEFFICIENT;
// status = status ^ toggle;
// if ( (status & toggle) != NO_VALUE)
// status = status | toggle;
// else
// status = status | (~toggle);
break;
case Qt::Key_1:
status = status ^ FIRST_COEFFICIENT;
break;
case Qt::Key_2:
status = status ^ SECOND_COEFFICIENT;
break;
case Qt::Key_3:
status = status ^ THIRD_COEFFICIENT;
break;
// Toggle Colors
// TODO: Broken... For some reason this also is toggling the APPLY_LINEAR_TRANSFORM and I don't know why yet... Investigate
//case Qt::Key_Tab:
// toggle = GREEN | BLUE | RED;
// status = status ^ toggle;
// if ( (status & toggle) != NO_VALUE)
// status = status | toggle;
// else
// status = status | (~toggle);
// break;
case Qt::Key_Q:
status = status ^ GREEN;
break;
case Qt::Key_W:
status = status ^ BLUE;
break;
case Qt::Key_E:
status = status ^ RED;
break;
// Toggle Eyes
case Qt::Key_Z:
status = status ^ LEFT_EYE;
break;
case Qt::Key_X:
status = status ^ RIGHT_EYE;
break;
// Toggle linear transform on/off/partial
case Qt::Key_Enter:
case Qt::Key_Return:
toggleLinearTransform();
break;
// Adjust modification value or adjust center
case Qt::Key_Left:
if (event->modifiers() & Qt::ShiftModifier)
shiftCenter(0, -1);
else if (event->modifiers() & Qt::ControlModifier)
adjustAspectRatio(-1, 0);
else
shiftCoeffecientOffset(-1);
break;
case Qt::Key_Right:
if (event->modifiers() & Qt::ShiftModifier)
shiftCenter(0, 1);
else if (event->modifiers() & Qt::ControlModifier)
adjustAspectRatio(1, 0);
else
shiftCoeffecientOffset(1);
break;
case Qt::Key_Down:
if (event->modifiers() & Qt::ShiftModifier)
shiftCenter(1, 0);
else if (event->modifiers() & Qt::ControlModifier)
adjustAspectRatio(0, -1);
else
adjustCoeffecients(-1);
break;
case Qt::Key_Up:
if (event->modifiers() & Qt::ShiftModifier)
shiftCenter(-1, 0);
else if (event->modifiers() & Qt::ControlModifier)
adjustAspectRatio(0, 1);
else
adjustCoeffecients(1);
break;
case Qt::Key_I:
ApplyCenterToIntrinsics();
break;
// Toggle Text overlay
case Qt::Key_Space:
if (displayOverValues)
displayOverValues = false;
else
displayOverValues = true;
break;
// Reset values
case Qt::Key_G:
resetCenter(false);
break;
case Qt::Key_H:
adjustCoeffecients(0);
// Added reset functionality to the adjustCoeffecients function so don't need a dedicated reset anymore.
// TODO: Might want to repurpose the reset so it 0's out all regardless of what is "active"
// resetCoeffiecents();
break;
case Qt::Key_J:
adjustAspectRatio(-2, -2);
break;
case Qt::Key_K:
resetCenter(true);
break;
}
updateGL();
}
void OpenGL_Widget::mousePressEvent(QMouseEvent *event)
{
}
void OpenGL_Widget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
// XXX
}
else if (event->buttons() & Qt::RightButton) {
// XXX
}
}
QPointF OpenGL_Widget::pixelToRelative(QPointF cop) {
QPointF relative_cop;
float cop_x, cop_y;
cop_x = (float)cop.x() / (float)d_width;
cop_y = (float)cop.y() / (float)d_height;
relative_cop.setX(cop_x);
relative_cop.setY(cop_y);
return relative_cop;
}
QPoint OpenGL_Widget::relativeToPixel(QPointF cop) {
QPoint pixel_cop;
int cop_x, cop_y;
cop_x = cop.x() * d_width;
cop_y = cop.y() * d_height;
pixel_cop.setX(cop_x);
pixel_cop.setY(cop_y);
return pixel_cop;
}
bool OpenGL_Widget::saveConfigToJson(QString filename)
{
// Todo... Add some basic error checking
// Left Eye
json["tracking_to_eye_transform"][0]["distortion"]["center_x"].SetDouble(Centers[0][0]);
json["tracking_to_eye_transform"][0]["distortion"]["center_y"].SetDouble(Centers[0][1]);
// Intrinsics
json["tracking_to_eye_transform"][0]["intrinsics"][0][0].SetDouble(Intrinsics[0][0][0]);
json["tracking_to_eye_transform"][0]["intrinsics"][0][1].SetDouble(Intrinsics[0][0][1]);
json["tracking_to_eye_transform"][0]["intrinsics"][0][2].SetDouble(Intrinsics[0][0][2]);
json["tracking_to_eye_transform"][0]["intrinsics"][1][0].SetDouble(Intrinsics[0][1][0]);
json["tracking_to_eye_transform"][0]["intrinsics"][1][1].SetDouble(Intrinsics[0][1][1]);
json["tracking_to_eye_transform"][0]["intrinsics"][1][2].SetDouble(Intrinsics[0][1][2]);
json["tracking_to_eye_transform"][0]["intrinsics"][2][0].SetDouble(Intrinsics[0][2][0]);
json["tracking_to_eye_transform"][0]["intrinsics"][2][1].SetDouble(Intrinsics[0][2][1]);
json["tracking_to_eye_transform"][0]["intrinsics"][2][2].SetDouble(Intrinsics[0][2][2]);
// Green
json["tracking_to_eye_transform"][0]["distortion"]["coeffs"][0].SetDouble(NLT_Coeffecients[0][0][0]);
json["tracking_to_eye_transform"][0]["distortion"]["coeffs"][1].SetDouble(NLT_Coeffecients[0][0][1]);
json["tracking_to_eye_transform"][0]["distortion"]["coeffs"][2].SetDouble(NLT_Coeffecients[0][0][2]);
json["tracking_to_eye_transform"][0]["distortion"]["center_x"].SetDouble(Intrinsics[0][0][2]);
json["tracking_to_eye_transform"][0]["distortion"]["center_y"].SetDouble(Intrinsics[0][1][2]);
// Blue
json["tracking_to_eye_transform"][0]["distortion_blue"]["coeffs"][0].SetDouble(NLT_Coeffecients[0][1][0]);
json["tracking_to_eye_transform"][0]["distortion_blue"]["coeffs"][1].SetDouble(NLT_Coeffecients[0][1][1]);
json["tracking_to_eye_transform"][0]["distortion_blue"]["coeffs"][2].SetDouble(NLT_Coeffecients[0][1][2]);
json["tracking_to_eye_transform"][0]["distortion_blue"]["center_x"].SetDouble(Intrinsics[0][0][2]);
json["tracking_to_eye_transform"][0]["distortion_blue"]["center_y"].SetDouble(Intrinsics[0][1][2]);
// Red
json["tracking_to_eye_transform"][0]["distortion_red"]["coeffs"][0].SetDouble(NLT_Coeffecients[0][2][0]);
json["tracking_to_eye_transform"][0]["distortion_red"]["coeffs"][1].SetDouble(NLT_Coeffecients[0][2][1]);
json["tracking_to_eye_transform"][0]["distortion_red"]["coeffs"][2].SetDouble(NLT_Coeffecients[0][2][2]);
json["tracking_to_eye_transform"][0]["distortion_red"]["center_x"].SetDouble(Intrinsics[0][0][2]);
json["tracking_to_eye_transform"][0]["distortion_red"]["center_y"].SetDouble(Intrinsics[0][1][2]);
// Right Eye
json["tracking_to_eye_transform"][1]["distortion"]["center_x"].SetDouble(Centers[1][0]);
json["tracking_to_eye_transform"][1]["distortion"]["center_y"].SetDouble(Centers[1][1]);
// Intrinsics
json["tracking_to_eye_transform"][1]["intrinsics"][0][0].SetDouble(Intrinsics[1][0][0]);
json["tracking_to_eye_transform"][1]["intrinsics"][0][1].SetDouble(Intrinsics[1][0][1]);
json["tracking_to_eye_transform"][1]["intrinsics"][0][2].SetDouble(Intrinsics[1][0][2]);
json["tracking_to_eye_transform"][1]["intrinsics"][1][0].SetDouble(Intrinsics[1][1][0]);
json["tracking_to_eye_transform"][1]["intrinsics"][1][1].SetDouble(Intrinsics[1][1][1]);
json["tracking_to_eye_transform"][1]["intrinsics"][1][2].SetDouble(Intrinsics[1][1][2]);
json["tracking_to_eye_transform"][1]["intrinsics"][2][0].SetDouble(Intrinsics[1][2][0]);
json["tracking_to_eye_transform"][1]["intrinsics"][2][1].SetDouble(Intrinsics[1][2][1]);
json["tracking_to_eye_transform"][1]["intrinsics"][2][2].SetDouble(Intrinsics[1][2][2]);
// Green
json["tracking_to_eye_transform"][1]["distortion"]["coeffs"][0].SetDouble(NLT_Coeffecients[1][0][0]);
json["tracking_to_eye_transform"][1]["distortion"]["coeffs"][1].SetDouble(NLT_Coeffecients[1][0][1]);
json["tracking_to_eye_transform"][1]["distortion"]["coeffs"][2].SetDouble(NLT_Coeffecients[1][0][2]);
json["tracking_to_eye_transform"][1]["distortion"]["center_x"].SetDouble(Intrinsics[1][0][2]);
json["tracking_to_eye_transform"][1]["distortion"]["center_y"].SetDouble(Intrinsics[1][1][2]);
// Blue
json["tracking_to_eye_transform"][1]["distortion_blue"]["coeffs"][0].SetDouble(NLT_Coeffecients[1][1][0]);
json["tracking_to_eye_transform"][1]["distortion_blue"]["coeffs"][1].SetDouble(NLT_Coeffecients[1][1][1]);
json["tracking_to_eye_transform"][1]["distortion_blue"]["coeffs"][2].SetDouble(NLT_Coeffecients[1][1][2]);
json["tracking_to_eye_transform"][1]["distortion_blue"]["center_x"].SetDouble(Intrinsics[1][0][2]);
json["tracking_to_eye_transform"][1]["distortion_blue"]["center_y"].SetDouble(Intrinsics[1][1][2]);
// Red
json["tracking_to_eye_transform"][1]["distortion_red"]["coeffs"][0].SetDouble(NLT_Coeffecients[1][2][0]);
json["tracking_to_eye_transform"][1]["distortion_red"]["coeffs"][1].SetDouble(NLT_Coeffecients[1][2][1]);
json["tracking_to_eye_transform"][1]["distortion_red"]["coeffs"][2].SetDouble(NLT_Coeffecients[1][2][2]);
json["tracking_to_eye_transform"][1]["distortion_red"]["center_x"].SetDouble(Intrinsics[1][0][2]);
json["tracking_to_eye_transform"][1]["distortion_red"]["center_y"].SetDouble(Intrinsics[1][1][2]);
QFile file(filename);
file.open(QIODevice::WriteOnly | QIODevice::Text);
rapidjson::StringBuffer buffer;
buffer.Clear();
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
json.Accept(writer);
file.write(buffer.GetString());
file.close();
return true;
}
bool OpenGL_Widget::loadConfigFromJson(QString filename)
{
// Todo: Add some basic error checking/sanity checking
QString val;
QFile file("HMD_Config.json");
if (!file.exists())
return false;
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
using namespace rapidjson;
json.Parse(val.toStdString().c_str());
// Todo make sure all the various parts of the JSON file actually exist....
// Left Eye
Centers[0][0] = json["tracking_to_eye_transform"][0]["distortion"]["center_x"].GetDouble();
Centers[0][1] = json["tracking_to_eye_transform"][0]["distortion"]["center_y"].GetDouble();
Intrinsics[0][0][0] = json["tracking_to_eye_transform"][0]["intrinsics"][0][0].GetDouble();
Intrinsics[0][0][1] = json["tracking_to_eye_transform"][0]["intrinsics"][0][1].GetDouble();
Intrinsics[0][0][2] = json["tracking_to_eye_transform"][0]["intrinsics"][0][2].GetDouble();
Intrinsics[0][1][0] = json["tracking_to_eye_transform"][0]["intrinsics"][1][0].GetDouble();
Intrinsics[0][1][1] = json["tracking_to_eye_transform"][0]["intrinsics"][1][1].GetDouble();
Intrinsics[0][1][2] = json["tracking_to_eye_transform"][0]["intrinsics"][1][2].GetDouble();
Intrinsics[0][2][0] = json["tracking_to_eye_transform"][0]["intrinsics"][2][0].GetDouble();
Intrinsics[0][2][1] = json["tracking_to_eye_transform"][0]["intrinsics"][2][1].GetDouble();
Intrinsics[0][2][2] = json["tracking_to_eye_transform"][0]["intrinsics"][2][2].GetDouble();
// Green
NLT_Coeffecients[0][0][0] = json["tracking_to_eye_transform"][0]["distortion"]["coeffs"][0].GetDouble();
NLT_Coeffecients[0][0][1] = json["tracking_to_eye_transform"][0]["distortion"]["coeffs"][1].GetDouble();
NLT_Coeffecients[0][0][2] = json["tracking_to_eye_transform"][0]["distortion"]["coeffs"][2].GetDouble();
// Blue
NLT_Coeffecients[0][1][0] = json["tracking_to_eye_transform"][0]["distortion_blue"]["coeffs"][0].GetDouble();
NLT_Coeffecients[0][1][1] = json["tracking_to_eye_transform"][0]["distortion_blue"]["coeffs"][1].GetDouble();
NLT_Coeffecients[0][1][2] = json["tracking_to_eye_transform"][0]["distortion_blue"]["coeffs"][2].GetDouble();
// Red
NLT_Coeffecients[0][2][0] = json["tracking_to_eye_transform"][0]["distortion_red"]["coeffs"][0].GetDouble();
NLT_Coeffecients[0][2][1] = json["tracking_to_eye_transform"][0]["distortion_red"]["coeffs"][1].GetDouble();
NLT_Coeffecients[0][2][2] = json["tracking_to_eye_transform"][0]["distortion_red"]["coeffs"][2].GetDouble();
// Right Eye
Centers[1][0] = json["tracking_to_eye_transform"][1]["distortion"]["center_x"].GetDouble();
Centers[1][1] = json["tracking_to_eye_transform"][1]["distortion"]["center_y"].GetDouble();
Intrinsics[1][0][0] = json["tracking_to_eye_transform"][1]["intrinsics"][0][0].GetDouble();
Intrinsics[1][0][1] = json["tracking_to_eye_transform"][1]["intrinsics"][0][1].GetDouble();
Intrinsics[1][0][2] = json["tracking_to_eye_transform"][1]["intrinsics"][0][2].GetDouble();
Intrinsics[1][1][0] = json["tracking_to_eye_transform"][1]["intrinsics"][1][0].GetDouble();
Intrinsics[1][1][1] = json["tracking_to_eye_transform"][1]["intrinsics"][1][1].GetDouble();
Intrinsics[1][1][2] = json["tracking_to_eye_transform"][1]["intrinsics"][1][2].GetDouble();
Intrinsics[1][2][0] = json["tracking_to_eye_transform"][1]["intrinsics"][2][0].GetDouble();
Intrinsics[1][2][1] = json["tracking_to_eye_transform"][1]["intrinsics"][2][1].GetDouble();
Intrinsics[1][2][2] = json["tracking_to_eye_transform"][1]["intrinsics"][2][2].GetDouble();