This website uses cookies to improve our services and deliver relevant ads.
By interacting with this site, you agree to this use. For more information, see our Cookies Policy

Mobile 1.22 Reference Guide

VrApi_Helpers.h File Reference

Macros

#define
VRAPI_PI
#define
VRAPI_ZNEAR
#define
VRAPI_UNUSED

Functions

static float
ovrRadiansFromDegrees ( float deg )
static float
ovrDegreesFromRadians ( float rad )
static ovrVector4f
static ovrMatrix4f
ovrMatrix4f_Multiply ( const ovrMatrix4f * a, const ovrMatrix4f * b )
Use left-multiplication to accumulate transformations.
static ovrMatrix4f
Returns the transpose of a 4x4 matrix.
static float
ovrMatrix4f_Minor ( const ovrMatrix4f * m, int r0, int r1, int r2, int c0, int c1, int c2 )
Returns a 3x3 minor of a 4x4 matrix.
static ovrMatrix4f
Returns the inverse of a 4x4 matrix.
static ovrMatrix4f
Returns a 4x4 identity matrix.
static ovrMatrix4f
ovrMatrix4f_CreateScale ( const float x, const float y, const float z )
Returns a 4x4 scaling matrix.
static ovrMatrix4f
ovrMatrix4f_CreateTranslation ( const float x, const float y, const float z )
Returns a 4x4 homogeneous translation matrix.
static ovrMatrix4f
ovrMatrix4f_CreateRotation ( const float radiansX, const float radiansY, const float radiansZ )
Returns a 4x4 homogeneous rotation matrix.
static ovrMatrix4f
ovrMatrix4f_CreateProjection ( const float minX, const float maxX, float const minY, const float maxY, const float nearZ, const float farZ )
Returns a projection matrix based on the specified dimensions.
static ovrMatrix4f
ovrMatrix4f_CreateProjectionFov ( const float fovDegreesX, const float fovDegreesY, const float offsetX, const float offsetY, const float nearZ, const float farZ )
Returns a projection matrix based on the given FOV.
static ovrMatrix4f
ovrMatrix4f_CreateProjectionAsymmetricFov ( const float leftDegrees, const float rightDegrees, const float upDegrees, const float downDegrees, const float nearZ, const float farZ )
Returns a projection matrix based on the given asymmetric FOV.
static void
ovrMatrix4f_ExtractFov ( const ovrMatrix4f * m, float * leftDegrees, float * rightDegrees, float * upDegrees, float * downDegrees )
static ovrMatrix4f
Returns the 4x4 rotation matrix for the given quaternion.
static ovrMatrix4f
Convert a standard projection matrix into a TexCoordsFromTanAngles matrix for the primary time warp surface.
static ovrMatrix4f
If a simple quad defined as a -1 to 1 XY unit square is transformed to the camera view with the given modelView matrix, it can alternately be drawn as a time warp overlay image to take advantage of the full window resolution, which is usually higher than the eye buffer textures, and avoids resampling both into the eye buffer, and again to the screen.
static ovrMatrix4f
Convert a standard view matrix into a TexCoordsFromTanAngles matrix for the lookup into a cube map.
static ovrVector3f
ovrVector3f_RotateAboutPivot ( const ovrQuatf * rotation, const ovrVector3f * pivot, const ovrVector3f * point )
Utility function to rotate a point about a pivot.
static ovrInitParms
Utility function to default initialize the ovrInitParms.
static ovrModeParms
Utility function to default initialize the ovrModeParms.
static ovrModeParmsVulkan
vrapi_DefaultModeParmsVulkan ( const ovrJava * java, unsigned long long synchronizationQueue )
static ovrPerformanceParms
Utility function to default initialize the ovrPerformanceParms.
static ovrFrameParms
vrapi_DefaultFrameParms ( const ovrJava * java, const ovrFrameInit init, const double currentTime, ovrTextureSwapChain * textureSwapChain )
Utility function to default initialize the ovrFrameParms.
static ovrLayerProjection2
static ovrLayerProjection2
static ovrLayerProjection2
static ovrLayerCylinder2
static ovrLayerCube2
static ovrLayerEquirect2
static ovrLayerLoadingIcon2
static float
static float
vrapi_GetEyeHeight ( const ovrPosef * eyeLevelTrackingPose, const ovrPosef * currentTrackingPose )
static ovrMatrix4f
static ovrMatrix4f
static ovrMatrix4f
vrapi_GetEyeViewMatrix ( const ovrMatrix4f * centerEyeViewMatrix, const float interpupillaryDistance, const int eye )
Utility function to get the eye view matrix based on the center eye view matrix and the IPD.

Macros Documentation

#define VRAPI_PI
#define VRAPI_ZNEAR
#define VRAPI_UNUSED

Enumeration Type Documentation

enum VrApi_Helpers.h.ovrFrameInit
Enumerator
VRAPI_FRAME_INIT_DEFAULT= 0
VRAPI_FRAME_INIT_BLACK= 1
VRAPI_FRAME_INIT_BLACK_FLUSH= 2
VRAPI_FRAME_INIT_BLACK_FINAL= 3
VRAPI_FRAME_INIT_LOADING_ICON= 4
VRAPI_FRAME_INIT_LOADING_ICON_FLUSH= 5

Function Documentation

