Skip to content

Commit 556b540

Browse files
committed
TOVR 0.7
1 parent 6824551 commit 556b540

File tree

1 file changed

+61
-70
lines changed

1 file changed

+61
-70
lines changed

OpenVR/samples/driver_sample/driver_sample.cpp

Lines changed: 61 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,35 @@ typedef struct _Controller
8888
double Yaw;
8989
double Pitch;
9090
double Roll;
91-
WORD Buttons;
92-
BYTE Trigger;
93-
SHORT ThumbX;
94-
SHORT ThumbY;
91+
unsigned short Buttons;
92+
float Trigger;
93+
float AxisX;
94+
float AxisY;
9595
} TController, *PController;
9696

97-
typedef DWORD(__stdcall *_GetHMDData)(__out THMD* myHMD);
98-
typedef DWORD(__stdcall *_GetControllersData)(__out TController *myController, __out TController *myController2);
99-
typedef DWORD(__stdcall *_SetControllerData)(__in int dwIndex, __in WORD MotorSpeed);
97+
#define TOVR_SUCCESS 0
98+
#define TOVR_FAILURE 1
99+
100+
typedef DWORD(__stdcall *_GetHMDData)(__out THMD *HMD);
101+
typedef DWORD(__stdcall *_GetControllersData)(__out TController *FirstController, __out TController *SecondController);
102+
typedef DWORD(__stdcall *_SetControllerData)(__in int dwIndex, __in unsigned char MotorSpeed);
100103
typedef DWORD(__stdcall *_SetCentering)(__in int dwIndex);
101104

102105
_GetHMDData GetHMDData;
103106
_GetControllersData GetControllersData;
104107
_SetControllerData SetControllerData;
105108
_SetCentering SetCentering;
106109

107-
#define GRIPBTN 0x0001
108-
#define THUMBSTICKBTN 0x0002
109-
#define MENUBTN 0x0004
110-
#define SYSTEMBTN 0x0008
110+
#define GRIP_BTN 0x0001
111+
#define THUMB_BTN 0x0002
112+
#define A_BTN 0x0004
113+
#define B_BTN 0x0008
114+
#define MENU_BTN 0x0010
115+
#define SYS_BTN 0x0020
111116

112117
HMODULE hDll;
113-
THMD myHMD;
114-
TController myCtrl, myCtrl2;
118+
THMD MyHMD;
119+
TController MyCtrl, MyCtrl2;
115120

116121
bool HMDConnected = false, ctrlsConnected = false;
117122

@@ -232,7 +237,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
232237
if (GetHMDData == NULL) HMDConnected = false;
233238
if (SetCentering == NULL) HMDConnected = false;
234239

235-
if (GetControllersData != NULL && SetControllerData !=NULL && GetControllersData(&myCtrl, &myCtrl2) == 1)
240+
if (GetControllersData != NULL && SetControllerData !=NULL && GetControllersData(&MyCtrl, &MyCtrl2) == TOVR_SUCCESS)
236241
ctrlsConnected = true;
237242
}
238243
}
@@ -449,18 +454,18 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
449454
if ((GetAsyncKeyState(VK_NUMPAD5) & 0x8000) != 0 || ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0 && (GetAsyncKeyState(VK_MENU) & 0x8000) != 0 && (GetAsyncKeyState(82) & 0x8000) != 0))
450455
SetCentering(0);
451456

452-
GetHMDData(&myHMD);
457+
GetHMDData(&MyHMD);
453458

