forked from LibreVR/Revive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
REV_CAPI.cpp
1065 lines (859 loc) · 33.9 KB
/
REV_CAPI.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
#include "OVR_CAPI.h"
#include "OVR_Version.h"
#include "Extras/OVR_Math.h"
#include "openvr.h"
#include "MinHook.h"
#include <DXGI.h>
#include <d3d11.h>
#include <Xinput.h>
#include "REV_Assert.h"
#include "REV_Common.h"
#include "REV_Error.h"
#include "REV_Math.h"
#define REV_SETTINGS_SECTION "revive"
typedef DWORD(__stdcall* _XInputGetState)(DWORD dwUserIndex, XINPUT_STATE* pState);
typedef DWORD(__stdcall* _XInputSetState)(DWORD dwUserIndex, XINPUT_VIBRATION* pVibration);
HMODULE g_hXInputLib;
_XInputGetState g_pXInputGetState;
_XInputSetState g_pXInputSetState;
vr::EVRInitError g_InitError = vr::VRInitError_None;
vr::IVRSystem* g_VRSystem = nullptr;
char* g_StringBuffer = nullptr;
OVR_PUBLIC_FUNCTION(ovrResult) ovr_Initialize(const ovrInitParams* params)
{
MH_QueueDisableHook(GetProcAddress);
MH_QueueDisableHook(LoadLibraryW);
MH_QueueDisableHook(OpenEventW);
MH_ApplyQueued();
g_hXInputLib = LoadLibraryW(L"xinput1_3.dll");
if (!g_hXInputLib)
return ovrError_LibLoad;
g_pXInputGetState = (_XInputGetState)GetProcAddress(g_hXInputLib, "XInputGetState");
if (!g_pXInputGetState)
return ovrError_LibLoad;
g_pXInputSetState = (_XInputSetState)GetProcAddress(g_hXInputLib, "XInputSetState");
if (!g_pXInputSetState)
return ovrError_LibLoad;
g_VRSystem = vr::VR_Init(&g_InitError, vr::VRApplication_Scene);
MH_QueueEnableHook(LoadLibraryW);
MH_QueueEnableHook(OpenEventW);
MH_ApplyQueued();
return REV_InitErrorToOvrError(g_InitError);
}
OVR_PUBLIC_FUNCTION(void) ovr_Shutdown()
{
// Delete the global string property buffer.
delete g_StringBuffer;
g_StringBuffer = nullptr;
if (g_hXInputLib)
FreeLibrary(g_hXInputLib);
vr::VR_Shutdown();
}
OVR_PUBLIC_FUNCTION(void) ovr_GetLastErrorInfo(ovrErrorInfo* errorInfo)
{
if (!errorInfo)
return;
const char* error = VR_GetVRInitErrorAsEnglishDescription(g_InitError);
strncpy_s(errorInfo->ErrorString, error, sizeof(ovrErrorInfo::ErrorString));
errorInfo->Result = REV_InitErrorToOvrError(g_InitError);
}
OVR_PUBLIC_FUNCTION(const char*) ovr_GetVersionString()
{
return OVR_VERSION_STRING;
}
OVR_PUBLIC_FUNCTION(int) ovr_TraceMessage(int level, const char* message) { return 0; /* Debugging feature */ }
OVR_PUBLIC_FUNCTION(ovrHmdDesc) ovr_GetHmdDesc(ovrSession session)
{
ovrHmdDesc desc;
desc.Type = ovrHmd_CV1;
// Get HMD name
g_VRSystem->GetStringTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_TrackingSystemName_String, desc.ProductName, 64);
g_VRSystem->GetStringTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_ManufacturerName_String, desc.Manufacturer, 64);
// TODO: Get HID information
desc.VendorId = 0;
desc.ProductId = 0;
// Get serial number
g_VRSystem->GetStringTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_SerialNumber_String, desc.SerialNumber, 24);
// TODO: Get firmware version
desc.FirmwareMajor = 0;
desc.FirmwareMinor = 0;
// Get capabilities
desc.AvailableHmdCaps = 0;
desc.DefaultHmdCaps = 0;
desc.AvailableTrackingCaps = ovrTrackingCap_Orientation | ovrTrackingCap_Position;
if (!g_VRSystem->GetBoolTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_WillDriftInYaw_Bool))
desc.AvailableTrackingCaps |= ovrTrackingCap_MagYawCorrection;
desc.DefaultTrackingCaps = ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position;
// Get field-of-view
for (int i = 0; i < ovrEye_Count; i++)
{
ovrFovPort eye;
g_VRSystem->GetProjectionRaw((vr::EVREye)i, &eye.LeftTan, &eye.RightTan, &eye.DownTan, &eye.UpTan);
eye.LeftTan *= -1.0f;
eye.DownTan *= -1.0f;
desc.DefaultEyeFov[i] = eye;
desc.MaxEyeFov[i] = eye;
}
// Get display properties
g_VRSystem->GetRecommendedRenderTargetSize((uint32_t*)&desc.Resolution.w, (uint32_t*)&desc.Resolution.h);
desc.Resolution.w *= 2; // Both eye ports
desc.DisplayRefreshRate = g_VRSystem->GetFloatTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_DisplayFrequency_Float);
return desc;
}
OVR_PUBLIC_FUNCTION(unsigned int) ovr_GetTrackerCount(ovrSession session)
{
return g_VRSystem->GetSortedTrackedDeviceIndicesOfClass(vr::TrackedDeviceClass_TrackingReference, nullptr, 0);
}
OVR_PUBLIC_FUNCTION(ovrTrackerDesc) ovr_GetTrackerDesc(ovrSession session, unsigned int trackerDescIndex)
{
// Get the index for this tracker.
vr::TrackedDeviceIndex_t trackers[vr::k_unMaxTrackedDeviceCount];
g_VRSystem->GetSortedTrackedDeviceIndicesOfClass(vr::TrackedDeviceClass_TrackingReference, trackers, vr::k_unMaxTrackedDeviceCount);
vr::TrackedDeviceIndex_t index = trackers[trackerDescIndex];
// Fill the descriptor.
ovrTrackerDesc desc;
// Calculate field-of-view.
float left = g_VRSystem->GetFloatTrackedDeviceProperty(index, vr::Prop_FieldOfViewLeftDegrees_Float);
float right = g_VRSystem->GetFloatTrackedDeviceProperty(index, vr::Prop_FieldOfViewRightDegrees_Float);
float top = g_VRSystem->GetFloatTrackedDeviceProperty(index, vr::Prop_FieldOfViewTopDegrees_Float);
float bottom = g_VRSystem->GetFloatTrackedDeviceProperty(index, vr::Prop_FieldOfViewBottomDegrees_Float);
desc.FrustumHFovInRadians = OVR::DegreeToRad(left + right);
desc.FrustumVFovInRadians = OVR::DegreeToRad(top + bottom);
// Because the base stations are oriented differently we need to invert the tracking frustum.
desc.FrustumNearZInMeters = -g_VRSystem->GetFloatTrackedDeviceProperty(index, vr::Prop_TrackingRangeMaximumMeters_Float);
desc.FrustumFarZInMeters = -g_VRSystem->GetFloatTrackedDeviceProperty(index, vr::Prop_TrackingRangeMinimumMeters_Float);
return desc;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_Create(ovrSession* pSession, ovrGraphicsLuid* pLuid)
{
if (!pSession)
return ovrError_InvalidParameter;
// Initialize the opaque pointer with our own OpenVR-specific struct
ovrSession session = new struct ovrHmdStruct();
memset(session->ColorTexture, 0, sizeof(ovrHmdStruct::ColorTexture));
session->ThumbStick[ovrHand_Left] = true;
// Get the compositor interface
session->compositor = (vr::IVRCompositor*)VR_GetGenericInterface(vr::IVRCompositor_Version, &g_InitError);
if (g_InitError != vr::VRInitError_None)
return REV_InitErrorToOvrError(g_InitError);
// Get the settings interface
session->settings = (vr::IVRSettings*)VR_GetGenericInterface(vr::IVRSettings_Version, &g_InitError);
if (g_InitError != vr::VRInitError_None)
return REV_InitErrorToOvrError(g_InitError);
// Get the overlay interface
session->overlay = (vr::IVROverlay*)VR_GetGenericInterface(vr::IVROverlay_Version, &g_InitError);
if (g_InitError != vr::VRInitError_None)
return REV_InitErrorToOvrError(g_InitError);
// Apply settings
session->ThumbStickRange = session->settings->GetFloat(REV_SETTINGS_SECTION, "ThumbStickRange", 0.8f);
// Get the LUID for the adapter
int32_t index;
g_VRSystem->GetDXGIOutputInfo(&index);
if (index == -1)
index = 0;
// Create the DXGI factory
IDXGIFactory* pFactory;
HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory));
if (FAILED(hr))
return ovrError_IncompatibleGPU;
IDXGIAdapter* pAdapter;
hr = pFactory->EnumAdapters(index, &pAdapter);
if (FAILED(hr))
return ovrError_MismatchedAdapters;
DXGI_ADAPTER_DESC desc;
hr = pAdapter->GetDesc(&desc);
if (FAILED(hr))
return ovrError_MismatchedAdapters;
// Copy the LUID into the structure
memcpy(pLuid, &desc.AdapterLuid, sizeof(LUID));
// Cleanup and return
pFactory->Release();
pAdapter->Release();
*pSession = session;
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(void) ovr_Destroy(ovrSession session)
{
delete session;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_GetSessionStatus(ovrSession session, ovrSessionStatus* sessionStatus)
{
if (!sessionStatus)
return ovrError_InvalidParameter;
// Fill in the status
sessionStatus->IsVisible = session->compositor->CanRenderScene();
sessionStatus->HmdPresent = g_VRSystem->IsTrackedDeviceConnected(vr::k_unTrackedDeviceIndex_Hmd);
sessionStatus->HmdMounted = sessionStatus->HmdPresent;
sessionStatus->DisplayLost = false;
sessionStatus->ShouldQuit = false;
sessionStatus->ShouldRecenter = false;
// Check for quit event
// TODO: Should we poll for events here?
/*vr::VREvent_t ev;
while (g_VRSystem->PollNextEvent(&ev, sizeof(vr::VREvent_t)))
{
if (ev.eventType == vr::VREvent_Quit)
{
sessionStatus->ShouldQuit = true;
g_VRSystem->AcknowledgeQuit_Exiting();
}
}*/
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_SetTrackingOriginType(ovrSession session, ovrTrackingOrigin origin)
{
if (!session)
return ovrError_InvalidSession;
// Both enums match exactly, so we can just cast them
session->compositor->SetTrackingSpace((vr::ETrackingUniverseOrigin)origin);
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(ovrTrackingOrigin) ovr_GetTrackingOriginType(ovrSession session)
{
if (!session)
return ovrTrackingOrigin_EyeLevel;
// Both enums match exactly, so we can just cast them
return (ovrTrackingOrigin)session->compositor->GetTrackingSpace();
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_RecenterTrackingOrigin(ovrSession session)
{
if (!session)
return ovrError_InvalidSession;
// When an Oculus game recenters the tracking origin it is implied that the tracking origin
// should now be seated.
session->compositor->SetTrackingSpace(vr::TrackingUniverseSeated);
g_VRSystem->ResetSeatedZeroPose();
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(void) ovr_ClearShouldRecenterFlag(ovrSession session) { /* No such flag, do nothing */ }
OVR_PUBLIC_FUNCTION(ovrTrackingState) ovr_GetTrackingState(ovrSession session, double absTime, ovrBool latencyMarker)
{
ovrTrackingState state = { 0 };
if (!session)
return state;
// Gain focus for the compositor
float time = (float)ovr_GetTimeInSeconds();
// Get the absolute tracking poses
vr::TrackedDevicePose_t* poses = session->poses;
// Convert the head pose
state.HeadPose = REV_TrackedDevicePoseToOVRPose(poses[vr::k_unTrackedDeviceIndex_Hmd], time);
state.StatusFlags = REV_TrackedDevicePoseToOVRStatusFlags(poses[vr::k_unTrackedDeviceIndex_Hmd]);
// Convert the hand poses
vr::TrackedDeviceIndex_t hands[] = { g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_LeftHand),
g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_RightHand) };
for (int i = 0; i < ovrHand_Count; i++)
{
vr::TrackedDeviceIndex_t deviceIndex = hands[i];
if (deviceIndex == vr::k_unTrackedDeviceIndexInvalid)
{
state.HandPoses[i].ThePose = OVR::Posef::Identity();
continue;
}
state.HandPoses[i] = REV_TrackedDevicePoseToOVRPose(poses[deviceIndex], time);
state.HandStatusFlags[i] = REV_TrackedDevicePoseToOVRStatusFlags(poses[deviceIndex]);
}
OVR::Matrix4f origin = REV_HmdMatrixToOVRMatrix(g_VRSystem->GetSeatedZeroPoseToStandingAbsoluteTrackingPose());
// The calibrated origin should be the location of the seated origin relative to the absolute tracking space.
// It currently describes the location of the absolute origin relative to the seated origin, so we have to invert it.
origin.Invert();
state.CalibratedOrigin.Orientation = OVR::Quatf(origin);
state.CalibratedOrigin.Position = origin.GetTranslation();
return state;
}
OVR_PUBLIC_FUNCTION(ovrTrackerPose) ovr_GetTrackerPose(ovrSession session, unsigned int trackerPoseIndex)
{
ovrTrackerPose pose = { 0 };
if (!session)
return pose;
// Get the index for this tracker.
vr::TrackedDeviceIndex_t trackers[vr::k_unMaxTrackedDeviceCount] = { vr::k_unTrackedDeviceIndexInvalid };
g_VRSystem->GetSortedTrackedDeviceIndicesOfClass(vr::TrackedDeviceClass_TrackingReference, trackers, vr::k_unMaxTrackedDeviceCount);
vr::TrackedDeviceIndex_t index = trackers[trackerPoseIndex];
// Set the flags
pose.TrackerFlags = 0;
if (index != vr::k_unTrackedDeviceIndexInvalid)
{
if (session->poses[index].bDeviceIsConnected)
pose.TrackerFlags |= ovrTracker_Connected;
if (session->poses[index].bPoseIsValid)
pose.TrackerFlags |= ovrTracker_PoseTracked;
}
// Convert the pose
OVR::Matrix4f matrix;
if (index != vr::k_unTrackedDeviceIndexInvalid && session->poses[index].bPoseIsValid)
matrix = REV_HmdMatrixToOVRMatrix(session->poses[index].mDeviceToAbsoluteTracking);
OVR::Quatf quat = OVR::Quatf(matrix);
pose.Pose.Orientation = quat;
pose.Pose.Position = matrix.GetTranslation();
// Level the pose
float yaw;
quat.GetYawPitchRoll(&yaw, nullptr, nullptr);
pose.LeveledPose.Orientation = OVR::Quatf(OVR::Axis_Y, yaw);
pose.LeveledPose.Position = matrix.GetTranslation();
return pose;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_GetInputState(ovrSession session, ovrControllerType controllerType, ovrInputState* inputState)
{
if (!session)
return ovrError_InvalidSession;
if (!inputState)
return ovrError_InvalidParameter;
memset(inputState, 0, sizeof(ovrInputState));
inputState->TimeInSeconds = ovr_GetTimeInSeconds();
if (controllerType & ovrControllerType_Touch)
{
// Get controller indices.
vr::TrackedDeviceIndex_t hands[] = { g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_LeftHand),
g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_RightHand) };
// Only if both controllers are assigned, then the touch controller is connected.
if (REV_IsTouchConnected(hands))
{
for (int i = 0; i < ovrHand_Count; i++)
{
if (hands[i] == vr::k_unTrackedDeviceIndexInvalid)
continue;
vr::VRControllerState_t state;
g_VRSystem->GetControllerState(hands[i], &state);
unsigned int buttons = 0, touches = 0;
bool isLeft = (i == ovrHand_Left);
if (state.ulButtonPressed & vr::ButtonMaskFromId(vr::k_EButton_ApplicationMenu))
{
if (!session->MenuWasPressed[i])
session->ThumbStick[i] = !session->ThumbStick[i];
session->MenuWasPressed[i] = true;
}
else
{
session->MenuWasPressed[i] = false;
}
if (state.ulButtonPressed & vr::ButtonMaskFromId(vr::k_EButton_SteamVR_Trigger))
buttons |= isLeft ? ovrButton_LShoulder : ovrButton_RShoulder;
if (state.ulButtonPressed & vr::ButtonMaskFromId(vr::k_EButton_Grip))
inputState->HandTrigger[i] = 1.0f;
if (state.ulButtonTouched & vr::ButtonMaskFromId(vr::k_EButton_SteamVR_Touchpad))
touches |= isLeft ? ovrTouch_LThumb : ovrTouch_RThumb;
if (state.ulButtonTouched & vr::ButtonMaskFromId(vr::k_EButton_SteamVR_Trigger))
touches |= isLeft ? ovrTouch_LIndexTrigger : ovrTouch_RIndexTrigger;
// Convert the axes
for (int j = 0; j < vr::k_unControllerStateAxisCount; j++)
{
vr::ETrackedDeviceProperty prop = (vr::ETrackedDeviceProperty)(vr::Prop_Axis0Type_Int32 + j);
vr::EVRControllerAxisType type = (vr::EVRControllerAxisType)g_VRSystem->GetInt32TrackedDeviceProperty(hands[i], prop);
vr::VRControllerAxis_t axis = state.rAxis[j];
if (type == vr::k_eControllerAxis_TrackPad)
{
if (session->ThumbStick[i])
{
// Map the touchpad to the thumbstick with a slightly smaller range
float x = axis.x / session->ThumbStickRange;
float y = axis.y / session->ThumbStickRange;
if (x > 1.0f) x = 1.0f;
if (y > 1.0f) y = 1.0f;
inputState->Thumbstick[i].x = x;
inputState->Thumbstick[i].y = y;
}
if (state.ulButtonPressed & vr::ButtonMaskFromId(vr::k_EButton_SteamVR_Touchpad))
{
if (session->ThumbStick[i])
{
buttons |= isLeft ? ovrButton_LThumb : ovrButton_RThumb;
}
else
{
if (axis.y < axis.x) {
if (axis.y < -axis.x)
buttons |= ovrButton_A;
else
buttons |= ovrButton_B;
}
else {
if (axis.y < -axis.x)
buttons |= ovrButton_X;
else
buttons |= ovrButton_Y;
}
}
}
}
if (type == vr::k_eControllerAxis_Trigger)
inputState->IndexTrigger[i] = axis.x;
}
// Commit buttons/touches, count pressed buttons as touches.
inputState->Buttons |= buttons;
inputState->Touches |= touches | buttons;
}
inputState->ControllerType = ovrControllerType_Touch;
}
}
if (controllerType & ovrControllerType_Remote)
{
// Get controller indices.
vr::TrackedDeviceIndex_t hands[] = { g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_LeftHand),
g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_RightHand) };
if (!REV_IsTouchConnected(hands))
{
for (int i = 0; i < ovrHand_Count; i++)
{
if (hands[i] == vr::k_unTrackedDeviceIndexInvalid)
continue;
vr::VRControllerState_t state;
g_VRSystem->GetControllerState(hands[i], &state);
if (state.ulButtonPressed & vr::ButtonMaskFromId(vr::k_EButton_ApplicationMenu))
inputState->Buttons |= ovrButton_Back;
// Convert the axes
for (int j = 0; j < vr::k_unControllerStateAxisCount; j++)
{
vr::ETrackedDeviceProperty prop = (vr::ETrackedDeviceProperty)(vr::Prop_Axis0Type_Int32 + j);
vr::EVRControllerAxisType type = (vr::EVRControllerAxisType)g_VRSystem->GetInt32TrackedDeviceProperty(hands[i], prop);
vr::VRControllerAxis_t axis = state.rAxis[j];
if (type == vr::k_eControllerAxis_TrackPad)
{
if (state.ulButtonPressed & vr::ButtonMaskFromId(vr::k_EButton_SteamVR_Touchpad))
{
float magnitude = sqrt(axis.x*axis.x + axis.y*axis.y);
if (magnitude < 0.5f)
{
inputState->Buttons |= ovrButton_Enter;
}
else
{
if (axis.y < axis.x) {
if (axis.y < -axis.x)
inputState->Buttons |= ovrButton_Down;
else
inputState->Buttons |= ovrButton_Right;
}
else {
if (axis.y < -axis.x)
inputState->Buttons |= ovrButton_Left;
else
inputState->Buttons |= ovrButton_Up;
}
}
}
}
}
inputState->ControllerType = ovrControllerType_Remote;
}
}
}
if (controllerType & ovrControllerType_XBox)
{
// Use XInput for Xbox controllers.
XINPUT_STATE state;
if (g_pXInputGetState(0, &state) == ERROR_SUCCESS)
{
// Convert the buttons
WORD buttons = state.Gamepad.wButtons;
if (buttons & XINPUT_GAMEPAD_DPAD_UP)
inputState->Buttons |= ovrButton_Up;
if (buttons & XINPUT_GAMEPAD_DPAD_DOWN)
inputState->Buttons |= ovrButton_Down;
if (buttons & XINPUT_GAMEPAD_DPAD_LEFT)
inputState->Buttons |= ovrButton_Left;
if (buttons & XINPUT_GAMEPAD_DPAD_RIGHT)
inputState->Buttons |= ovrButton_Right;
if (buttons & XINPUT_GAMEPAD_START)
inputState->Buttons |= ovrButton_Enter;
if (buttons & XINPUT_GAMEPAD_BACK)
inputState->Buttons |= ovrButton_Back;
if (buttons & XINPUT_GAMEPAD_LEFT_THUMB)
inputState->Buttons |= ovrButton_LThumb;
if (buttons & XINPUT_GAMEPAD_RIGHT_THUMB)
inputState->Buttons |= ovrButton_RThumb;
if (buttons & XINPUT_GAMEPAD_LEFT_SHOULDER)
inputState->Buttons |= ovrButton_LShoulder;
if (buttons & XINPUT_GAMEPAD_RIGHT_SHOULDER)
inputState->Buttons |= ovrButton_RShoulder;
if (buttons & XINPUT_GAMEPAD_A)
inputState->Buttons |= ovrButton_A;
if (buttons & XINPUT_GAMEPAD_B)
inputState->Buttons |= ovrButton_B;
if (buttons & XINPUT_GAMEPAD_X)
inputState->Buttons |= ovrButton_X;
if (buttons & XINPUT_GAMEPAD_Y)
inputState->Buttons |= ovrButton_Y;
// Convert the axes
SHORT deadzones[] = { XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE };
for (int i = 0; i < ovrHand_Count; i++)
{
float X, Y, trigger;
if (i == ovrHand_Left)
{
X = state.Gamepad.sThumbLX;
Y = state.Gamepad.sThumbLY;
trigger = state.Gamepad.bLeftTrigger;
}
if (i == ovrHand_Right)
{
X = state.Gamepad.sThumbRX;
Y = state.Gamepad.sThumbRY;
trigger = state.Gamepad.bRightTrigger;
}
//determine how far the controller is pushed
float magnitude = sqrt(X*X + Y*Y);
//determine the direction the controller is pushed
float normalizedX = X / magnitude;
float normalizedY = Y / magnitude;
//check if the controller is outside a circular dead zone
if (magnitude > deadzones[i])
{
//clip the magnitude at its expected maximum value
if (magnitude > 32767) magnitude = 32767;
//adjust magnitude relative to the end of the dead zone
magnitude -= deadzones[i];
//optionally normalize the magnitude with respect to its expected range
//giving a magnitude value of 0.0 to 1.0
float normalizedMagnitude = magnitude / (32767 - deadzones[i]);
inputState->Thumbstick[i].x = normalizedMagnitude * normalizedX;
inputState->Thumbstick[i].y = normalizedMagnitude * normalizedY;
}
if (trigger > XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
{
//clip the magnitude at its expected maximum value
if (trigger > 255) trigger = 255;
//adjust magnitude relative to the end of the dead zone
trigger -= XINPUT_GAMEPAD_TRIGGER_THRESHOLD;
//optionally normalize the magnitude with respect to its expected range
//giving a magnitude value of 0.0 to 1.0
float normalizedTrigger = trigger / (255 - XINPUT_GAMEPAD_TRIGGER_THRESHOLD);
inputState->IndexTrigger[i] = normalizedTrigger;
}
}
// Set the controller as connected.
inputState->ControllerType = ovrControllerType_XBox;
}
}
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(unsigned int) ovr_GetConnectedControllerTypes(ovrSession session)
{
unsigned int types = 0;
// Check for Vive controllers
vr::TrackedDeviceIndex_t hands[] = { g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_LeftHand),
g_VRSystem->GetTrackedDeviceIndexForControllerRole(vr::TrackedControllerRole_RightHand) };
// If both controllers are assigned, it's a touch controller. If not, it's a remote.
if (REV_IsTouchConnected(hands))
{
if (g_VRSystem->IsTrackedDeviceConnected(hands[ovrHand_Left]))
types |= ovrControllerType_LTouch;
if (g_VRSystem->IsTrackedDeviceConnected(hands[ovrHand_Right]))
types |= ovrControllerType_RTouch;
}
else
types |= ovrControllerType_Remote;
// Check for Xbox controller
XINPUT_STATE input;
if (g_pXInputGetState(0, &input) == ERROR_SUCCESS)
{
types |= ovrControllerType_XBox;
}
return types;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_SetControllerVibration(ovrSession session, ovrControllerType controllerType, float frequency, float amplitude)
{
// TODO: Disable the rumbler after a nominal amount of time.
// TODO: Implement Oculus Touch support.
if (controllerType == ovrControllerType_XBox)
{
XINPUT_VIBRATION vibration;
ZeroMemory(&vibration, sizeof(XINPUT_VIBRATION));
if (frequency > 0.0f)
{
// The right motor is the high-frequency motor, the left motor is the low-frequency motor.
if (frequency > 0.5f)
vibration.wRightMotorSpeed = WORD(65535.0f * amplitude);
else
vibration.wLeftMotorSpeed = WORD(65535.0f * amplitude);
}
g_pXInputSetState(0, &vibration);
return ovrSuccess;
}
return ovrError_DeviceUnavailable;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_GetTextureSwapChainLength(ovrSession session, ovrTextureSwapChain chain, int* out_Length)
{
if (!chain)
return ovrError_InvalidParameter;
*out_Length = chain->length;
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_GetTextureSwapChainCurrentIndex(ovrSession session, ovrTextureSwapChain chain, int* out_Index)
{
if (!chain)
return ovrError_InvalidParameter;
*out_Index = chain->index;
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_GetTextureSwapChainDesc(ovrSession session, ovrTextureSwapChain chain, ovrTextureSwapChainDesc* out_Desc)
{
if (!chain)
return ovrError_InvalidParameter;
out_Desc = &chain->desc;
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_CommitTextureSwapChain(ovrSession session, ovrTextureSwapChain chain)
{
if (!chain)
return ovrError_InvalidParameter;
chain->current = chain->texture[chain->index];
chain->index++;
chain->index %= chain->length;
return ovrSuccess;
}
OVR_PUBLIC_FUNCTION(void) ovr_DestroyTextureSwapChain(ovrSession session, ovrTextureSwapChain chain)
{
if (!chain)
return;
if (chain->texture[0].eType == vr::API_DirectX)
{
for (int i = 0; i < chain->length; i++)
((ID3D11Texture2D*)chain->texture[i].handle)->Release();
}
delete chain;
}
OVR_PUBLIC_FUNCTION(void) ovr_DestroyMirrorTexture(ovrSession session, ovrMirrorTexture mirrorTexture)
{
if (!mirrorTexture)
return;
if (mirrorTexture->texture.eType == vr::API_DirectX)
((ID3D11Texture2D*)mirrorTexture->texture.handle)->Release();
delete mirrorTexture;
}
OVR_PUBLIC_FUNCTION(ovrSizei) ovr_GetFovTextureSize(ovrSession session, ovrEyeType eye, ovrFovPort fov, float pixelsPerDisplayPixel)
{
ovrSizei size;
g_VRSystem->GetRecommendedRenderTargetSize((uint32_t*)&size.w, (uint32_t*)&size.h);
float left, right, top, bottom;
g_VRSystem->GetProjectionRaw((vr::EVREye)eye, &left, &right, &top, &bottom);
float uMin = 0.5f + 0.5f * left / fov.LeftTan;
float uMax = 0.5f + 0.5f * right / fov.RightTan;
float vMin = 0.5f - 0.5f * bottom / fov.DownTan;
float vMax = 0.5f - 0.5f * top / fov.UpTan;
// Grow the recommended size to account for the overlapping fov
size.w = int(size.w / (uMax - uMin));
size.h = int(size.h / (vMax - vMin));
return size;
}
OVR_PUBLIC_FUNCTION(ovrEyeRenderDesc) ovr_GetRenderDesc(ovrSession session, ovrEyeType eyeType, ovrFovPort fov)
{
ovrEyeRenderDesc desc;
desc.Eye = eyeType;
desc.Fov = fov;
OVR::Matrix4f HmdToEyeMatrix = REV_HmdMatrixToOVRMatrix(g_VRSystem->GetEyeToHeadTransform((vr::EVREye)eyeType));
float WidthTan = fov.LeftTan + fov.RightTan;
float HeightTan = fov.UpTan + fov.DownTan;
ovrSizei size;
g_VRSystem->GetRecommendedRenderTargetSize((uint32_t*)&size.w, (uint32_t*)&size.h);
desc.DistortedViewport = OVR::Recti(eyeType == ovrEye_Right ? size.w : 0, 0, size.w, size.h);
desc.PixelsPerTanAngleAtCenter = OVR::Vector2f(size.w / WidthTan, size.h / HeightTan);
desc.HmdToEyeOffset = HmdToEyeMatrix.GetTranslation();
return desc;
}
OVR_PUBLIC_FUNCTION(ovrResult) ovr_SubmitFrame(ovrSession session, long long frameIndex, const ovrViewScaleDesc* viewScaleDesc,
ovrLayerHeader const * const * layerPtrList, unsigned int layerCount)
{
// TODO: Implement scaling through ApplyTransform().
if (!session)
return ovrError_InvalidSession;
if (layerCount == 0 || !layerPtrList)
return ovrError_InvalidParameter;
// Other layers are interpreted as overlays.
for (size_t i = 0; i < ovrMaxLayerCount; i++)
{
char keyName[vr::k_unVROverlayMaxKeyLength];
snprintf(keyName, vr::k_unVROverlayMaxKeyLength, "Revive_%d", i);
// Look if the overlay already exists.
vr::VROverlayHandle_t overlay;
vr::EVROverlayError err = session->overlay->FindOverlay(keyName, &overlay);
// If this layer is defined in the list, show it. If not, hide the layer if it exists.
if (i < layerCount)
{
// Overlays are assumed to be monoscopic quads.
if (layerPtrList[i]->Type != ovrLayerType_Quad)
continue;
// Create a new overlay if it doesn't exist.
if (err == vr::VROverlayError_UnknownOverlay)
{
char title[vr::k_unVROverlayMaxNameLength];
snprintf(title, vr::k_unVROverlayMaxNameLength, "Revive Layer %d", i);
session->overlay->CreateOverlay(keyName, title, &overlay);
}
// A layer was added, but not defined, hide it.
if (layerPtrList[i] == nullptr)
{
session->overlay->HideOverlay(overlay);
continue;
}
ovrLayerQuad* layer = (ovrLayerQuad*)layerPtrList[i];
// Set the high quality overlay.
// FIXME: Why are High quality overlays headlocked in OpenVR?
//if (layer->Header.Flags & ovrLayerFlag_HighQuality)
// session->overlay->SetHighQualityOverlay(overlay);
// Transform the overlay.
vr::HmdMatrix34_t transform = REV_OvrPoseToHmdMatrix(layer->QuadPoseCenter);
session->overlay->SetOverlayWidthInMeters(overlay, layer->QuadSize.x);
if (layer->Header.Flags & ovrLayerFlag_HeadLocked)
session->overlay->SetOverlayTransformTrackedDeviceRelative(overlay, vr::k_unTrackedDeviceIndex_Hmd, &transform);
else
session->overlay->SetOverlayTransformAbsolute(overlay, session->compositor->GetTrackingSpace(), &transform);
// Set the texture and show the overlay.
ovrTextureSwapChain chain = layer->ColorTexture;
vr::VRTextureBounds_t bounds = REV_ViewportToTextureBounds(layer->Viewport, layer->ColorTexture, layer->Header.Flags);
session->overlay->SetOverlayTextureBounds(overlay, &bounds);
session->overlay->SetOverlayTexture(overlay, &chain->current);
// TODO: Handle overlay errors.
session->overlay->ShowOverlay(overlay);
}
else
{
if (err == vr::VROverlayError_UnknownOverlay)
break;
// Hide all overlays no longer visible.
session->overlay->HideOverlay(overlay);
}
}
// The first layer is assumed to be the application scene.
vr::EVRCompositorError err = vr::VRCompositorError_None;
if (layerPtrList[0]->Type == ovrLayerType_EyeFov)
{
ovrLayerEyeFov* sceneLayer = (ovrLayerEyeFov*)layerPtrList[0];
// Submit the scene layer.
for (int i = 0; i < ovrEye_Count; i++)
{
ovrTextureSwapChain chain = sceneLayer->ColorTexture[i];
session->ColorTexture[i] = chain;
vr::VRTextureBounds_t bounds = REV_ViewportToTextureBounds(sceneLayer->Viewport[i], sceneLayer->ColorTexture[i], sceneLayer->Header.Flags);
float left, right, top, bottom;
g_VRSystem->GetProjectionRaw((vr::EVREye)i, &left, &right, &top, &bottom);
// Shrink the bounds to account for the overlapping fov
ovrFovPort fov = sceneLayer->Fov[i];
float uMin = 0.5f + 0.5f * left / fov.LeftTan;
float uMax = 0.5f + 0.5f * right / fov.RightTan;
float vMin = 0.5f - 0.5f * bottom / fov.DownTan;
float vMax = 0.5f - 0.5f * top / fov.UpTan;
// Combine the fov bounds with the viewport bounds
bounds.uMin += uMin * bounds.uMax;
bounds.uMax *= uMax;
bounds.vMin += vMin * bounds.vMax;
bounds.vMax *= vMax;
err = session->compositor->Submit((vr::EVREye)i, &chain->current, &bounds);
if (err != vr::VRCompositorError_None)
break;
}
}
// Call WaitGetPoses() to do some cleanup from the previous frame.
session->compositor->WaitGetPoses(session->poses, vr::k_unMaxTrackedDeviceCount, session->gamePoses, vr::k_unMaxTrackedDeviceCount);
return REV_CompositorErrorToOvrError(err);
}
OVR_PUBLIC_FUNCTION(double) ovr_GetPredictedDisplayTime(ovrSession session, long long frameIndex)
{
if (!session)
return ovrError_InvalidSession;
float fDisplayFrequency = g_VRSystem->GetFloatTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_DisplayFrequency_Float);
float fVsyncToPhotons = g_VRSystem->GetFloatTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_SecondsFromVsyncToPhotons_Float);
return double(frameIndex) / fDisplayFrequency + fVsyncToPhotons;
}
OVR_PUBLIC_FUNCTION(double) ovr_GetTimeInSeconds()
{
float fDisplayFrequency = g_VRSystem->GetFloatTrackedDeviceProperty(vr::k_unTrackedDeviceIndex_Hmd, vr::Prop_DisplayFrequency_Float);
// Get time in seconds
float fSecondsSinceLastVsync;
uint64_t unFrame;
g_VRSystem->GetTimeSinceLastVsync(&fSecondsSinceLastVsync, &unFrame);
return double(unFrame) / fDisplayFrequency + fSecondsSinceLastVsync;
}
OVR_PUBLIC_FUNCTION(ovrBool) ovr_GetBool(ovrSession session, const char* propertyName, ovrBool defaultVal)
{
if (!session)
return ovrFalse;
return session->settings->GetBool(REV_SETTINGS_SECTION, propertyName, !!defaultVal);
}
OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetBool(ovrSession session, const char* propertyName, ovrBool value)
{
if (!session)
return ovrFalse;
vr::EVRSettingsError error;
session->settings->SetBool(REV_SETTINGS_SECTION, propertyName, !!value, &error);
session->settings->Sync();
return error == vr::VRSettingsError_None;
}
OVR_PUBLIC_FUNCTION(int) ovr_GetInt(ovrSession session, const char* propertyName, int defaultVal)
{
if (!session)
return 0;
return session->settings->GetInt32(REV_SETTINGS_SECTION, propertyName, defaultVal);
}
OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetInt(ovrSession session, const char* propertyName, int value)
{
if (!session)
return ovrFalse;
vr::EVRSettingsError error;
session->settings->SetInt32(REV_SETTINGS_SECTION, propertyName, value, &error);
session->settings->Sync();
return error == vr::VRSettingsError_None;
}
OVR_PUBLIC_FUNCTION(float) ovr_GetFloat(ovrSession session, const char* propertyName, float defaultVal)
{
if (!session)
return 0.0f;
return session->settings->GetFloat(REV_SETTINGS_SECTION, propertyName, defaultVal);
}
OVR_PUBLIC_FUNCTION(ovrBool) ovr_SetFloat(ovrSession session, const char* propertyName, float value)
{
if (!session)
return ovrFalse;
vr::EVRSettingsError error;
session->settings->SetFloat(REV_SETTINGS_SECTION, propertyName, value, &error);
session->settings->Sync();
return error == vr::VRSettingsError_None;