static float ovrRadiansFromDegrees ( float deg )
static float ovrDegreesFromRadians ( float rad )
static ovrVector4f ovrVector4f_MultiplyMatrix4f ( const ovrMatrix4f * a, const ovrVector4f * v )
static ovrMatrix4f ovrMatrix4f_Multiply ( const ovrMatrix4f * a, const ovrMatrix4f * b )
Use left-multiplication to accumulate transformations.
static ovrMatrix4f ovrMatrix4f_Transpose ( const ovrMatrix4f * a )
Returns the transpose of a 4x4 matrix.
static float ovrMatrix4f_Minor ( const ovrMatrix4f * m, int r0, int r1, int r2, int c0, int c1, int c2 )
Returns a 3x3 minor of a 4x4 matrix.
static ovrMatrix4f ovrMatrix4f_Inverse ( const ovrMatrix4f * m )
Returns the inverse of a 4x4 matrix.
static ovrMatrix4f ovrMatrix4f_CreateIdentity ( )
Returns a 4x4 identity matrix.
static ovrMatrix4f ovrMatrix4f_CreateScale ( const float x, const float y, const float z )
Returns a 4x4 scaling matrix.
static ovrMatrix4f ovrMatrix4f_CreateTranslation ( const float x, const float y, const float z )
Returns a 4x4 homogeneous translation matrix.
static ovrMatrix4f ovrMatrix4f_CreateRotation ( const float radiansX, const float radiansY, const float radiansZ )
Returns a 4x4 homogeneous rotation matrix.
static ovrMatrix4f ovrMatrix4f_CreateProjection ( const float minX, const float maxX, float const minY, const float maxY, const float nearZ, const float farZ )
Returns a projection matrix based on the specified dimensions.
The projection matrix transforms -Z=forward, +Y=up, +X=right to the appropriate clip space for the graphics API. The far plane is placed at infinity if farZ <= nearZ. An infinite projection matrix is preferred for rasterization because, except for things right up against the near plane, it always provides better precision: "Tightening the Precision of Perspective Rendering" Paul Upchurch, Mathieu Desbrun Journal of Graphics Tools, Volume 16, Issue 1, 2012
static ovrMatrix4f ovrMatrix4f_CreateProjectionFov ( const float fovDegreesX, const float fovDegreesY, const float offsetX, const float offsetY, const float nearZ, const float farZ )
Returns a projection matrix based on the given FOV.
static ovrMatrix4f ovrMatrix4f_CreateProjectionAsymmetricFov ( const float leftDegrees, const float rightDegrees, const float upDegrees, const float downDegrees, const float nearZ, const float farZ )
Returns a projection matrix based on the given asymmetric FOV.
static void ovrMatrix4f_ExtractFov ( const ovrMatrix4f * m, float * leftDegrees, float * rightDegrees, float * upDegrees, float * downDegrees )
static ovrMatrix4f ovrMatrix4f_CreateFromQuaternion ( const ovrQuatf * q )
Returns the 4x4 rotation matrix for the given quaternion.
static ovrMatrix4f ovrMatrix4f_TanAngleMatrixFromProjection ( const ovrMatrix4f * projection )
Convert a standard projection matrix into a TexCoordsFromTanAngles matrix for the primary time warp surface.
static ovrMatrix4f ovrMatrix4f_TanAngleMatrixFromUnitSquare ( const ovrMatrix4f * modelView )
If a simple quad defined as a -1 to 1 XY unit square is transformed to the camera view with the given modelView matrix, it can alternately be drawn as a time warp overlay image to take advantage of the full window resolution, which is usually higher than the eye buffer textures, and avoids resampling both into the eye buffer, and again to the screen.
This is used for high quality movie screens and user interface planes.
Note that this is NOT an MVP matrix the "projection" is handled by the distortion process.
This utility functions converts a model-view matrix that would normally draw a -1 to 1 unit square to the view into a TexCoordsFromTanAngles matrix for an overlay surface.
The resulting z value should be straight ahead distance to the plane. The x and y values will be pre-multiplied by z for projective texturing.
static ovrMatrix4f ovrMatrix4f_TanAngleMatrixForCubeMap ( const ovrMatrix4f * viewMatrix )
Convert a standard view matrix into a TexCoordsFromTanAngles matrix for the lookup into a cube map.
static ovrVector3f ovrVector3f_RotateAboutPivot ( const ovrQuatf * rotation, const ovrVector3f * pivot, const ovrVector3f * point )
Utility function to rotate a point about a pivot.
static ovrInitParms vrapi_DefaultInitParms ( const ovrJava * java )
Utility function to default initialize the ovrInitParms.
static ovrModeParms vrapi_DefaultModeParms ( const ovrJava * java )
Utility function to default initialize the ovrModeParms.
static ovrModeParmsVulkan vrapi_DefaultModeParmsVulkan ( const ovrJava * java, unsigned long long synchronizationQueue )
static ovrPerformanceParms vrapi_DefaultPerformanceParms ( )
Utility function to default initialize the ovrPerformanceParms.
static ovrFrameParms vrapi_DefaultFrameParms ( const ovrJava * java, const ovrFrameInit init, const double currentTime, ovrTextureSwapChain * textureSwapChain )
Utility function to default initialize the ovrFrameParms.
static ovrLayerProjection2 vrapi_DefaultLayerProjection2 ( )
static ovrLayerProjection2 vrapi_DefaultLayerBlackProjection2 ( )
static ovrLayerProjection2 vrapi_DefaultLayerSolidColorProjection2 ( const ovrVector4f * colorScale )
static ovrLayerCylinder2 vrapi_DefaultLayerCylinder2 ( )
static ovrLayerCube2 vrapi_DefaultLayerCube2 ( )
static ovrLayerEquirect2 vrapi_DefaultLayerEquirect2 ( )
static ovrLayerLoadingIcon2 vrapi_DefaultLayerLoadingIcon2 ( )
static float vrapi_GetInterpupillaryDistance ( const ovrTracking2 * tracking2 )
static float vrapi_GetEyeHeight ( const ovrPosef * eyeLevelTrackingPose, const ovrPosef * currentTrackingPose )
static ovrMatrix4f vrapi_GetTransformFromPose ( const ovrPosef * pose )
static ovrMatrix4f vrapi_GetViewMatrixFromPose ( const ovrPosef * pose )
static ovrMatrix4f vrapi_GetEyeViewMatrix ( const ovrMatrix4f * centerEyeViewMatrix, const float interpupillaryDistance, const int eye )
Utility function to get the eye view matrix based on the center eye view matrix and the IPD.
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
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
85
86
87
88
89
90
91
92
93
94
96
97
98
99
100
101
102
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
131
132
133
134
135
136
137
138
139
140
142
143
144
145
146
147
148
149
150
151
153
154
155
156
157
158
159
160
161
162
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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
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
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
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
458
459
460
461
462
463
464
465
466
467
468
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
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
884
885
886
887
888
889
890
891
892
893
/************************************************************************************

Filename    :   VrApi_Helpers.h
Content     :   Pure, stateless, inlined helper functions, used to initialize
                parameters to the VrApi.
Created     :   March 2, 2015
Authors     :   J.M.P. van Waveren
Language    :   C99

Copyright   :   Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.

*************************************************************************************/
#ifndef OVR_VrApi_Helpers_h
#define OVR_VrApi_Helpers_h

#include "math.h"// for cosf(), sinf(), tanf()
#include "string.h"// for memset()
#include "VrApi_Config.h"
#include "VrApi_Version.h"
#include "VrApi_Types.h"

#define VRAPI_PI        3.14159265358979323846f
#define VRAPI_ZNEAR     0.1f

#if defined( __GNUC__ )
#  define   VRAPI_UNUSED(a)   do {__typeof__ (&a) __attribute__ ((unused)) __tmp = &a; } while(0)
#else
#  define   VRAPI_UNUSED(a)   (a)
#endif

//-----------------------------------------------------------------
// Misc helper functions.
//-----------------------------------------------------------------
staticinlinefloat ovrRadiansFromDegrees( float deg )
{
return deg * VRAPI_PI / 180.0f;
}

staticinlinefloat ovrDegreesFromRadians( float rad )
{
return rad * 180.f / VRAPI_PI;
}

//-----------------------------------------------------------------
// Matrix helper functions.
//-----------------------------------------------------------------