454459
//Set head tracking rotation
455-
pose.qRotation.w = cos(DegToRad(myHMD.Yaw) * 0.5) * cos(DegToRad(myHMD.Roll) * 0.5) * cos(DegToRad(myHMD.Pitch) * 0.5) + sin(DegToRad(myHMD.Yaw) * 0.5) * sin(DegToRad(myHMD.Roll) * 0.5) * sin(DegToRad(myHMD.Pitch) * 0.5);
456-
pose.qRotation.x = cos(DegToRad(myHMD.Yaw) * 0.5) * sin(DegToRad(myHMD.Roll) * 0.5) * cos(DegToRad(myHMD.Pitch) * 0.5) - sin(DegToRad(myHMD.Yaw) * 0.5) * cos(DegToRad(myHMD.Roll) * 0.5) * sin(DegToRad(myHMD.Pitch) * 0.5);
457-
pose.qRotation.y = cos(DegToRad(myHMD.Yaw) * 0.5) * cos(DegToRad(myHMD.Roll) * 0.5) * sin(DegToRad(myHMD.Pitch) * 0.5) + sin(DegToRad(myHMD.Yaw) * 0.5) * sin(DegToRad(myHMD.Roll) * 0.5) * cos(DegToRad(myHMD.Pitch) * 0.5);
458-
pose.qRotation.z = sin(DegToRad(myHMD.Yaw) * 0.5) * cos(DegToRad(myHMD.Roll) * 0.5) * cos(DegToRad(myHMD.Pitch) * 0.5) - cos(DegToRad(myHMD.Yaw) * 0.5) * sin(DegToRad(myHMD.Roll) * 0.5) * sin(DegToRad(myHMD.Pitch) * 0.5);
460+
pose.qRotation.w = cos(DegToRad(MyHMD.Yaw) * 0.5) * cos(DegToRad(MyHMD.Roll) * 0.5) * cos(DegToRad(MyHMD.Pitch) * 0.5) + sin(DegToRad(MyHMD.Yaw) * 0.5) * sin(DegToRad(MyHMD.Roll) * 0.5) * sin(DegToRad(MyHMD.Pitch) * 0.5);
461+
pose.qRotation.x = cos(DegToRad(MyHMD.Yaw) * 0.5) * sin(DegToRad(MyHMD.Roll) * 0.5) * cos(DegToRad(MyHMD.Pitch) * 0.5) - sin(DegToRad(MyHMD.Yaw) * 0.5) * cos(DegToRad(MyHMD.Roll) * 0.5) * sin(DegToRad(MyHMD.Pitch) * 0.5);
462+
pose.qRotation.y = cos(DegToRad(MyHMD.Yaw) * 0.5) * cos(DegToRad(MyHMD.Roll) * 0.5) * sin(DegToRad(MyHMD.Pitch) * 0.5) + sin(DegToRad(MyHMD.Yaw) * 0.5) * sin(DegToRad(MyHMD.Roll) * 0.5) * cos(DegToRad(MyHMD.Pitch) * 0.5);
463+
pose.qRotation.z = sin(DegToRad(MyHMD.Yaw) * 0.5) * cos(DegToRad(MyHMD.Roll) * 0.5) * cos(DegToRad(MyHMD.Pitch) * 0.5) - cos(DegToRad(MyHMD.Yaw) * 0.5) * sin(DegToRad(MyHMD.Roll) * 0.5) * sin(DegToRad(MyHMD.Pitch) * 0.5);
459464

460465
//Set position tracking
461-
pose.vecPosition[0] = myHMD.X;
462-
pose.vecPosition[1] = myHMD.Y;
463-
pose.vecPosition[2] = myHMD.Z;
466+
pose.vecPosition[0] = MyHMD.X;
467+
pose.vecPosition[1] = MyHMD.Y;
468+
pose.vecPosition[2] = MyHMD.Z;
464469
}
465470

