-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFionaUTRender.cpp
More file actions
2418 lines (2119 loc) · 84.5 KB
/
FionaUTRender.cpp
File metadata and controls
2418 lines (2119 loc) · 84.5 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
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
//
// FionaUTRender.cpp
// FionaUT
//
// Created by Hyun Joon Shin on 5/8/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#define BIG_OPENGL_OPT
#include "FionaUT.h"
#ifndef LINUX_BUILD
#include "GL/wglew.h" //for nvcopyimage
#endif
#include <Kit3D/jtrans.h>
#include "glm/common.hpp"
#include "glm/gtx/matrix_interpolation.hpp"
#include "glm/gtc/quaternion.hpp"
int fionaRenderCycleCount = 0;
bool fionaRenderCycleCenter = false;
bool fionaRenderCycleLeft = false;
bool fionaRenderCycleRight= false;
const FionaViewport VP_FBO(0,0,1,1,0,0,1,1);
static GLuint extFBOTexture = 0;
static bool isInFBO = false;
static bool isUsingExtFBO = false;
//#define NEW_DISTORTION
#ifdef ENABLE_OCULUS
#include <Kit3D/glslUtils.h>
static GLuint m_quadVertexBuffer = 0;
static GLuint m_quadLeftUVBuffer = 0;
static GLuint m_quadRightUVBuffer = 0;
static GLuint correctionProgram=0;
static GLuint uniformLensCenter=0;
static GLuint uniformScreenCenter=0;
static GLuint uniformScale=0;
static GLuint uniformScaleIn=0;
static GLuint uniformHMDWarpParam=0;
static GLuint uniformChromAbParam=0;
static GLuint uniformTexture0=0;
#ifdef ENABLE_DK2
static GLuint uniformScreenCenterOffset = 0;
static GLuint uniformDistortionClear = 0;
static GLuint uniformTanEyeAngleScale = 0;
static GLuint uniformTanEyeAngleOffset = 0;
static GLuint uniformView = 0;
static GLuint uniformTexM = 0;
#endif
/*static const char* PostProcessVertexShaderSrc =
"#version 110\n"
"uniform mat4 View;\n"
"uniform mat4 Texm;\n"
"attribute vec4 Position;\n"
"attribute vec2 TexCoord;\n"
"varying vec2 oTexCoord;\n"
"void main()\n"
"{\n"
" gl_Position = View * Position;\n"
" oTexCoord = vec2(Texm * vec4(TexCoord,0,1));\n"
"}\n";
#else*/
static const char* PostProcessVertexShaderSrc =
"attribute vec3 Position;\n"
"attribute vec2 TexCoord;\n"
"varying vec2 oTexCoord;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(Position, 1.0);\n"
" oTexCoord = TexCoord;\n"
"}\n";
static const char* PostProcessFragShaderSrc =
"uniform vec2 LensCenter;\n"
"uniform vec2 ScreenCenter;\n"
"uniform vec2 Scale;\n"
"uniform vec2 ScaleIn;\n"
"uniform vec4 HmdWarpParam;\n"
"uniform sampler2D Texture0;\n"
"varying vec2 oTexCoord;\n"
"\n"
"vec2 HmdWarp(vec2 in01)\n"
"{\n"
" vec2 theta = (in01 - LensCenter) * ScaleIn;\n" // Scales to [-1, 1]
" float rSq = theta.x * theta.x + theta.y * theta.y;\n"
" vec2 theta1 = theta * (HmdWarpParam.x + HmdWarpParam.y * rSq + HmdWarpParam.z * rSq * rSq + HmdWarpParam.w * rSq * rSq * rSq);\n"
" return LensCenter + (Scale * theta1);\n"
"}\n"
"void main()\n"
"{\n"
" vec2 tc = HmdWarp(oTexCoord);\n"
" if (!all(equal(clamp(tc, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25,0.5)), tc)))\n"
" gl_FragColor = vec4(0);\n"
" else\n"
" gl_FragColor = texture2D(Texture0, tc);\n"
"}\n";
static const char* PostProcessFragTestShaderSrc =
"uniform sampler2D Texture0;\n"
"varying vec2 oTexCoord;\n"
"\n"
"void main()\n"
"{\n"
" gl_FragColor = texture2D(Texture0, oTexCoord);\n"
"}\n";
#ifdef ENABLE_DK2
// Shader with lens distortion and chromatic aberration correction.
#ifdef NEW_DISTORTION
static const char* PostProcessFullFragShaderSrc =
"#version 110\n"
"uniform sampler2D Texture;\n"
"uniform vec3 DistortionClearColor;\n"
"uniform float EdgeFadeScale;\n"
"uniform vec2 EyeToSourceUVScale;\n"
"uniform vec2 EyeToSourceUVOffset;\n"
"uniform vec2 EyeToSourceNDCScale;\n"
"uniform vec2 EyeToSourceNDCOffset;\n"
"uniform vec2 TanEyeAngleScale;\n"
"uniform vec2 TanEyeAngleOffset;\n"
"uniform vec4 HmdWarpParam;\n"
"uniform vec4 ChromAbParam;\n"
"varying vec4 oPosition;\n"
"varying vec2 oTexCoord;\n"
"void main()\n"
"{\n"
// Input oTexCoord is [-1,1] across the half of the screen used for a single eye.
" vec2 TanEyeAngleDistorted = oTexCoord * TanEyeAngleScale + TanEyeAngleOffset;\n" // Scales to tan(thetaX),tan(thetaY), but still distorted (i.e. only the center is correct)
" float RadiusSq = TanEyeAngleDistorted.x * TanEyeAngleDistorted.x + TanEyeAngleDistorted.y * TanEyeAngleDistorted.y;\n"
" float Distort = 1.0 / ( 1.0 + RadiusSq * ( HmdWarpParam.y + RadiusSq * ( HmdWarpParam.z + RadiusSq * ( HmdWarpParam.w ) ) ) );\n"
" float DistortR = Distort * ( ChromAbParam.x + RadiusSq * ChromAbParam.y );\n"
" float DistortG = Distort;\n"
" float DistortB = Distort * ( ChromAbParam.z + RadiusSq * ChromAbParam.w );\n"
" vec2 TanEyeAngleR = DistortR * TanEyeAngleDistorted;\n"
" vec2 TanEyeAngleG = DistortG * TanEyeAngleDistorted;\n"
" vec2 TanEyeAngleB = DistortB * TanEyeAngleDistorted;\n"
// These are now in "TanEyeAngle" space.
// The vectors (TanEyeAngleRGB.x, TanEyeAngleRGB.y, 1.0) are real-world vectors pointing from the eye to where the components of the pixel appear to be.
// If you had a raytracer, you could just use them directly.
// Scale them into ([0,0.5],[0,1]) or ([0.5,0],[0,1]) UV lookup space (depending on eye)
" vec2 SourceCoordR = TanEyeAngleR * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
" SourceCoordR.y = 1.0 - SourceCoordR.y;\n"
" vec2 SourceCoordG = TanEyeAngleG * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
" SourceCoordG.y = 1.0 - SourceCoordG.y;\n"
" vec2 SourceCoordB = TanEyeAngleB * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
" SourceCoordB.y = 1.0 - SourceCoordB.y;\n"
// Find the distance to the nearest edge.
" vec2 NDCCoord = TanEyeAngleG * EyeToSourceNDCScale + EyeToSourceNDCOffset;\n"
" float EdgeFadeIn = clamp ( EdgeFadeScale, 0.0, 1e5 ) * ( 1.0 - max ( abs ( NDCCoord.x ), abs ( NDCCoord.y ) ) );\n"
" if ( EdgeFadeIn < 0.0 )\n"
" {\n"
" gl_FragColor = vec4(DistortionClearColor.r, DistortionClearColor.g, DistortionClearColor.b, 1.0);\n"
" return;\n"
" }\n"
" EdgeFadeIn = clamp ( EdgeFadeIn, 0.0, 1.0 );\n"
// Actually do the lookups.
" float ResultR = texture2D(Texture, SourceCoordR).r;\n"
" float ResultG = texture2D(Texture, SourceCoordG).g;\n"
" float ResultB = texture2D(Texture, SourceCoordB).b;\n"
" gl_FragColor = vec4(ResultR * EdgeFadeIn, ResultG * EdgeFadeIn, ResultB * EdgeFadeIn, 1.0);\n"
"}\n";
#else
// Shader with lens distortion and chromatic aberration correction.
static const char* PostProcessFullFragShaderSrc =
"uniform vec2 LensCenter;\n"
"uniform vec2 ScreenCenter;\n"
"uniform vec2 Scale;\n"
"uniform vec2 ScaleIn;\n"
"uniform vec4 HmdWarpParam;\n"
"uniform vec4 ChromAbParam;\n"
"uniform sampler2D Texture0;\n"
"varying vec2 oTexCoord;\n"
"\n"
// Scales input texture coordinates for distortion.
// ScaleIn maps texture coordinates to Scales to ([-1, 1]), although top/bottom will be
// larger due to aspect ratio.
"void main()\n"
"{\n"
" vec2 theta = (oTexCoord - LensCenter) * ScaleIn;\n" // Scales to [-1, 1]
" float rSq= theta.x * theta.x + theta.y * theta.y;\n"
" vec2 theta1 = theta * (HmdWarpParam.x + HmdWarpParam.y * rSq + "
" HmdWarpParam.z * rSq * rSq + HmdWarpParam.w * rSq * rSq * rSq);\n"
" \n"
" // Detect whether blue texture coordinates are out of range since these will scaled out the furthest.\n"
" vec2 thetaBlue = theta1 * (ChromAbParam.z + ChromAbParam.w * rSq);\n"
" vec2 tcBlue = LensCenter + Scale * thetaBlue;\n"
" if (!all(equal(clamp(tcBlue, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25,0.5)), tcBlue)))\n"
" {\n"
" gl_FragColor = vec4(0);\n"
" return;\n"
" }\n"
" \n"
" // Now do blue texture lookup.\n"
" float blue = texture2D(Texture0, tcBlue).b;\n"
" \n"
" // Do green lookup (no scaling).\n"
" vec2 tcGreen = LensCenter + Scale * theta1;\n"
" vec4 center = texture2D(Texture0, tcGreen);\n"
" \n"
" // Do red scale and lookup.\n"
" vec2 thetaRed = theta1 * (ChromAbParam.x + ChromAbParam.y * rSq);\n"
" vec2 tcRed = LensCenter + Scale * thetaRed;\n"
" float red = texture2D(Texture0, tcRed).r;\n"
" \n"
" gl_FragColor = vec4(red, center.g, blue, center.a);\n"
"}\n";
#endif
#endif
#ifdef ENABLE_CV1
// Shader with lens distortion and chromatic aberration correction.
#ifdef NEW_DISTORTION
static const char* PostProcessFullFragShaderSrc =
"#version 110\n"
"uniform sampler2D Texture;\n"
"uniform vec3 DistortionClearColor;\n"
"uniform float EdgeFadeScale;\n"
"uniform vec2 EyeToSourceUVScale;\n"
"uniform vec2 EyeToSourceUVOffset;\n"
"uniform vec2 EyeToSourceNDCScale;\n"
"uniform vec2 EyeToSourceNDCOffset;\n"
"uniform vec2 TanEyeAngleScale;\n"
"uniform vec2 TanEyeAngleOffset;\n"
"uniform vec4 HmdWarpParam;\n"
"uniform vec4 ChromAbParam;\n"
"varying vec4 oPosition;\n"
"varying vec2 oTexCoord;\n"
"void main()\n"
"{\n"
// Input oTexCoord is [-1,1] across the half of the screen used for a single eye.
" vec2 TanEyeAngleDistorted = oTexCoord * TanEyeAngleScale + TanEyeAngleOffset;\n" // Scales to tan(thetaX),tan(thetaY), but still distorted (i.e. only the center is correct)
" float RadiusSq = TanEyeAngleDistorted.x * TanEyeAngleDistorted.x + TanEyeAngleDistorted.y * TanEyeAngleDistorted.y;\n"
" float Distort = 1.0 / ( 1.0 + RadiusSq * ( HmdWarpParam.y + RadiusSq * ( HmdWarpParam.z + RadiusSq * ( HmdWarpParam.w ) ) ) );\n"
" float DistortR = Distort * ( ChromAbParam.x + RadiusSq * ChromAbParam.y );\n"
" float DistortG = Distort;\n"
" float DistortB = Distort * ( ChromAbParam.z + RadiusSq * ChromAbParam.w );\n"
" vec2 TanEyeAngleR = DistortR * TanEyeAngleDistorted;\n"
" vec2 TanEyeAngleG = DistortG * TanEyeAngleDistorted;\n"
" vec2 TanEyeAngleB = DistortB * TanEyeAngleDistorted;\n"
// These are now in "TanEyeAngle" space.
// The vectors (TanEyeAngleRGB.x, TanEyeAngleRGB.y, 1.0) are real-world vectors pointing from the eye to where the components of the pixel appear to be.
// If you had a raytracer, you could just use them directly.
// Scale them into ([0,0.5],[0,1]) or ([0.5,0],[0,1]) UV lookup space (depending on eye)
" vec2 SourceCoordR = TanEyeAngleR * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
" SourceCoordR.y = 1.0 - SourceCoordR.y;\n"
" vec2 SourceCoordG = TanEyeAngleG * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
" SourceCoordG.y = 1.0 - SourceCoordG.y;\n"
" vec2 SourceCoordB = TanEyeAngleB * EyeToSourceUVScale + EyeToSourceUVOffset;\n"
" SourceCoordB.y = 1.0 - SourceCoordB.y;\n"
// Find the distance to the nearest edge.
" vec2 NDCCoord = TanEyeAngleG * EyeToSourceNDCScale + EyeToSourceNDCOffset;\n"
" float EdgeFadeIn = clamp ( EdgeFadeScale, 0.0, 1e5 ) * ( 1.0 - max ( abs ( NDCCoord.x ), abs ( NDCCoord.y ) ) );\n"
" if ( EdgeFadeIn < 0.0 )\n"
" {\n"
" gl_FragColor = vec4(DistortionClearColor.r, DistortionClearColor.g, DistortionClearColor.b, 1.0);\n"
" return;\n"
" }\n"
" EdgeFadeIn = clamp ( EdgeFadeIn, 0.0, 1.0 );\n"
// Actually do the lookups.
" float ResultR = texture2D(Texture, SourceCoordR).r;\n"
" float ResultG = texture2D(Texture, SourceCoordG).g;\n"
" float ResultB = texture2D(Texture, SourceCoordB).b;\n"
" gl_FragColor = vec4(ResultR * EdgeFadeIn, ResultG * EdgeFadeIn, ResultB * EdgeFadeIn, 1.0);\n"
"}\n";
#else
// Shader with lens distortion and chromatic aberration correction.
static const char* PostProcessFullFragShaderSrc =
"uniform vec2 LensCenter;\n"
"uniform vec2 ScreenCenter;\n"
"uniform vec2 Scale;\n"
"uniform vec2 ScaleIn;\n"
"uniform vec4 HmdWarpParam;\n"
"uniform vec4 ChromAbParam;\n"
"uniform sampler2D Texture0;\n"
"varying vec2 oTexCoord;\n"
"\n"
// Scales input texture coordinates for distortion.
// ScaleIn maps texture coordinates to Scales to ([-1, 1]), although top/bottom will be
// larger due to aspect ratio.
"void main()\n"
"{\n"
" vec2 theta = (oTexCoord - LensCenter) * ScaleIn;\n" // Scales to [-1, 1]
" float rSq= theta.x * theta.x + theta.y * theta.y;\n"
" vec2 theta1 = theta * (HmdWarpParam.x + HmdWarpParam.y * rSq + "
" HmdWarpParam.z * rSq * rSq + HmdWarpParam.w * rSq * rSq * rSq);\n"
" \n"
" // Detect whether blue texture coordinates are out of range since these will scaled out the furthest.\n"
" vec2 thetaBlue = theta1 * (ChromAbParam.z + ChromAbParam.w * rSq);\n"
" vec2 tcBlue = LensCenter + Scale * thetaBlue;\n"
" if (!all(equal(clamp(tcBlue, ScreenCenter-vec2(0.25,0.5), ScreenCenter+vec2(0.25,0.5)), tcBlue)))\n"
" {\n"
" gl_FragColor = vec4(0);\n"
" return;\n"
" }\n"
" \n"
" // Now do blue texture lookup.\n"
" float blue = texture2D(Texture0, tcBlue).b;\n"
" \n"
" // Do green lookup (no scaling).\n"
" vec2 tcGreen = LensCenter + Scale * theta1;\n"
" vec4 center = texture2D(Texture0, tcGreen);\n"
" \n"
" // Do red scale and lookup.\n"
" vec2 thetaRed = theta1 * (ChromAbParam.x + ChromAbParam.y * rSq);\n"
" vec2 tcRed = LensCenter + Scale * thetaRed;\n"
" float red = texture2D(Texture0, tcRed).r;\n"
" \n"
" gl_FragColor = vec4(red, center.g, blue, center.a);\n"
"}\n";
#endif
#endif
static void createOculusCorrection(void)
{
#ifdef ENABLE_DK2
correctionProgram = loadProgram(std::string(PostProcessVertexShaderSrc), std::string(PostProcessFullFragShaderSrc), false);
#ifdef NEW_DISTORTION
uniformLensCenter=glGetUniformLocation(correctionProgram, "EdgeFadeScale");
uniformScreenCenter=glGetUniformLocation(correctionProgram, "EyeToSourceNDCScale");
uniformScreenCenterOffset=glGetUniformLocation(correctionProgram, "EyeToSourceNDCOffset");
uniformScale=glGetUniformLocation(correctionProgram, "EyeToSourceUVScale");
uniformScaleIn=glGetUniformLocation(correctionProgram, "EyeToSourceUVOffset");
uniformHMDWarpParam=glGetUniformLocation(correctionProgram, "HmdWarpParam");
uniformChromAbParam=glGetUniformLocation(correctionProgram, "ChromAbParam");
uniformTexture0=glGetUniformLocation(correctionProgram, "Texture");
uniformDistortionClear = glGetUniformLocation(correctionProgram, "DistortionClearColor");
uniformTanEyeAngleScale = glGetUniformLocation(correctionProgram, "TanEyeAngleScale");
uniformTanEyeAngleOffset = glGetUniformLocation(correctionProgram, "TanEyeAngleOffset");
uniformView = glGetUniformLocation(correctionProgram, "View");
uniformTexM = glGetUniformLocation(correctionProgram, "Texm");
#else
uniformLensCenter=glGetUniformLocation(correctionProgram, "LensCenter");
uniformScreenCenter=glGetUniformLocation(correctionProgram, "ScreenCenter");
uniformScale=glGetUniformLocation(correctionProgram, "Scale");
uniformScaleIn=glGetUniformLocation(correctionProgram, "ScaleIn");
uniformHMDWarpParam=glGetUniformLocation(correctionProgram, "HmdWarpParam");
uniformChromAbParam=glGetUniformLocation(correctionProgram, "ChromAbParam");
uniformTexture0=glGetUniformLocation(correctionProgram, "Texture0");
#endif
#endif
#ifdef ENABLE_CV1
correctionProgram = loadProgram(std::string(PostProcessVertexShaderSrc), std::string(PostProcessFullFragShaderSrc), false);
#ifdef NEW_DISTORTION
uniformLensCenter = glGetUniformLocation(correctionProgram, "EdgeFadeScale");
uniformScreenCenter = glGetUniformLocation(correctionProgram, "EyeToSourceNDCScale");
uniformScreenCenterOffset = glGetUniformLocation(correctionProgram, "EyeToSourceNDCOffset");
uniformScale = glGetUniformLocation(correctionProgram, "EyeToSourceUVScale");
uniformScaleIn = glGetUniformLocation(correctionProgram, "EyeToSourceUVOffset");
uniformHMDWarpParam = glGetUniformLocation(correctionProgram, "HmdWarpParam");
uniformChromAbParam = glGetUniformLocation(correctionProgram, "ChromAbParam");
uniformTexture0 = glGetUniformLocation(correctionProgram, "Texture");
uniformDistortionClear = glGetUniformLocation(correctionProgram, "DistortionClearColor");
uniformTanEyeAngleScale = glGetUniformLocation(correctionProgram, "TanEyeAngleScale");
uniformTanEyeAngleOffset = glGetUniformLocation(correctionProgram, "TanEyeAngleOffset");
uniformView = glGetUniformLocation(correctionProgram, "View");
uniformTexM = glGetUniformLocation(correctionProgram, "Texm");
#else
uniformLensCenter = glGetUniformLocation(correctionProgram, "LensCenter");
uniformScreenCenter = glGetUniformLocation(correctionProgram, "ScreenCenter");
uniformScale = glGetUniformLocation(correctionProgram, "Scale");
uniformScaleIn = glGetUniformLocation(correctionProgram, "ScaleIn");
uniformHMDWarpParam = glGetUniformLocation(correctionProgram, "HmdWarpParam");
uniformChromAbParam = glGetUniformLocation(correctionProgram, "ChromAbParam");
uniformTexture0 = glGetUniformLocation(correctionProgram, "Texture0");
#endif
#endif
}
#endif
// Drawing rectangle with textured
// The first four parameters are for the area to cover (-1 to 1)
// The last four parameters are area of the source image (0 to 1)
static void drawRect(float l, float r, float b, float t,
float tl, float tr, float tb, float tt)
{
#ifndef ENABLE_OCULUS
glBegin(GL_QUADS);
glTexCoord2f(tl, tb); glVertex2f(l, b);
glTexCoord2f(tr, tb); glVertex2f(r, b);
glTexCoord2f(tr, tt); glVertex2f(r, t);
glTexCoord2f(tl, tt); glVertex2f(l, t);
glEnd();
#else
static const GLfloat g_quad_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f
};
static const GLfloat q_tex_buffer_data_left[] = {
0.f, 0.f,
0.5f, 0.f,
0.f, 1.f,
0.f, 1.f,
0.5f, 0.f,
0.5f, 1.f };
static const GLfloat q_tex_buffer_data_right[] = {
0.5f, 0.f,
1.f, 0.f,
0.5f, 1.f,
0.5f, 1.f,
1.f, 0.f,
1.f, 1.f };
if(m_quadVertexBuffer == 0)
{
glGenBuffers(1, &m_quadVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_quadVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);
}
if(m_quadLeftUVBuffer == 0)
{
glGenBuffers(1, &m_quadLeftUVBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_quadLeftUVBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(q_tex_buffer_data_left), q_tex_buffer_data_left, GL_STATIC_DRAW);
}
if(m_quadRightUVBuffer == 0)
{
glGenBuffers(1, &m_quadRightUVBuffer);
glBindBuffer(GL_ARRAY_BUFFER, m_quadRightUVBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(q_tex_buffer_data_left), q_tex_buffer_data_right, GL_STATIC_DRAW);
}
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_quadVertexBuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
if(fionaRenderCycleLeft)
{
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, m_quadLeftUVBuffer);
glVertexAttribPointer(
1, // attribute 0. No particular reason for 0, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
}
else if(fionaRenderCycleRight)
{
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, m_quadRightUVBuffer);
glVertexAttribPointer(
1, // attribute 0. No particular reason for 0, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
}
// Draw the triangles !
glDrawArrays(GL_TRIANGLES, 0, 6); // 2*3 indices starting at 0 -> 2 triangles
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
#endif
}
// Simple helper function to create FBO and corresponding texture
static bool createFBO(int w, int h)
{
#ifdef LINUX_BUILD
glewInit();
#endif
printf("Creating FBO of size %dx%d\n", w, h);
glGenFramebuffers(1, &fionaConf.fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fionaConf.fbo);
glGenTextures(1, &fionaConf.img);
glBindTexture(GL_TEXTURE_2D, fionaConf.img);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
//glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glGenTextures(1, &fionaConf.depth);
glBindTexture(GL_TEXTURE_2D, fionaConf.depth);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, w, h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, w, h, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fionaConf.img, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, fionaConf.depth, 0);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
if( status != GL_FRAMEBUFFER_COMPLETE )
{
printf("FBO: Something wrong..\n");
return false;
}
printf("MADE FRAMEBUFFER OBJECT!\n");
//glEnable(GL_MULTISAMPLE);
return true;
}
tran caveProjectionWall(const FionaWall &wall, const jvec3& e)
{
//this version doesn't include the eye position within the projection matrix.
jvec3 vr = (wall.vLB - wall.vRB).normalize();
jvec3 vu = (wall.vLB - wall.vLT).normalize();
jvec3 vn = (vr * vu).normalize();
//compute screen corner vectors
jvec3 va = wall.vLB - e;
jvec3 vb = wall.vRB - e;
jvec3 vc = wall.vLT - e;
//find the distance from the eye to screen plane
float n = fionaConf.nearClip;
float f = fionaConf.farClip;
float d = va % vn;
float nod = n / d;
float l = (vr % va) * nod;
float r = (vr % vb) * nod;
float b = (vu % va) * nod;
float t = (vu % vc) * nod;
return tran(2.0f * n / (r - l), 0.f, (r + l) / (r - l), 0.f,
0.f, 2.0f * n / (t - b), (t + b) / (t - b), 0.f,
0.f, 0.f, -(f + n) / (f - n), (-2.0f * f * n) / (f - n),
0.f, 0.f, -1.f, 0.f);
}
tran caveProjection(const jvec3& sz, const jvec3& e)
{
//this version does include the eye position within the projection matrix and a lot of hyun joon's crazy math.
float f=fionaConf.farClip, n=fionaConf.nearClip; // far and near clip depth
float w=ABS(sz.x/2), h=ABS(sz.y/2), d=ABS(sz.z/2);
/*printf("*********************\n%2f, %2f, %2f, %2f\n%2f, %2f, %2f, %2f\n, %2f, %2f, %2f, %2f\n, %2f, %2f, %2f, %2f\n",
d/w+e.z/w, 0.f, -e.x/w, -e.x*d/w,
0.f, d/h+e.z/h, -e.y/h, -e.y*d/h,
0.f, 0.f, -1.f*(-f+e.z+n)/(-f+e.z-n),(2.f*n*f+e.z*(-f-n)+e.z*e.z)/(-f+e.z-n),
0.f, 0.f, -1.f, e.z);*/
// Cave projection matrix
//printf("%f\n", d / w);
return tran(d/w+e.z/w, 0.f, -e.x/w, -e.x*d/w,
0.f, d/h+e.z/h, -e.y/h, -e.y*d/h,
0.f, 0.f, -1.f*(-f+e.z+n)/(-f+e.z-n), (2.f*n*f+e.z*(-f-n)+e.z*e.z)/(-f+e.z-n),
0.f, 0.f, -1.f, e.z);
}
tran projectorCalibration(const FionaViewport& vp)
{
return tran(vp.cScaleX,0,0,vp.cOffsetX,0,vp.cScaleY,0,vp.cOffsetY,0,0,1,0,0,0,0,1);
}
void caveProjectionGLM(const glm::vec3& sz, const glm::vec3& e, glm::mat4 &caveMat)
{
float f=fionaConf.farClip, n=fionaConf.nearClip; // far and near clip depth
float w=ABS(sz.x/2), h=ABS(sz.y/2), d=ABS(sz.z/2);
// Cave projection matrix
caveMat[0][0]=d/w+e.z/w;
caveMat[0][1]=0.f;
caveMat[0][2]=-e.x/w;
caveMat[0][3]=-e.x*d/w;
caveMat[1][0]=0.f;
caveMat[1][1]=d/h+e.z/h;
caveMat[1][2]=-e.y/h;
caveMat[1][3]=-e.y*d/h;
caveMat[2][0]=0.f;
caveMat[2][1]=0.f;
caveMat[2][2]=-1.f*(-f+e.z+n)/(-f+e.z-n);
caveMat[2][3]=(2.f*n*f+e.z*(-f-n)+e.z*e.z)/(-f+e.z-n);
caveMat[3][0]=0.f;
caveMat[3][1]=0.f;
caveMat[3][2]=-1.f;
caveMat[3][3]=e.z;
}
void projectionCalibrationGLM(const FionaViewport &vp, glm::mat4 &pcMat)
{
pcMat[0][0] = vp.cScaleX;
pcMat[0][1] = 0.f;
pcMat[0][2] = 0.f;
pcMat[0][3] = vp.cOffsetX;
pcMat[1][0] = 0.f;
pcMat[1][1] = vp.cScaleY;
pcMat[1][2] = 0.f;
pcMat[1][3] = vp.cOffsetY;
pcMat[2][0] = 0.f;
pcMat[2][1] = 0.f;
pcMat[2][2] = 1.f;
pcMat[2][3] = 0.f;
pcMat[3][0] = 0.f;
pcMat[3][1] = 0.f;
pcMat[3][2] = 0.f;
pcMat[3][3] = 1.f;
}
void viewportProjectionGLM(const FionaViewport& vp, glm::mat4 &vpMat)
{
float x0=vp.sx*2-1, w=vp.sw*2;
float y0=vp.sy*2-1, h=vp.sh*2;
vpMat[0][0] = 2.f/w;
vpMat[0][1] = 0.f;
vpMat[0][2] = 0.f;
vpMat[0][3] = -2.f/w*x0-1.f;
vpMat[1][0] = 0.f;
vpMat[1][1] = 2.f/h;
vpMat[1][2] = 0.f;
vpMat[1][3] = -2.f/h*y0-1.f;
vpMat[2][0] = 0.f;
vpMat[2][1] = 0.f;
vpMat[2][2] = 1.f;
vpMat[2][3] = 0.f;
vpMat[3][0] = 0.f;
vpMat[3][1] = 0.f;
vpMat[3][2] = 0.f;
vpMat[3][3] = 1.f;
}
tran viewportProjection(const FionaViewport& vp)
{
float x0=vp.sx*2-1, w=vp.sw*2;
float y0=vp.sy*2-1, h=vp.sh*2;
return tran(2/w,0,0,-2/w*x0-1,
0,2/h,0,-2/h*y0-1,
0,0,1,0,0,0,0,1);
}
jvec3 _FionaUTCalcEyePosition(int eye, const FionaWall &wall)
{
jvec3 head = fionaConf.headPos;
jvec3 eyePos;
if (eye == 0)
{
eyePos = fionaConf.kevinOffset;
}
else if (eye == 1)
{
eyePos = fionaConf.kevinOffset + fionaConf.lEyeOffset;
}
else if (eye == 2)
{
eyePos = fionaConf.kevinOffset + fionaConf.rEyeOffset;
}
//wall preRot is calculated in FionaWall constructor
quat preRot = wall.preRot;
//same with wall cntr
//head is now in coordinate space relative to wall center
head = preRot.rot(head)-wall.cntr;
eyePos = preRot.rot(fionaConf.headRot.rot(eyePos)) + head;
return eyePos;
}
#ifdef ENABLE_VIVE
void ComposeProjection2(float fLeft, float fRight, float fTop, float fBottom, float zNear, float zFar, vr::HmdMatrix44_t *pmProj)
{
/*float projXScale = 2.0f / (fLeft + fRight);
float projXOffset = (fLeft - fRight) * projXScale * 0.5f;
float projYScale = 2.0f / (fTop + fBottom);
float projYOffset = (fTop - fBottom) * projYScale * 0.5f;*/
//float idx = 1.0f / (fRight - fLeft);
//float idy = 1.0f / (fBottom - fTop);
//float idz = 1.0f / (zFar - zNear);
/*float sx = fLeft - fRight;
float sy = fTop - fBottom;
float projXScale = 2.f / (fLeft + fRight);
float projYScale = 2.f / (fBottom + fTop);*/
/*float(*p)[4] = pmProj->m;
p[0][0] = projXScale; p[0][1] = 0; p[0][2] = -1.f * projXOffset; p[0][3] = 0;
p[1][0] = 0; p[1][1] = projYScale; p[1][2] = -1.f * -projYOffset; p[1][3] = 0;
p[2][0] = 0; p[2][1] = 0; p[2][2] = (zNear+zFar)/(zNear-zFar); p[2][3] = 2.f*zFar*zNear/(zNear-zFar);
p[3][0] = 0; p[3][1] = 0; p[3][2] = -1.0f; p[3][3] = 0;*/
float idx = 1.0f / (fRight - fLeft);
float idy = -(1.0f / (fBottom - fTop));
float idz = 1.0f / (zFar - zNear);
float sx = fRight + fLeft;
float sy = fBottom + fTop;
float(*p)[4] = pmProj->m;
p[0][0] = 2 * idx; p[0][1] = 0; p[0][2] = sx*idx; p[0][3] = 0;
p[1][0] = 0; p[1][1] = 2 * idy; p[1][2] = sy*idy; p[1][3] = 0;
p[2][0] = 0; p[2][1] = 0; p[2][2] = -zFar*idz; p[2][3] = -zFar*zNear*idz;
p[3][0] = 0; p[3][1] = 0; p[3][2] = -1.0f; p[3][3] = 0;
/*float(*p)[4] = pmProj->m;
p[0][0] = zNear/fRight; p[0][1] = 0; p[0][2] = 0; p[0][3] = 0;
p[1][0] = 0; p[1][1] = zNear/fTop; p[1][2] = 0; p[1][3] = 0;
p[2][0] = 0; p[2][1] = 0; p[2][2] = -(zFar+zNear)/(zFar-zNear); p[2][3] = (-2.f * zFar*zNear)/(zFar-zNear);
p[3][0] = 0; p[3][1] = 0; p[3][2] = -1.0f; p[3][3] = 0;*/
}
#endif
void _FionaUTCalcMatrices(int eye, int wini, const FionaWall& wall, const FionaViewport& vp, const jvec3& _ep, int w, int h)
{
#ifdef ENABLE_OCULUS
#ifdef ENABLE_DK2
//static float BodyYaw(3.141592f);
//static OVR::Vector3f HeadPos(0.0f, 1.6f, -5.0f);
//code below appears in latest SDK.. may need to replace below stuff with it...
ovrVector3f eyeOffset[2];
eyeOffset[0] = fionaConf.oculusEyeRenderDesc[0].HmdToEyeViewOffset;
eyeOffset[1] = fionaConf.oculusEyeRenderDesc[1].HmdToEyeViewOffset;
//ovrPosef poses[2];
ovrFrameTiming ftiming = ovrHmd_GetFrameTiming(fionaConf.oculusHmd, 0);
ovrTrackingState hmdState = ovrHmd_GetTrackingState(fionaConf.oculusHmd, ftiming.DisplayMidpointSeconds);
ovr_CalcEyePoses(hmdState.HeadPose.ThePose, eyeOffset, fionaConf.layer.RenderPose);//poses);
//these could be moved outside of the loop
/* ovrEyeType oeye = fionaConf.oculusHmd->EyeRenderOrder[eye];
ovrEyeRenderDesc desc = ovrHmd_GetRenderDesc(fionaConf.oculusHmd, ovrEyeType::ovrEye_Left, fionaConf.oculusHmd->DefaultEyeFov[0]);
ovrEyeRenderDesc desc2 = ovrHmd_GetRenderDesc(fionaConf.oculusHmd, ovrEyeType::ovrEye_Right, fionaConf.oculusHmd->DefaultEyeFov[1]);
ovrPosef poses[2];
//ovrVector3f eyeOffset[2];
eyeOffset[0] = desc.HmdToEyeViewOffset;
eyeOffset[1] = desc2.HmdToEyeViewOffset;*/
//ovrHmd_GetEyePoses(fionaConf.oculusHmd, 0, eyeOffset, poses, 0);
ovrPosef eyeRenderPose = fionaConf.layer.RenderPose[eye == 2 ? 1 : 0];//ovrHmd_GetHmdPosePerEye(fionaConf.oculusHmd, oeye);
// Get view and projection matrices
//OVR::Matrix4f rollPitchYaw = OVR::Matrix4f::RotationY(BodyYaw);
//OVR::Matrix4f finalRollPitchYaw = /*rollPitchYaw * OVR::Matrix4f(eyeRenderPose.Orientation);
//OVR::Vector3f finalUp = finalRollPitchYaw.Transform(OVR::Vector3f(0,1,0));
//OVR::Vector3f finalForward = finalRollPitchYaw.Transform(OVR::Vector3f(0,0,-1));
fionaConf.headPos = jvec3(eyeRenderPose.Position.x, eyeRenderPose.Position.y, eyeRenderPose.Position.z);
fionaConf.headRot = quat(eyeRenderPose.Orientation.w, eyeRenderPose.Orientation.x, eyeRenderPose.Orientation.y, eyeRenderPose.Orientation.z);
//OVR::Vector3f shiftedEyePos = HeadPos + /*rollPitchYaw.Transform(*/eyeRenderPose.Position;//);
//OVR::Matrix4f view = OVR::Matrix4f::LookAtRH(shiftedEyePos, shiftedEyePos + finalForward, finalUp);
//OVR::Matrix4f proj = ovrMatrix4f_Projection(EyeRenderDesc[oeye].Fov, 0.01f, 10000.0f, true);
//pRender->SetViewport(Recti(EyeRenderViewport[eye]));
//pRoomScene->Render(pRender, Matrix4f::Translation(EyeRenderDesc[eye].ViewAdjust) * view);
#endif
#endif
#ifdef ENABLE_OCULUS
#ifdef ENABLE_CV1
ovrEyeRenderDesc eyeRenderDesc[2];
eyeRenderDesc[0] = ovr_GetRenderDesc(fionaConf.session, ovrEye_Left, fionaConf.hmdDesc.DefaultEyeFov[0]);
eyeRenderDesc[1] = ovr_GetRenderDesc(fionaConf.session, ovrEye_Right, fionaConf.hmdDesc.DefaultEyeFov[1]);
ovrPosef HmdToEyeOffset[2] = { eyeRenderDesc[0].HmdToEyePose,
eyeRenderDesc[1].HmdToEyePose };
//printf("Eye Left: %f %f %f\n", HmdToEyeOffset[0].Position.x, HmdToEyeOffset[0].Position.y, HmdToEyeOffset[0].Position.z);
//printf("Eye Right: %f %f %f\n", HmdToEyeOffset[1].Position.x, HmdToEyeOffset[1].Position.y, HmdToEyeOffset[1].Position.z);
if (fionaConf.singlePassStereo)
{
HmdToEyeOffset[0].Position.x = 0.f;
HmdToEyeOffset[0].Position.y = 0.f;
HmdToEyeOffset[0].Position.z = 0.f;
HmdToEyeOffset[1].Position.x = 0.f;
HmdToEyeOffset[1].Position.y = 0.f;
HmdToEyeOffset[1].Position.z = 0.f;
ovrFovPort fovMax;
fovMax.UpTan = MAX(fionaConf.hmdDesc.DefaultEyeFov[0].UpTan, fionaConf.hmdDesc.DefaultEyeFov[1].UpTan);
fovMax.DownTan = MAX(fionaConf.hmdDesc.DefaultEyeFov[0].DownTan, fionaConf.hmdDesc.DefaultEyeFov[1].DownTan);
fovMax.LeftTan = MAX(fionaConf.hmdDesc.DefaultEyeFov[0].LeftTan, fionaConf.hmdDesc.DefaultEyeFov[1].LeftTan);
fovMax.RightTan = MAX(fionaConf.hmdDesc.DefaultEyeFov[0].RightTan, fionaConf.hmdDesc.DefaultEyeFov[1].RightTan);
//ovrFovPort fovMax = FovPort::Max(fionaConf.hmdDesc.DefaultEyeFov[0], fionaConf.hmdDesc.DefaultEyeFov[1]);
float combinedTanHalfFovHorizontal = MAX(fovMax.LeftTan, fovMax.RightTan);
float combinedTanHalfFovVertical = MAX(fovMax.UpTan, fovMax.DownTan);
ovrFovPort fovBoth;
fovBoth.LeftTan = fovBoth.RightTan = combinedTanHalfFovHorizontal;
fovBoth.UpTan = fovBoth.DownTan = combinedTanHalfFovVertical;
fionaConf.hmdDesc.DefaultEyeFov[0] = fovBoth;
fionaConf.hmdDesc.DefaultEyeFov[1] = fovBoth;
}
if (fionaRenderCycleCount == 0)
{
fionaConf.sensorSampleTime = ovr_GetPredictedDisplayTime(fionaConf.session, fionaConf.frameIndex);
ovrTrackingState hmdState = ovr_GetTrackingState(fionaConf.session, fionaConf.sensorSampleTime, ovrTrue);
ovr_CalcEyePoses(hmdState.HeadPose.ThePose, HmdToEyeOffset, fionaConf.eyeRenderPose);
}
//ovr_GetEyePoses(fionaConf.session, fionaConf.frameIndex, ovrTrue, HmdToEyeOffset, fionaConf.eyeRenderPose, &fionaConf.sensorSampleTime);
//printf("Eye: %d\n", eye);
//printf("Left: %f, %f, %f\n", fionaConf.eyeRenderPose[0].Position.x, fionaConf.eyeRenderPose[0].Position.y, fionaConf.eyeRenderPose[0].Position.z);
//printf("Right: %f, %f, %f\n", fionaConf.eyeRenderPose[1].Position.x, fionaConf.eyeRenderPose[1].Position.y, fionaConf.eyeRenderPose[1].Position.z);
fionaConf.headPos = jvec3(fionaConf.eyeRenderPose[eye == 2 ? 1 : 0].Position.x, fionaConf.eyeRenderPose[eye == 2 ? 1 : 0].Position.y, fionaConf.eyeRenderPose[eye == 2 ? 1 : 0].Position.z);// -jvec3(HmdToEyeOffset[0].Position.x, HmdToEyeOffset[0].Position.y, HmdToEyeOffset[0].Position.z);
fionaConf.headRot = quat(fionaConf.eyeRenderPose[eye == 2 ? 1 : 0].Orientation.w, fionaConf.eyeRenderPose[eye == 2 ? 1 : 0].Orientation.x, fionaConf.eyeRenderPose[eye == 2 ? 1 : 0].Orientation.y, fionaConf.eyeRenderPose[eye == 2 ? 1 : 0].Orientation.z);
//this also sets viewport...
if (!fionaConf.singlePassStereo)
{
fionaConf.eyeRenderTexture[eye == 2 ? 1 : 0]->SetAndClearRenderSurface(fionaConf.eyeDepthBuffer[eye == 2 ? 1 : 0]);
}
#endif
#endif
if(eye==0||eye==1)
{
if(fionaConf.appType != FionaConfig::OCULUS)
{
#ifdef ENABLE_VIVE
glBindFramebuffer(GL_FRAMEBUFFER, fionaConf.leftEyeDesc.m_nRenderFramebufferId);
glViewport(0, 0, fionaConf.FBOWidth, fionaConf.FBOHeight);
glClearColor(0.f, 1.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#else
glViewport(vp.lx*w,vp.ly*h,vp.lw*w,vp.lh*h);
#endif
}
else
{
#ifdef ENABLE_OCULUS
#ifdef ENABLE_DK2
glViewport(0, 0, fionaConf.FBOWidth / 2, fionaConf.FBOHeight);
#endif
#ifdef ENABLE_DK1
OVR::Util::Render::StereoEyeParams le = fionaConf.stereoConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Left);
glViewport(le.VP.x, le.VP.y, le.VP.w, le.VP.h);
#endif
#endif
}
}
else
{
if(fionaConf.appType != FionaConfig::OCULUS)
{
#ifdef ENABLE_VIVE
glBindFramebuffer(GL_FRAMEBUFFER, fionaConf.rightEyeDesc.m_nRenderFramebufferId);
glViewport(0, 0, fionaConf.FBOWidth, fionaConf.FBOHeight);
glClearColor(1.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#else
glViewport(vp.rx*w,vp.ry*h,vp.rw*w,vp.rh*h);
#endif
}
else
{
#ifdef ENABLE_OCULUS
#ifdef ENABLE_DK2
glViewport((fionaConf.FBOWidth) / 2, 0, fionaConf.FBOWidth / 2, fionaConf.FBOHeight);
#endif
#ifdef ENABLE_DK1
OVR::Util::Render::StereoEyeParams re = fionaConf.stereoConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Right);
glViewport(re.VP.x, re.VP.y, re.VP.w, re.VP.h);
#endif
#endif
}
}
// setup projection matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
if( fionaConf.desktopProjection )
{
gluPerspective(fionaConf.desktopFOV, (double)((double)w / (double)h), fionaConf.nearClip, fionaConf.farClip);
}
else
{
if (fionaConf.appType != FionaConfig::OCULUS)
{
#ifdef ENABLE_VIVE
if (eye == 2)
{
glm::mat4 mvp = fionaConf.projRight * fionaConf.mvRight * fionaConf.hmdPose;
glMultMatrix(glm::value_ptr(mvp));
}
else
{
if (fionaConf.singlePassStereo)
{
glm::mat4 mvp = fionaConf.projLeft;
glMultMatrix(glm::value_ptr(mvp));
}
else
{
glm::mat4 mvp = fionaConf.projLeft * fionaConf.mvLeft * fionaConf.hmdPose;
glMultMatrix(glm::value_ptr(mvp));
}
}
#else
glMultMatrix(viewportProjection(vp)*projectorCalibration(vp)*caveProjection(wall.sz,_ep));
//glMultMatrix(viewportProjection(vp)*projectorCalibration(vp)*caveProjectionWall(wall, _ep));
#endif
}
else
{
#ifdef ENABLE_OCULUS
#ifdef ENABLE_DK2
OVR::Matrix4f m;
//todo - pull fov from renderpose if possible..
if(eye <= 1)
{
m = ovrMatrix4f_Projection(fionaConf.oculusEyeRenderDesc[0].Fov, fionaConf.nearClip, fionaConf.farClip, ovrProjection_RightHanded);// | ovrProjection_FarLessThanNear);
}
else
{
m = ovrMatrix4f_Projection(fionaConf.oculusEyeRenderDesc[1].Fov, fionaConf.nearClip, fionaConf.farClip, ovrProjection_RightHanded);// | ovrProjection_FarLessThanNear);
}
mat4 fm(m.M[0][0], m.M[0][1], m.M[0][2], m.M[0][3], m.M[1][0], m.M[1][1], m.M[1][2], m.M[1][3], m.M[2][0], m.M[2][1], m.M[2][2], m.M[2][3], m.M[3][0], m.M[3][1], m.M[3][2], m.M[3][3]);
glMultMatrix(fm);
#endif
#ifdef ENABLE_DK1
if(eye==0)
{
OVR::Util::Render::StereoEyeParams ce = fionaConf.stereoConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Center);
float mat[16];
memset(mat, 0.f, sizeof(float)*16);
OVR::Matrix4f m = (ce.Projection /** ce.ViewAdjust*/);
mat4 fm(m.M[0][0], m.M[0][1], m.M[0][2], m.M[0][3], m.M[1][0], m.M[1][1], m.M[1][2], m.M[1][3], m.M[2][0], m.M[2][1], m.M[2][2], m.M[2][3], m.M[3][0], m.M[3][1], m.M[3][2], m.M[3][3]);
glMultMatrix(fm);
}
else if(eye==1)
{
OVR::Util::Render::StereoEyeParams le = fionaConf.stereoConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Left);
float mat[16];
memset(mat, 0.f, sizeof(float)*16);
OVR::Matrix4f m = (le.Projection/* * le.ViewAdjust*/);
mat4 fm(m.M[0][0], m.M[0][1], m.M[0][2], m.M[0][3], m.M[1][0], m.M[1][1], m.M[1][2], m.M[1][3], m.M[2][0], m.M[2][1], m.M[2][2], m.M[2][3], m.M[3][0], m.M[3][1], m.M[3][2], m.M[3][3]);
glMultMatrix(fm);
}
else if(eye==2)
{
OVR::Util::Render::StereoEyeParams re = fionaConf.stereoConfig.GetEyeRenderParams(OVR::Util::Render::StereoEye_Right);
float mat[16];
memset(mat, 0.f, sizeof(float)*16);
OVR::Matrix4f m = (re.Projection/* * re.ViewAdjust*/);
mat4 fm(m.M[0][0], m.M[0][1], m.M[0][2], m.M[0][3], m.M[1][0], m.M[1][1], m.M[1][2], m.M[1][3], m.M[2][0], m.M[2][1], m.M[2][2], m.M[2][3], m.M[3][0], m.M[3][1], m.M[3][2], m.M[3][3]);
glMultMatrix(fm);
}
#endif
#ifdef ENABLE_CV1
ovrMatrix4f m;
if (eye <= 1)
{
m = ovrMatrix4f_Projection(fionaConf.hmdDesc.DefaultEyeFov[0], fionaConf.nearClip, fionaConf.farClip, ovrProjection_None);// | ovrProjection_FarLessThanNear);
}
else
{