staticinline ovrVector4f ovrVector4f_MultiplyMatrix4f( const ovrMatrix4f * a, const ovrVector4f * v )
{
    ovrVector4f out;
    out.x = a->M[0][0] * v->x + a->M[0][1] * v->y + a->M[0][2] * v->z + a->M[0][3] * v->w;
    out.y = a->M[1][0] * v->x + a->M[1][1] * v->y + a->M[1][2] * v->z + a->M[1][3] * v->w;
    out.z = a->M[2][0] * v->x + a->M[2][1] * v->y + a->M[2][2] * v->z + a->M[2][3] * v->w;
    out.w = a->M[3][0] * v->x + a->M[3][1] * v->y + a->M[3][2] * v->z + a->M[3][3] * v->w;
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_Multiply( const ovrMatrix4f * a, const ovrMatrix4f * b )
{
    ovrMatrix4f out;
    out.M[0][0] = a->M[0][0] * b->M[0][0] + a->M[0][1] * b->M[1][0] + a->M[0][2] * b->M[2][0] + a->M[0][3] * b->M[3][0];
    out.M[1][0] = a->M[1][0] * b->M[0][0] + a->M[1][1] * b->M[1][0] + a->M[1][2] * b->M[2][0] + a->M[1][3] * b->M[3][0];
    out.M[2][0] = a->M[2][0] * b->M[0][0] + a->M[2][1] * b->M[1][0] + a->M[2][2] * b->M[2][0] + a->M[2][3] * b->M[3][0];
    out.M[3][0] = a->M[3][0] * b->M[0][0] + a->M[3][1] * b->M[1][0] + a->M[3][2] * b->M[2][0] + a->M[3][3] * b->M[3][0];

    out.M[0][1] = a->M[0][0] * b->M[0][1] + a->M[0][1] * b->M[1][1] + a->M[0][2] * b->M[2][1] + a->M[0][3] * b->M[3][1];
    out.M[1][1] = a->M[1][0] * b->M[0][1] + a->M[1][1] * b->M[1][1] + a->M[1][2] * b->M[2][1] + a->M[1][3] * b->M[3][1];
    out.M[2][1] = a->M[2][0] * b->M[0][1] + a->M[2][1] * b->M[1][1] + a->M[2][2] * b->M[2][1] + a->M[2][3] * b->M[3][1];
    out.M[3][1] = a->M[3][0] * b->M[0][1] + a->M[3][1] * b->M[1][1] + a->M[3][2] * b->M[2][1] + a->M[3][3] * b->M[3][1];

    out.M[0][2] = a->M[0][0] * b->M[0][2] + a->M[0][1] * b->M[1][2] + a->M[0][2] * b->M[2][2] + a->M[0][3] * b->M[3][2];
    out.M[1][2] = a->M[1][0] * b->M[0][2] + a->M[1][1] * b->M[1][2] + a->M[1][2] * b->M[2][2] + a->M[1][3] * b->M[3][2];
    out.M[2][2] = a->M[2][0] * b->M[0][2] + a->M[2][1] * b->M[1][2] + a->M[2][2] * b->M[2][2] + a->M[2][3] * b->M[3][2];
    out.M[3][2] = a->M[3][0] * b->M[0][2] + a->M[3][1] * b->M[1][2] + a->M[3][2] * b->M[2][2] + a->M[3][3] * b->M[3][2];

    out.M[0][3] = a->M[0][0] * b->M[0][3] + a->M[0][1] * b->M[1][3] + a->M[0][2] * b->M[2][3] + a->M[0][3] * b->M[3][3];
    out.M[1][3] = a->M[1][0] * b->M[0][3] + a->M[1][1] * b->M[1][3] + a->M[1][2] * b->M[2][3] + a->M[1][3] * b->M[3][3];
    out.M[2][3] = a->M[2][0] * b->M[0][3] + a->M[2][1] * b->M[1][3] + a->M[2][2] * b->M[2][3] + a->M[2][3] * b->M[3][3];
    out.M[3][3] = a->M[3][0] * b->M[0][3] + a->M[3][1] * b->M[1][3] + a->M[3][2] * b->M[2][3] + a->M[3][3] * b->M[3][3];
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_Transpose( const ovrMatrix4f * a )
{
    ovrMatrix4f out;
    out.M[0][0] = a->M[0][0]; out.M[0][1] = a->M[1][0]; out.M[0][2] = a->M[2][0]; out.M[0][3] = a->M[3][0];
    out.M[1][0] = a->M[0][1]; out.M[1][1] = a->M[1][1]; out.M[1][2] = a->M[2][1]; out.M[1][3] = a->M[3][1];
    out.M[2][0] = a->M[0][2]; out.M[2][1] = a->M[1][2]; out.M[2][2] = a->M[2][2]; out.M[2][3] = a->M[3][2];
    out.M[3][0] = a->M[0][3]; out.M[3][1] = a->M[1][3]; out.M[3][2] = a->M[2][3]; out.M[3][3] = a->M[3][3];
return out;
}

staticinlinefloat ovrMatrix4f_Minor( const ovrMatrix4f * m, int r0, int r1, int r2, int c0, int c1, int c2 )
{
return  m->M[r0][c0] * ( m->M[r1][c1] * m->M[r2][c2] - m->M[r2][c1] * m->M[r1][c2] ) -
            m->M[r0][c1] * ( m->M[r1][c0] * m->M[r2][c2] - m->M[r2][c0] * m->M[r1][c2] ) +
            m->M[r0][c2] * ( m->M[r1][c0] * m->M[r2][c1] - m->M[r2][c0] * m->M[r1][c1] );
}

staticinline ovrMatrix4f ovrMatrix4f_Inverse( const ovrMatrix4f * m )
{
constfloat rcpDet = 1.0f / (   m->M[0][0] * ovrMatrix4f_Minor( m, 1, 2, 3, 1, 2, 3 ) -
                                    m->M[0][1] * ovrMatrix4f_Minor( m, 1, 2, 3, 0, 2, 3 ) +
                                    m->M[0][2] * ovrMatrix4f_Minor( m, 1, 2, 3, 0, 1, 3 ) -
                                    m->M[0][3] * ovrMatrix4f_Minor( m, 1, 2, 3, 0, 1, 2 ) );
    ovrMatrix4f out;
    out.M[0][0] =  ovrMatrix4f_Minor( m, 1, 2, 3, 1, 2, 3 ) * rcpDet;
    out.M[0][1] = -ovrMatrix4f_Minor( m, 0, 2, 3, 1, 2, 3 ) * rcpDet;
    out.M[0][2] =  ovrMatrix4f_Minor( m, 0, 1, 3, 1, 2, 3 ) * rcpDet;
    out.M[0][3] = -ovrMatrix4f_Minor( m, 0, 1, 2, 1, 2, 3 ) * rcpDet;
    out.M[1][0] = -ovrMatrix4f_Minor( m, 1, 2, 3, 0, 2, 3 ) * rcpDet;
    out.M[1][1] =  ovrMatrix4f_Minor( m, 0, 2, 3, 0, 2, 3 ) * rcpDet;
    out.M[1][2] = -ovrMatrix4f_Minor( m, 0, 1, 3, 0, 2, 3 ) * rcpDet;
    out.M[1][3] =  ovrMatrix4f_Minor( m, 0, 1, 2, 0, 2, 3 ) * rcpDet;
    out.M[2][0] =  ovrMatrix4f_Minor( m, 1, 2, 3, 0, 1, 3 ) * rcpDet;
    out.M[2][1] = -ovrMatrix4f_Minor( m, 0, 2, 3, 0, 1, 3 ) * rcpDet;
    out.M[2][2] =  ovrMatrix4f_Minor( m, 0, 1, 3, 0, 1, 3 ) * rcpDet;
    out.M[2][3] = -ovrMatrix4f_Minor( m, 0, 1, 2, 0, 1, 3 ) * rcpDet;
    out.M[3][0] = -ovrMatrix4f_Minor( m, 1, 2, 3, 0, 1, 2 ) * rcpDet;
    out.M[3][1] =  ovrMatrix4f_Minor( m, 0, 2, 3, 0, 1, 2 ) * rcpDet;
    out.M[3][2] = -ovrMatrix4f_Minor( m, 0, 1, 3, 0, 1, 2 ) * rcpDet;
    out.M[3][3] =  ovrMatrix4f_Minor( m, 0, 1, 2, 0, 1, 2 ) * rcpDet;
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_CreateIdentity()
{
    ovrMatrix4f out;
    out.M[0][0] = 1.0f; out.M[0][1] = 0.0f; out.M[0][2] = 0.0f; out.M[0][3] = 0.0f;
    out.M[1][0] = 0.0f; out.M[1][1] = 1.0f; out.M[1][2] = 0.0f; out.M[1][3] = 0.0f;
    out.M[2][0] = 0.0f; out.M[2][1] = 0.0f; out.M[2][2] = 1.0f; out.M[2][3] = 0.0f;
    out.M[3][0] = 0.0f; out.M[3][1] = 0.0f; out.M[3][2] = 0.0f; out.M[3][3] = 1.0f;
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_CreateScale( constfloat x, constfloat y, constfloat z )
{
    ovrMatrix4f out;
    out.M[0][0] = x;    out.M[0][1] = 0.0f; out.M[0][2] = 0.0f; out.M[0][3] = 0.0f;
    out.M[1][0] = 0.0f; out.M[1][1] = y;    out.M[1][2] = 0.0f; out.M[1][3] = 0.0f;
    out.M[2][0] = 0.0f; out.M[2][1] = 0.0f; out.M[2][2] = z;    out.M[2][3] = 0.0f;
    out.M[3][0] = 0.0f; out.M[3][1] = 0.0f; out.M[3][2] = 0.0f; out.M[3][3] = 1.0f;
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_CreateTranslation( constfloat x, constfloat y, constfloat z )
{
    ovrMatrix4f out;
    out.M[0][0] = 1.0f; out.M[0][1] = 0.0f; out.M[0][2] = 0.0f; out.M[0][3] = x;
    out.M[1][0] = 0.0f; out.M[1][1] = 1.0f; out.M[1][2] = 0.0f; out.M[1][3] = y;
    out.M[2][0] = 0.0f; out.M[2][1] = 0.0f; out.M[2][2] = 1.0f; out.M[2][3] = z;
    out.M[3][0] = 0.0f; out.M[3][1] = 0.0f; out.M[3][2] = 0.0f; out.M[3][3] = 1.0f;
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_CreateRotation( constfloat radiansX, constfloat radiansY, constfloat radiansZ )
{
constfloat sinX = sinf( radiansX );
constfloat cosX = cosf( radiansX );
const ovrMatrix4f rotationX =
    { {
        { 1,    0,     0, 0 },
        { 0, cosX, -sinX, 0 },
        { 0, sinX,  cosX, 0 },
        { 0,    0,     0, 1 }
    } };
constfloat sinY = sinf( radiansY );
constfloat cosY = cosf( radiansY );
const ovrMatrix4f rotationY =
    { {
        {  cosY, 0, sinY, 0 },
        {     0, 1,    0, 0 },
        { -sinY, 0, cosY, 0 },
        {     0, 0,    0, 1 }
    } };
constfloat sinZ = sinf( radiansZ );
constfloat cosZ = cosf( radiansZ );
const ovrMatrix4f rotationZ =
    { {
        { cosZ, -sinZ, 0, 0 },
        { sinZ,  cosZ, 0, 0 },
        {    0,     0, 1, 0 },
        {    0,     0, 0, 1 }
    } };
const ovrMatrix4f rotationXY = ovrMatrix4f_Multiply( &rotationY, &rotationX );
return ovrMatrix4f_Multiply( &rotationZ, &rotationXY );
}

staticinline ovrMatrix4f ovrMatrix4f_CreateProjection( constfloat minX, constfloat maxX,
floatconst minY, constfloat maxY, constfloat nearZ, constfloat farZ )
{
constfloat width = maxX - minX;
constfloat height = maxY - minY;
constfloat offsetZ = nearZ;    // set to zero for a [0,1] clip space

    ovrMatrix4f out;
if ( farZ <= nearZ )
    {
// place the far plane at infinity
        out.M[0][0] = 2 * nearZ / width;
        out.M[0][1] = 0;
        out.M[0][2] = ( maxX + minX ) / width;
        out.M[0][3] = 0;

        out.M[1][0] = 0;
        out.M[1][1] = 2 * nearZ / height;
        out.M[1][2] = ( maxY + minY ) / height;
        out.M[1][3] = 0;

        out.M[2][0] = 0;
        out.M[2][1] = 0;
        out.M[2][2] = -1;
        out.M[2][3] = -( nearZ + offsetZ );

        out.M[3][0] = 0;
        out.M[3][1] = 0;
        out.M[3][2] = -1;
        out.M[3][3] = 0;
    }
else
    {
// normal projection
        out.M[0][0] = 2 * nearZ / width;
        out.M[0][1] = 0;
        out.M[0][2] = ( maxX + minX ) / width;
        out.M[0][3] = 0;

        out.M[1][0] = 0;
        out.M[1][1] = 2 * nearZ / height;
        out.M[1][2] = ( maxY + minY ) / height;
        out.M[1][3] = 0;

        out.M[2][0] = 0;
        out.M[2][1] = 0;
        out.M[2][2] = -( farZ + offsetZ ) / ( farZ - nearZ );
        out.M[2][3] = -( farZ * ( nearZ + offsetZ ) ) / ( farZ - nearZ );

        out.M[3][0] = 0;
        out.M[3][1] = 0;
        out.M[3][2] = -1;
        out.M[3][3] = 0;
    }
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_CreateProjectionFov( constfloat fovDegreesX, constfloat fovDegreesY,
constfloat offsetX, constfloat offsetY, constfloat nearZ, constfloat farZ )
{
constfloat halfWidth = nearZ * tanf( fovDegreesX * ( VRAPI_PI / 180.0f * 0.5f ) );
constfloat halfHeight = nearZ * tanf( fovDegreesY * ( VRAPI_PI / 180.0f * 0.5f ) );

constfloat minX = offsetX - halfWidth;
constfloat maxX = offsetX + halfWidth;

constfloat minY = offsetY - halfHeight;
constfloat maxY = offsetY + halfHeight;

return ovrMatrix4f_CreateProjection( minX, maxX, minY, maxY, nearZ, farZ );
}

staticinline ovrMatrix4f ovrMatrix4f_CreateProjectionAsymmetricFov( constfloat leftDegrees, constfloat rightDegrees,
constfloat upDegrees, constfloat downDegrees,
constfloat nearZ, constfloat farZ )
{
constfloat minX = - nearZ * tanf( leftDegrees * ( VRAPI_PI / 180.0f ) );
constfloat maxX = nearZ * tanf( rightDegrees * ( VRAPI_PI / 180.0f ) );

constfloat minY = - nearZ * tanf( downDegrees * ( VRAPI_PI / 180.0f ) );
constfloat maxY = nearZ * tanf( upDegrees * ( VRAPI_PI / 180.0f ) );

return ovrMatrix4f_CreateProjection( minX, maxX, minY, maxY, nearZ, farZ );
}

// returns the FOV from the projection matrix
staticinlinevoid ovrMatrix4f_ExtractFov( const ovrMatrix4f * m, float * leftDegrees, float * rightDegrees,
float * upDegrees, float * downDegrees )
{
const ovrMatrix4f mt = ovrMatrix4f_Transpose( m );

staticconst ovrVector4f leftClip = { 1, 0, 0, 1 };
const ovrVector4f leftEye = ovrVector4f_MultiplyMatrix4f( &mt, &leftClip );
    *leftDegrees = -ovrDegreesFromRadians( atanf( leftEye.z / leftEye.x ) );

staticconst ovrVector4f rightClip = { -1, 0, 0, 1 };
const ovrVector4f rightEye = ovrVector4f_MultiplyMatrix4f( &mt, &rightClip );
    *rightDegrees = ovrDegreesFromRadians( atanf( rightEye.z / rightEye.x ) );

staticconst ovrVector4f downClip = { 0, 1, 0, 1 };
const ovrVector4f downEye = ovrVector4f_MultiplyMatrix4f( &mt, &downClip );
    *downDegrees = -ovrDegreesFromRadians( atanf( downEye.z / downEye.y ) );

staticconst ovrVector4f upClip = { 0, -1, 0, 1 };
const ovrVector4f upEye = ovrVector4f_MultiplyMatrix4f( &mt, &upClip );
    *upDegrees = ovrDegreesFromRadians( atanf( upEye.z / upEye.y ) );
}

staticinline ovrMatrix4f ovrMatrix4f_CreateFromQuaternion( const ovrQuatf * q )
{
constfloat ww = q->w * q->w;
constfloat xx = q->x * q->x;
constfloat yy = q->y * q->y;
constfloat zz = q->z * q->z;

    ovrMatrix4f out;
    out.M[0][0] = ww + xx - yy - zz;
    out.M[0][1] = 2 * ( q->x * q->y - q->w * q->z );
    out.M[0][2] = 2 * ( q->x * q->z + q->w * q->y );
    out.M[0][3] = 0;

    out.M[1][0] = 2 * ( q->x * q->y + q->w * q->z );
    out.M[1][1] = ww - xx + yy - zz;
    out.M[1][2] = 2 * ( q->y * q->z - q->w * q->x );
    out.M[1][3] = 0;

    out.M[2][0] = 2 * ( q->x * q->z - q->w * q->y );
    out.M[2][1] = 2 * ( q->y * q->z + q->w * q->x );
    out.M[2][2] = ww - xx - yy + zz;
    out.M[2][3] = 0;

    out.M[3][0] = 0;
    out.M[3][1] = 0;
    out.M[3][2] = 0;
    out.M[3][3] = 1;
return out;
}

staticinline ovrMatrix4f ovrMatrix4f_TanAngleMatrixFromProjection( const ovrMatrix4f * projection )
{
/*
        A projection matrix goes from a view point to NDC, or -1 to 1 space.
        Scale and bias to convert that to a 0 to 1 space.

        const ovrMatrix3f m =
        { {
            { projection->M[0][0],                0.0f, projection->M[0][2] },
            {                0.0f, projection->M[1][1], projection->M[1][2] },
            {                0.0f,                0.0f,               -1.0f }
        } };
        // Note that there is no Y-flip because eye buffers have 0,0 = left-bottom.
        const ovrMatrix3f s = ovrMatrix3f_CreateScaling( 0.5f, 0.5f );
        const ovrMatrix3f t = ovrMatrix3f_CreateTranslation( 0.5f, 0.5f );
        const ovrMatrix3f r0 = ovrMatrix3f_Multiply( &s, &m );
        const ovrMatrix3f r1 = ovrMatrix3f_Multiply( &t, &r0 );
        return r1;

        clipZ = ( z * projection[2][2] + projection[2][3] ) / ( projection[3][2] * z )
        z = projection[2][3] / ( clipZ * projection[3][2] - projection[2][2] )
        z = ( projection[2][3] / projection[3][2] ) / ( clipZ - projection[2][2] / projection[3][2] )
    */
const ovrMatrix4f tanAngleMatrix =
    { {
        { 0.5f * projection->M[0][0], 0.0f, 0.5f * projection->M[0][2] - 0.5f, 0.0f },
        { 0.0f, 0.5f * projection->M[1][1], 0.5f * projection->M[1][2] - 0.5f, 0.0f },
        { 0.0f, 0.0f, -1.0f, 0.0f },
// Store the values to convert a clip-Z to a linear depth in the unused matrix elements.
        { projection->M[2][2], projection->M[2][3], projection->M[3][2], 1.0f }
    } };
return tanAngleMatrix;
}

staticinline ovrMatrix4f ovrMatrix4f_TanAngleMatrixFromUnitSquare( const ovrMatrix4f * modelView )
{
/*
        // Take the inverse of the view matrix because the view matrix transforms the unit square
        // from world space into view space, while the matrix needed here is the one that transforms
        // the unit square from view space to world space.
        const ovrMatrix4f inv = ovrMatrix4f_Inverse( modelView );
        // This matrix calculates the projection onto the (-1, 1) X and Y axes of the unit square,
        // of the intersection of the vector (tanX, tanY, -1) with the plane described by the matrix
        // that transforms the unit square into world space.
        const ovrMatrix3f m =
        { {
            {   inv.M[0][0] * inv.M[2][3] - inv.M[0][3] * inv.M[2][0],
                inv.M[0][1] * inv.M[2][3] - inv.M[0][3] * inv.M[2][1],
                inv.M[0][2] * inv.M[2][3] - inv.M[0][3] * inv.M[2][2] },
            {   inv.M[1][0] * inv.M[2][3] - inv.M[1][3] * inv.M[2][0],
                inv.M[1][1] * inv.M[2][3] - inv.M[1][3] * inv.M[2][1],
                inv.M[1][2] * inv.M[2][3] - inv.M[1][3] * inv.M[2][2] },
            {   - inv.M[2][0],
                - inv.M[2][1],
                - inv.M[2][2] }
        } };
        // Flip the Y because textures have 0,0 = left-top as opposed to left-bottom.
        const ovrMatrix3f f = ovrMatrix3f_CreateScaling( 1.0f, -1.0f );
        const ovrMatrix3f s = ovrMatrix3f_CreateScaling( 0.5f, 0.5f );
        const ovrMatrix3f t = ovrMatrix3f_CreateTranslation( 0.5f, 0.5f );
        const ovrMatrix3f r0 = ovrMatrix3f_Multiply( &f, &m );
        const ovrMatrix3f r1 = ovrMatrix3f_Multiply( &s, &r0 );
        const ovrMatrix3f r2 = ovrMatrix3f_Multiply( &t, &r1 );
        return r2;
    */

const ovrMatrix4f inv = ovrMatrix4f_Inverse( modelView );
constfloat coef = ( inv.M[2][3] > 0.0f ) ? 1.0f : -1.0f;

    ovrMatrix4f m;
    m.M[0][0] = ( +0.5f * ( inv.M[0][0] * inv.M[2][3] - inv.M[0][3] * inv.M[2][0] ) - 0.5f * inv.M[2][0] ) * coef;
    m.M[0][1] = ( +0.5f * ( inv.M[0][1] * inv.M[2][3] - inv.M[0][3] * inv.M[2][1] ) - 0.5f * inv.M[2][1] ) * coef;
    m.M[0][2] = ( +0.5f * ( inv.M[0][2] * inv.M[2][3] - inv.M[0][3] * inv.M[2][2] ) - 0.5f * inv.M[2][2] ) * coef;
    m.M[0][3] = 0.0f;

    m.M[1][0] = ( -0.5f * ( inv.M[1][0] * inv.M[2][3] - inv.M[1][3] * inv.M[2][0] ) - 0.5f * inv.M[2][0] ) * coef;
    m.M[1][1] = ( -0.5f * ( inv.M[1][1] * inv.M[2][3] - inv.M[1][3] * inv.M[2][1] ) - 0.5f * inv.M[2][1] ) * coef;
    m.M[1][2] = ( -0.5f * ( inv.M[1][2] * inv.M[2][3] - inv.M[1][3] * inv.M[2][2] ) - 0.5f * inv.M[2][2] ) * coef;
    m.M[1][3] = 0.0f;

    m.M[2][0] = ( -inv.M[2][0] ) * coef;
    m.M[2][1] = ( -inv.M[2][1] ) * coef;
    m.M[2][2] = ( -inv.M[2][2] ) * coef;
    m.M[2][3] = 0.0f;

    m.M[3][0] = 0.0f;
    m.M[3][1] = 0.0f;
    m.M[3][2] = 0.0f;
    m.M[3][3] = 1.0f;
return m;
}

staticinline ovrMatrix4f ovrMatrix4f_TanAngleMatrixForCubeMap( const ovrMatrix4f * viewMatrix )
{
    ovrMatrix4f m = *viewMatrix;
// clear translation
for ( int i = 0; i < 3; i++ )
    {
        m.M[ i ][ 3 ] = 0.0f;
    }
return ovrMatrix4f_Inverse( &m );
}

staticinline ovrVector3f ovrVector3f_RotateAboutPivot( const ovrQuatf * rotation, const ovrVector3f * pivot, const ovrVector3f * point )
{
const ovrMatrix4f t0 = ovrMatrix4f_CreateTranslation( pivot->x, pivot->y, pivot->z );
const ovrMatrix4f r = ovrMatrix4f_CreateFromQuaternion( rotation );
const ovrMatrix4f t1 = ovrMatrix4f_CreateTranslation( -pivot->x, -pivot->y, -pivot->z );
const ovrMatrix4f c0 = ovrMatrix4f_Multiply( &t0, &r );
const ovrMatrix4f c1 = ovrMatrix4f_Multiply( &c0, &t1 );
const ovrVector4f v = { point->x, point->y, point->z, 1.0f };
const ovrVector4f v2 = ovrVector4f_MultiplyMatrix4f( &c1, &v );
const ovrVector3f v3 = { v2.x, v2.y, v2.z };
return v3;
}

//-----------------------------------------------------------------
// Default initialization helper functions.
//-----------------------------------------------------------------

staticinline ovrInitParms vrapi_DefaultInitParms( const ovrJava * java )
{
    ovrInitParms parms;
    memset( &parms, 0, sizeof( parms ) );

    parms.Type = VRAPI_STRUCTURE_TYPE_INIT_PARMS;
    parms.ProductVersion = VRAPI_PRODUCT_VERSION;
    parms.MajorVersion = VRAPI_MAJOR_VERSION;
    parms.MinorVersion = VRAPI_MINOR_VERSION;
    parms.PatchVersion = VRAPI_PATCH_VERSION;
    parms.GraphicsAPI = VRAPI_GRAPHICS_API_OPENGL_ES_2;
    parms.Java = *java;

return parms;
}


staticinline ovrModeParms vrapi_DefaultModeParms( const ovrJava * java )
{
    ovrModeParms parms;
    memset( &parms, 0, sizeof( parms ) );

    parms.Type = VRAPI_STRUCTURE_TYPE_MODE_PARMS;
    parms.Flags |= VRAPI_MODE_FLAG_ALLOW_POWER_SAVE;
    parms.Flags |= VRAPI_MODE_FLAG_RESET_WINDOW_FULLSCREEN;
    parms.Java = *java;

return parms;
}

staticinline ovrModeParmsVulkan vrapi_DefaultModeParmsVulkan( const ovrJava * java, unsignedlonglong synchronizationQueue )
{
    ovrModeParmsVulkan parms;
    memset( &parms, 0, sizeof( parms ) );

    parms.ModeParms = vrapi_DefaultModeParms( java );
    parms.ModeParms.Type = VRAPI_STRUCTURE_TYPE_MODE_PARMS_VULKAN;
    parms.SynchronizationQueue = synchronizationQueue;

return parms;
}

staticinline ovrPerformanceParms vrapi_DefaultPerformanceParms()
{
    ovrPerformanceParms parms;
    parms.CpuLevel = 2;
    parms.GpuLevel = 2;
    parms.MainThreadTid = 0;
    parms.RenderThreadTid = 0;
return parms;
}

typedefenum
{
    VRAPI_FRAME_INIT_DEFAULT            = 0,
    VRAPI_FRAME_INIT_BLACK              = 1,
    VRAPI_FRAME_INIT_BLACK_FLUSH        = 2,
    VRAPI_FRAME_INIT_BLACK_FINAL        = 3,
    VRAPI_FRAME_INIT_LOADING_ICON       = 4,
    VRAPI_FRAME_INIT_LOADING_ICON_FLUSH = 5,

// enum 6 used to be VRAPI_FRAME_INIT_MESSAGE

// enum 7 used to be VRAPI_FRAME_INIT_MESSAGE_FLUSH
} ovrFrameInit;

staticinline ovrFrameParms vrapi_DefaultFrameParms( const ovrJava * java, const ovrFrameInit init, constdouble currentTime,
                                                     ovrTextureSwapChain * textureSwapChain )
{
const ovrMatrix4f projectionMatrix = ovrMatrix4f_CreateProjectionFov( 90.0f, 90.0f, 0.0f, 0.0f, 0.1f, 0.0f );
const ovrMatrix4f texCoordsFromTanAngles = ovrMatrix4f_TanAngleMatrixFromProjection( &projectionMatrix );

    ovrFrameParms parms;
    memset( &parms, 0, sizeof( parms ) );

    parms.Type = VRAPI_STRUCTURE_TYPE_FRAME_PARMS;
for ( int layer = 0; layer < VRAPI_FRAME_LAYER_TYPE_MAX; layer++ )
    {
        parms.Layers[layer].ColorScale = 1.0f;
for ( int eye = 0; eye < VRAPI_FRAME_LAYER_EYE_MAX; eye++ )
        {
            parms.Layers[layer].Textures[eye].TexCoordsFromTanAngles = texCoordsFromTanAngles;
            parms.Layers[layer].Textures[eye].TextureRect.width = 1.0f;
            parms.Layers[layer].Textures[eye].TextureRect.height = 1.0f;
            parms.Layers[layer].Textures[eye].HeadPose.Pose.Orientation.w = 1.0f;
            parms.Layers[layer].Textures[eye].HeadPose.TimeInSeconds = currentTime;
        }
    }
    parms.LayerCount = 1;
    parms.SwapInterval = 1;
    parms.ExtraLatencyMode = VRAPI_EXTRA_LATENCY_MODE_OFF;
    parms.Reserved.M[0][0] = 1.0f;
    parms.Reserved.M[1][1] = 1.0f;
    parms.Reserved.M[2][2] = 1.0f;
    parms.Reserved.M[3][3] = 1.0f;
    parms.PerformanceParms = vrapi_DefaultPerformanceParms();
    parms.Java = *java;

    parms.Layers[0].SrcBlend = VRAPI_FRAME_LAYER_BLEND_ONE;
    parms.Layers[0].DstBlend = VRAPI_FRAME_LAYER_BLEND_ZERO;
    parms.Layers[0].Flags = 0;
    parms.Layers[0].SpinSpeed = 0.0f;
    parms.Layers[0].SpinScale = 1.0f;

    parms.Layers[1].SrcBlend = VRAPI_FRAME_LAYER_BLEND_SRC_ALPHA;
    parms.Layers[1].DstBlend = VRAPI_FRAME_LAYER_BLEND_ONE_MINUS_SRC_ALPHA;
    parms.Layers[1].Flags = 0;
    parms.Layers[1].SpinSpeed = 0.0f;
    parms.Layers[1].SpinScale = 1.0f;

switch ( init )
    {
case VRAPI_FRAME_INIT_DEFAULT:
        {
break;
        }
case VRAPI_FRAME_INIT_BLACK:
case VRAPI_FRAME_INIT_BLACK_FLUSH:
case VRAPI_FRAME_INIT_BLACK_FINAL:
        {
            parms.Layers[0].Flags = VRAPI_FRAME_LAYER_FLAG_INHIBIT_SRGB_FRAMEBUFFER;
// NOTE: When requesting a solid black frame, set ColorScale to 0.0f
            parms.Layers[0].ColorScale = 0.0f;
for ( int eye = 0; eye < VRAPI_FRAME_LAYER_EYE_MAX; eye++ )
            {
                parms.Layers[0].Textures[eye].ColorTextureSwapChain = (ovrTextureSwapChain *)VRAPI_DEFAULT_TEXTURE_SWAPCHAIN;
            }
break;
        }
case VRAPI_FRAME_INIT_LOADING_ICON:
case VRAPI_FRAME_INIT_LOADING_ICON_FLUSH:
        {
            parms.LayerCount = 2;
            parms.Layers[0].Flags = VRAPI_FRAME_LAYER_FLAG_INHIBIT_SRGB_FRAMEBUFFER;
            parms.Layers[1].Flags = VRAPI_FRAME_LAYER_FLAG_INHIBIT_SRGB_FRAMEBUFFER;
// NOTE: When requesting a solid black frame, set ColorScale to 0.0f
            parms.Layers[0].ColorScale = 0.0f;
            parms.Layers[1].Flags |= VRAPI_FRAME_LAYER_FLAG_SPIN;
            parms.Layers[1].SpinSpeed = 1.0f;       // rotation in radians per second
            parms.Layers[1].SpinScale = 16.0f;      // icon size factor smaller than fullscreen
for ( int eye = 0; eye < VRAPI_FRAME_LAYER_EYE_MAX; eye++ )
            {
                parms.Layers[0].Textures[eye].ColorTextureSwapChain = (ovrTextureSwapChain *)VRAPI_DEFAULT_TEXTURE_SWAPCHAIN;
                parms.Layers[1].Textures[eye].ColorTextureSwapChain = ( textureSwapChain != NULL ) ? textureSwapChain : (ovrTextureSwapChain *)VRAPI_DEFAULT_TEXTURE_SWAPCHAIN_LOADING_ICON;
            }
break;
        }
    }

if ( init == VRAPI_FRAME_INIT_BLACK_FLUSH || init == VRAPI_FRAME_INIT_LOADING_ICON_FLUSH )
    {
        parms.Flags |= VRAPI_FRAME_FLAG_FLUSH;
    }
if ( init == VRAPI_FRAME_INIT_BLACK_FINAL )
    {
        parms.Flags |= VRAPI_FRAME_FLAG_FLUSH | VRAPI_FRAME_FLAG_FINAL;
    }

return parms;
}

//-----------------------------------------------------------------
// Layer Types - default initialization.
//-----------------------------------------------------------------

staticinline ovrLayerProjection2 vrapi_DefaultLayerProjection2()
{
    ovrLayerProjection2 layer = {};

const ovrMatrix4f projectionMatrix          = ovrMatrix4f_CreateProjectionFov( 90.0f, 90.0f, 0.0f, 0.0f, 0.1f, 0.0f );
const ovrMatrix4f texCoordsFromTanAngles    = ovrMatrix4f_TanAngleMatrixFromProjection( &projectionMatrix );

    layer.Header.Type   = VRAPI_LAYER_TYPE_PROJECTION2;
    layer.Header.Flags  = 0;
    layer.Header.ColorScale.x   = 1.0f;
    layer.Header.ColorScale.y   = 1.0f;
    layer.Header.ColorScale.z   = 1.0f;
    layer.Header.ColorScale.w   = 1.0f;
    layer.Header.SrcBlend       = VRAPI_FRAME_LAYER_BLEND_ONE;
    layer.Header.DstBlend       = VRAPI_FRAME_LAYER_BLEND_ZERO;
    layer.Header.Reserved       = NULL;

    layer.HeadPose.Pose.Orientation.w = 1.0f;

for ( int i = 0; i < VRAPI_FRAME_LAYER_EYE_MAX; i++ )
    {
        layer.Textures[i].TexCoordsFromTanAngles        = texCoordsFromTanAngles;
        layer.Textures[i].TextureRect.x                 = 0.0f;
        layer.Textures[i].TextureRect.y                 = 0.0f;
        layer.Textures[i].TextureRect.width             = 1.0f;
        layer.Textures[i].TextureRect.height            = 1.0f;
    }

return layer;
}

staticinline ovrLayerProjection2 vrapi_DefaultLayerBlackProjection2()
{
    ovrLayerProjection2 layer = {};

    layer.Header.Type   = VRAPI_LAYER_TYPE_PROJECTION2;
    layer.Header.Flags  = 0;
// NOTE: When requesting a solid black frame, set ColorScale to { 0.0f, 0.0f, 0.0f, 0.0f }
    layer.Header.ColorScale.x   = 0.0f;
    layer.Header.ColorScale.y   = 0.0f;
    layer.Header.ColorScale.z   = 0.0f;
    layer.Header.ColorScale.w   = 0.0f;
    layer.Header.SrcBlend       = VRAPI_FRAME_LAYER_BLEND_ONE;
    layer.Header.DstBlend       = VRAPI_FRAME_LAYER_BLEND_ZERO;
    layer.Header.Reserved       = NULL;

    layer.HeadPose.Pose.Orientation.w = 1.0f;

for ( int eye = 0; eye < VRAPI_FRAME_LAYER_EYE_MAX; eye++ )
    {
        layer.Textures[eye].SwapChainIndex = 0;
        layer.Textures[eye].ColorSwapChain = (ovrTextureSwapChain *)VRAPI_DEFAULT_TEXTURE_SWAPCHAIN;
    }

return layer;
}

staticinline ovrLayerProjection2 vrapi_DefaultLayerSolidColorProjection2( const ovrVector4f * colorScale )
{
    ovrLayerProjection2 layer = {};

    layer.Header.Type   = VRAPI_LAYER_TYPE_PROJECTION2;
    layer.Header.Flags  = 0;
    layer.Header.ColorScale.x   = colorScale->x;
    layer.Header.ColorScale.y   = colorScale->y;
    layer.Header.ColorScale.z   = colorScale->z;
    layer.Header.ColorScale.w   = colorScale->w;
    layer.Header.SrcBlend       = VRAPI_FRAME_LAYER_BLEND_ONE;
    layer.Header.DstBlend       = VRAPI_FRAME_LAYER_BLEND_ZERO;
    layer.Header.Reserved       = NULL;

    layer.HeadPose.Pose.Orientation.w = 1.0f;

for ( int eye = 0; eye < VRAPI_FRAME_LAYER_EYE_MAX; eye++ )
    {
        layer.Textures[eye].SwapChainIndex = 0;
        layer.Textures[eye].ColorSwapChain = (ovrTextureSwapChain *)VRAPI_DEFAULT_TEXTURE_SWAPCHAIN;
    }

return layer;
}

staticinline ovrLayerCylinder2 vrapi_DefaultLayerCylinder2()
{
    ovrLayerCylinder2 layer = {};

const ovrMatrix4f projectionMatrix          = ovrMatrix4f_CreateProjectionFov( 90.0f, 90.0f, 0.0f, 0.0f, 0.1f, 0.0f );
const ovrMatrix4f texCoordsFromTanAngles    = ovrMatrix4f_TanAngleMatrixFromProjection( &projectionMatrix );

    layer.Header.Type   = VRAPI_LAYER_TYPE_CYLINDER2;
    layer.Header.Flags  = 0;
    layer.Header.ColorScale.x   = 1.0f;
    layer.Header.ColorScale.y   = 1.0f;
    layer.Header.ColorScale.z   = 1.0f;
    layer.Header.ColorScale.w   = 1.0f;
    layer.Header.SrcBlend       = VRAPI_FRAME_LAYER_BLEND_ONE;
    layer.Header.DstBlend       = VRAPI_FRAME_LAYER_BLEND_ZERO;
    layer.Header.Reserved       = NULL;

    layer.HeadPose.Pose.Orientation.w = 1.0f;

for ( int i = 0; i < VRAPI_FRAME_LAYER_EYE_MAX; i++ )
    {
        layer.Textures[i].TexCoordsFromTanAngles    = texCoordsFromTanAngles;
        layer.Textures[i].TextureRect.x         = 0.0f;
        layer.Textures[i].TextureRect.y         = 0.0f;
        layer.Textures[i].TextureRect.width     = 1.0f;
        layer.Textures[i].TextureRect.height    = 1.0f;
        layer.Textures[i].TextureMatrix.M[0][0] = 1.0f;
        layer.Textures[i].TextureMatrix.M[1][1] = 1.0f;
        layer.Textures[i].TextureMatrix.M[2][2] = 1.0f;
        layer.Textures[i].TextureMatrix.M[3][3] = 1.0f;
    }

return layer;
}

staticinline ovrLayerCube2 vrapi_DefaultLayerCube2()
{
    ovrLayerCube2 layer = {};

    layer.Header.Type   = VRAPI_LAYER_TYPE_CUBE2;
    layer.Header.Flags  = 0;
    layer.Header.ColorScale.x   = 1.0f;
    layer.Header.ColorScale.y   = 1.0f;
    layer.Header.ColorScale.z   = 1.0f;
    layer.Header.ColorScale.w   = 1.0f;
    layer.Header.SrcBlend       = VRAPI_FRAME_LAYER_BLEND_ONE;
    layer.Header.DstBlend       = VRAPI_FRAME_LAYER_BLEND_ZERO;
    layer.Header.Reserved       = NULL;

    layer.HeadPose.Pose.Orientation.w = 1.0f;
    layer.TexCoordsFromTanAngles = ovrMatrix4f_CreateIdentity();

    layer.Offset.x = 0.0f;
    layer.Offset.y = 0.0f;
    layer.Offset.z = 0.0f;

return layer;
}

staticinline ovrLayerEquirect2 vrapi_DefaultLayerEquirect2()
{
    ovrLayerEquirect2 layer = {};

    layer.Header.Type   = VRAPI_LAYER_TYPE_EQUIRECT2;
    layer.Header.Flags  = 0;
    layer.Header.ColorScale.x   = 1.0f;
    layer.Header.ColorScale.y   = 1.0f;
    layer.Header.ColorScale.z   = 1.0f;
    layer.Header.ColorScale.w   = 1.0f;
    layer.Header.SrcBlend       = VRAPI_FRAME_LAYER_BLEND_ONE;
    layer.Header.DstBlend       = VRAPI_FRAME_LAYER_BLEND_ZERO;
    layer.Header.Reserved       = NULL;

    layer.HeadPose.Pose.Orientation.w = 1.0f;
    layer.TexCoordsFromTanAngles = ovrMatrix4f_CreateIdentity();

for ( int i = 0; i < VRAPI_FRAME_LAYER_EYE_MAX; i++ )
    {
        layer.Textures[i].TextureRect.x         = 0.0f;
        layer.Textures[i].TextureRect.y         = 0.0f;
        layer.Textures[i].TextureRect.width     = 1.0f;
        layer.Textures[i].TextureRect.height    = 1.0f;
        layer.Textures[i].TextureMatrix.M[0][0] = 1.0f;
        layer.Textures[i].TextureMatrix.M[1][1] = 1.0f;
        layer.Textures[i].TextureMatrix.M[2][2] = 1.0f;
        layer.Textures[i].TextureMatrix.M[3][3] = 1.0f;
    }

return layer;
}

staticinline ovrLayerLoadingIcon2 vrapi_DefaultLayerLoadingIcon2()
{
    ovrLayerLoadingIcon2 layer = {};

    layer.Header.Type   = VRAPI_LAYER_TYPE_LOADING_ICON2;
    layer.Header.Flags  = 0;
    layer.Header.ColorScale.x   = 1.0f;
    layer.Header.ColorScale.y   = 1.0f;
    layer.Header.ColorScale.z   = 1.0f;
    layer.Header.ColorScale.w   = 1.0f;
    layer.Header.SrcBlend       = VRAPI_FRAME_LAYER_BLEND_SRC_ALPHA;
    layer.Header.DstBlend       = VRAPI_FRAME_LAYER_BLEND_ONE_MINUS_SRC_ALPHA;
    layer.Header.Reserved       = NULL;

    layer.SpinSpeed         = 1.0f;
    layer.SpinScale         = 16.0f;

    layer.ColorSwapChain    = (ovrTextureSwapChain *)VRAPI_DEFAULT_TEXTURE_SWAPCHAIN_LOADING_ICON;
    layer.SwapChainIndex    = 0;

return layer;
}


//-----------------------------------------------------------------
// Eye view matrix helper functions.
//-----------------------------------------------------------------

staticinlinefloat vrapi_GetInterpupillaryDistance( const ovrTracking2 * tracking2 )
{
const ovrMatrix4f leftView = tracking2->Eye[0].ViewMatrix;
const ovrMatrix4f rightView = tracking2->Eye[1].ViewMatrix;
const ovrVector3f delta = { rightView.M[0][3] - leftView.M[0][3], rightView.M[1][3] - leftView.M[1][3], rightView.M[2][3] - leftView.M[2][3] };
return sqrtf( delta.x * delta.x + delta.y * delta.y + delta.z * delta.z );
}

staticinlinefloat vrapi_GetEyeHeight( const ovrPosef * eyeLevelTrackingPose, const ovrPosef * currentTrackingPose )
{
return eyeLevelTrackingPose->Position.y - currentTrackingPose->Position.y;
}

staticinline ovrMatrix4f vrapi_GetTransformFromPose( const ovrPosef * pose )
{
const ovrMatrix4f rotation = ovrMatrix4f_CreateFromQuaternion( &pose->Orientation );
const ovrMatrix4f translation = ovrMatrix4f_CreateTranslation( pose->Position.x, pose->Position.y, pose->Position.z );
return ovrMatrix4f_Multiply( &translation, &rotation );
}

staticinline ovrMatrix4f vrapi_GetViewMatrixFromPose( const ovrPosef * pose )
{
const ovrMatrix4f transform = vrapi_GetTransformFromPose( pose );
return ovrMatrix4f_Inverse( &transform );
}

staticinline ovrMatrix4f vrapi_GetEyeViewMatrix(   const ovrMatrix4f * centerEyeViewMatrix,
constfloat interpupillaryDistance,
constint eye )
{
constfloat eyeOffset = ( eye ? -0.5f : 0.5f ) * interpupillaryDistance;
const ovrMatrix4f eyeOffsetMatrix = ovrMatrix4f_CreateTranslation( eyeOffset, 0.0f, 0.0f );
return ovrMatrix4f_Multiply( &eyeOffsetMatrix, centerEyeViewMatrix );
}

#endif  // OVR_VrApi_Helpers_h
The documentation for this file was generated from the following file: Include/VrApi_Helpers.h