466471
return pose;
@@ -653,45 +658,31 @@ class CSampleControllerDriver : public vr::ITrackedDeviceServerDriver
653658
//Controllers positions and rotations
654659
if (ControllerIndex == 1) {
655660

656-
pose.vecPosition[0] = myCtrl.X;
657-
pose.vecPosition[1] = myCtrl.Y;
658-
pose.vecPosition[2] = myCtrl.Z;
661+
pose.vecPosition[0] = MyCtrl.X;
662+
pose.vecPosition[1] = MyCtrl.Y;
663+
pose.vecPosition[2] = MyCtrl.Z;
659664

660665
//Convert yaw, pitch, roll to quaternion
661-
pose.qRotation.w = cos(DegToRad(myCtrl.Yaw) * 0.5) * cos(DegToRad(myCtrl.Roll) * 0.5) * cos(DegToRad(myCtrl.Pitch) * 0.5) + sin(DegToRad(myCtrl.Yaw) * 0.5) * sin(DegToRad(myCtrl.Roll) * 0.5) * sin(DegToRad(myCtrl.Pitch) * 0.5);
662-
pose.qRotation.x = cos(DegToRad(myCtrl.Yaw) * 0.5) * sin(DegToRad(myCtrl.Roll) * 0.5) * cos(DegToRad(myCtrl.Pitch) * 0.5) - sin(DegToRad(myCtrl.Yaw) * 0.5) * cos(DegToRad(myCtrl.Roll) * 0.5) * sin(DegToRad(myCtrl.Pitch) * 0.5);
663-
pose.qRotation.y = cos(DegToRad(myCtrl.Yaw) * 0.5) * cos(DegToRad(myCtrl.Roll) * 0.5) * sin(DegToRad(myCtrl.Pitch) * 0.5) + sin(DegToRad(myCtrl.Yaw) * 0.5) * sin(DegToRad(myCtrl.Roll) * 0.5) * cos(DegToRad(myCtrl.Pitch) * 0.5);
664-
pose.qRotation.z = sin(DegToRad(myCtrl.Yaw) * 0.5) * cos(DegToRad(myCtrl.Roll) * 0.5) * cos(DegToRad(myCtrl.Pitch) * 0.5) - cos(DegToRad(myCtrl.Yaw) * 0.5) * sin(DegToRad(myCtrl.Roll) * 0.5) * sin(DegToRad(myCtrl.Pitch) * 0.5);
666+
pose.qRotation.w = cos(DegToRad(MyCtrl.Yaw) * 0.5) * cos(DegToRad(MyCtrl.Roll) * 0.5) * cos(DegToRad(MyCtrl.Pitch) * 0.5) + sin(DegToRad(MyCtrl.Yaw) * 0.5) * sin(DegToRad(MyCtrl.Roll) * 0.5) * sin(DegToRad(MyCtrl.Pitch) * 0.5);
667+
pose.qRotation.x = cos(DegToRad(MyCtrl.Yaw) * 0.5) * sin(DegToRad(MyCtrl.Roll) * 0.5) * cos(DegToRad(MyCtrl.Pitch) * 0.5) - sin(DegToRad(MyCtrl.Yaw) * 0.5) * cos(DegToRad(MyCtrl.Roll) * 0.5) * sin(DegToRad(MyCtrl.Pitch) * 0.5);
668+
pose.qRotation.y = cos(DegToRad(MyCtrl.Yaw) * 0.5) * cos(DegToRad(MyCtrl.Roll) * 0.5) * sin(DegToRad(MyCtrl.Pitch) * 0.5) + sin(DegToRad(MyCtrl.Yaw) * 0.5) * sin(DegToRad(MyCtrl.Roll) * 0.5) * cos(DegToRad(MyCtrl.Pitch) * 0.5);
669+
pose.qRotation.z = sin(DegToRad(MyCtrl.Yaw) * 0.5) * cos(DegToRad(MyCtrl.Roll) * 0.5) * cos(DegToRad(MyCtrl.Pitch) * 0.5) - cos(DegToRad(MyCtrl.Yaw) * 0.5) * sin(DegToRad(MyCtrl.Roll) * 0.5) * sin(DegToRad(MyCtrl.Pitch) * 0.5);
665670

666671
} else {
667672
//Controller2
668-
pose.vecPosition[0] = myCtrl2.X;
669-
pose.vecPosition[1] = myCtrl2.Y;
670-
pose.vecPosition[2] = myCtrl2.Z;
671-
672-
pose.qRotation.w = cos(DegToRad(myCtrl2.Yaw) * 0.5) * cos(DegToRad(myCtrl2.Roll) * 0.5) * cos(DegToRad(myCtrl2.Pitch) * 0.5) + sin(DegToRad(myCtrl2.Yaw) * 0.5) * sin(DegToRad(myCtrl2.Roll) * 0.5) * sin(DegToRad(myCtrl2.Pitch) * 0.5);
673-
pose.qRotation.x = cos(DegToRad(myCtrl2.Yaw) * 0.5) * sin(DegToRad(myCtrl2.Roll) * 0.5) * cos(DegToRad(myCtrl2.Pitch) * 0.5) - sin(DegToRad(myCtrl2.Yaw) * 0.5) * cos(DegToRad(myCtrl2.Roll) * 0.5) * sin(DegToRad(myCtrl2.Pitch) * 0.5);
674-
pose.qRotation.y = cos(DegToRad(myCtrl2.Yaw) * 0.5) * cos(DegToRad(myCtrl2.Roll) * 0.5) * sin(DegToRad(myCtrl2.Pitch) * 0.5) + sin(DegToRad(myCtrl2.Yaw) * 0.5) * sin(DegToRad(myCtrl2.Roll) * 0.5) * cos(DegToRad(myCtrl2.Pitch) * 0.5);
675-
pose.qRotation.z = sin(DegToRad(myCtrl2.Yaw) * 0.5) * cos(DegToRad(myCtrl2.Roll) * 0.5) * cos(DegToRad(myCtrl2.Pitch) * 0.5) - cos(DegToRad(myCtrl2.Yaw) * 0.5) * sin(DegToRad(myCtrl2.Roll) * 0.5) * sin(DegToRad(myCtrl2.Pitch) * 0.5);
673+
pose.vecPosition[0] = MyCtrl2.X;
674+
pose.vecPosition[1] = MyCtrl2.Y;
675+
pose.vecPosition[2] = MyCtrl2.Z;
676+
677+
pose.qRotation.w = cos(DegToRad(MyCtrl2.Yaw) * 0.5) * cos(DegToRad(MyCtrl2.Roll) * 0.5) * cos(DegToRad(MyCtrl2.Pitch) * 0.5) + sin(DegToRad(MyCtrl2.Yaw) * 0.5) * sin(DegToRad(MyCtrl2.Roll) * 0.5) * sin(DegToRad(MyCtrl2.Pitch) * 0.5);
678+
pose.qRotation.x = cos(DegToRad(MyCtrl2.Yaw) * 0.5) * sin(DegToRad(MyCtrl2.Roll) * 0.5) * cos(DegToRad(MyCtrl2.Pitch) * 0.5) - sin(DegToRad(MyCtrl2.Yaw) * 0.5) * cos(DegToRad(MyCtrl2.Roll) * 0.5) * sin(DegToRad(MyCtrl2.Pitch) * 0.5);
679+
pose.qRotation.y = cos(DegToRad(MyCtrl2.Yaw) * 0.5) * cos(DegToRad(MyCtrl2.Roll) * 0.5) * sin(DegToRad(MyCtrl2.Pitch) * 0.5) + sin(DegToRad(MyCtrl2.Yaw) * 0.5) * sin(DegToRad(MyCtrl2.Roll) * 0.5) * cos(DegToRad(MyCtrl2.Pitch) * 0.5);
680+
pose.qRotation.z = sin(DegToRad(MyCtrl2.Yaw) * 0.5) * cos(DegToRad(MyCtrl2.Roll) * 0.5) * cos(DegToRad(MyCtrl2.Pitch) * 0.5) - cos(DegToRad(MyCtrl2.Yaw) * 0.5) * sin(DegToRad(MyCtrl2.Roll) * 0.5) * sin(DegToRad(MyCtrl2.Pitch) * 0.5);
676681
}
677682

678683
return pose;
679684
}
680685

681-
double ConvAxis(double n) {
682-
if (n > 1) {
683-
return 1;
684-
}
685-
else if (n < -1)
686-
{
687-
return -1;
688-
}
689-
else
690-
{
691-
return n;
692-
}
693-
}
694-
695686
void RunFrame()
696687
{
697688
#if defined( _WINDOWS )
@@ -701,44 +692,44 @@ class CSampleControllerDriver : public vr::ITrackedDeviceServerDriver
701692

702693
if (ControllerIndex == 1) {
703694

704-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[0], (myCtrl.Buttons & MENUBTN) != 0, 0); //Application Menu
705-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[1], (myCtrl.Buttons & GRIPBTN) != 0, 0); //Grip
706-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[2], (myCtrl.Buttons & SYSTEMBTN) != 0, 0); //System
707-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[3], (myCtrl.Buttons & THUMBSTICKBTN) != 0, 0); //Trackpad
695+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[0], (MyCtrl.Buttons & MENU_BTN) != 0, 0); //Application Menu
696+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[1], (MyCtrl.Buttons & GRIP_BTN) != 0, 0); //Grip
697+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[2], (MyCtrl.Buttons & SYS_BTN) != 0, 0); //System
698+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[3], (MyCtrl.Buttons & THUMB_BTN) != 0, 0); //Trackpad
708699

709-
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[0], ConvAxis(myCtrl.ThumbX * 0.00003051758), 0); //Trackpad x
710-
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[1], ConvAxis(myCtrl.ThumbY * 0.00003051758), 0); //Trackpad y
700+
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[0], MyCtrl.AxisX, 0); //Trackpad x
701+
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[1], MyCtrl.AxisY, 0); //Trackpad y
711702

712-
if (myCtrl.ThumbX != 0 || myCtrl.ThumbY != 0) {
703+
if (MyCtrl.AxisX != 0 || MyCtrl.AxisY != 0) {
713704
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[4], 1, 0); //Trackpad touch
714705
}
715706
else {
716707
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[4], 0, 0); //Trackpad touch
717708
}
718709

719-
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[2], ConvAxis(myCtrl.Trigger * 0.003921568627451), 0); //Trigger
710+
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[2], MyCtrl.Trigger, 0); //Trigger
720711
} else {
721712
//Controller2
722-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[0], (myCtrl2.Buttons & MENUBTN) != 0, 0); //Application Menu
723-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[1], (myCtrl2.Buttons & GRIPBTN) != 0, 0); //Grip
724-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[2], (myCtrl2.Buttons & SYSTEMBTN) != 0, 0); //System
725-
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[3], (myCtrl2.Buttons & THUMBSTICKBTN) != 0, 0); //Trackpad
713+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[0], (MyCtrl2.Buttons & MENU_BTN) != 0, 0); //Application Menu
714+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[1], (MyCtrl2.Buttons & GRIP_BTN) != 0, 0); //Grip
715+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[2], (MyCtrl2.Buttons & SYS_BTN) != 0, 0); //System
716+
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[3], (MyCtrl2.Buttons & THUMB_BTN) != 0, 0); //Trackpad
726717

727-
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[0], ConvAxis(myCtrl2.ThumbX * 0.00003051758), 0); //Trackpad x
728-
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[1], ConvAxis(myCtrl2.ThumbY * 0.00003051758), 0); //Trackpad y
718+
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[0], MyCtrl2.AxisX, 0); //Trackpad x
719+
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[1], MyCtrl2.AxisY, 0); //Trackpad y
729720

730-
if (myCtrl2.ThumbX != 0 || myCtrl2.ThumbY != 0) {
721+
if (MyCtrl2.AxisX != 0 || MyCtrl2.AxisY != 0) {
731722
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[4], 1, 0); //Trackpad touch
732723
}
733724
else {
734725
vr::VRDriverInput()->UpdateBooleanComponent(HButtons[4], 0, 0); //Trackpad touch
735726
}
736727

737-
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[2], ConvAxis(myCtrl2.Trigger * 0.003921568627451), 0); //Trigger
728+
vr::VRDriverInput()->UpdateScalarComponent(HAnalog[2], MyCtrl2.Trigger, 0); //Trigger
738729
}
739730

740731
//Centring
741-
if ((myCtrl.Buttons & SYSTEMBTN) && (myCtrl.Buttons & GRIPBTN) && (myCtrl2.Buttons & SYSTEMBTN) && (myCtrl2.Buttons & GRIPBTN))
732+
if ((MyCtrl.Buttons & SYS_BTN) && (MyCtrl.Buttons & GRIP_BTN) && (MyCtrl2.Buttons & SYS_BTN) && (MyCtrl2.Buttons & GRIP_BTN))
742733
{
743734
SetCentering(0);
744735
SetCentering(1);
@@ -761,7 +752,7 @@ class CSampleControllerDriver : public vr::ITrackedDeviceServerDriver
761752
{
762753
if ( vrEvent.data.hapticVibration.componentHandle == m_compHaptic )
763754
{
764-
SetControllerData(ControllerIndex, 21845);
755+
SetControllerData(ControllerIndex, 100);
765756
// This is where you would send a signal to your hardware to trigger actual haptic feedback
766757
}
767758
}
@@ -864,7 +855,7 @@ void CServerDriver_Sample::RunFrame()
864855
m_pNullHmdLatest->RunFrame();
865856
}
866857
if (ctrlsConnected) {
867-
GetControllersData(&myCtrl, &myCtrl2);
858+
GetControllersData(&MyCtrl, &MyCtrl2);
868859

869860
if (m_pController)
870861
{

0 commit comments

Comments
 (0)