-
Notifications
You must be signed in to change notification settings - Fork 5
/
ViveControllerManager.cpp
173 lines (148 loc) · 6.73 KB
/
ViveControllerManager.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
#include "ViveControllerManager.h"
#include "pathtools.h"
ViveControllerManager::ViveControllerManager(uint32_t index)
{
m_nDeviceIndex = index;
}
ViveControllerManager::~ViveControllerManager()
{
}
void ViveControllerManager::ConvertPoseMatrix(vr::HmdMatrix34_t matPose, ViveControllerStatus &controllerStatus) {
controllerStatus.poseMatrixCalibrated[0] = matPose.m[0][0];
controllerStatus.poseMatrixCalibrated[1] = matPose.m[1][0];
controllerStatus.poseMatrixCalibrated[2] = matPose.m[2][0];
controllerStatus.poseMatrixCalibrated[3] = 0;
controllerStatus.poseMatrixCalibrated[4] = matPose.m[0][1];
controllerStatus.poseMatrixCalibrated[5] = matPose.m[1][1];
controllerStatus.poseMatrixCalibrated[6] = matPose.m[2][1];
controllerStatus.poseMatrixCalibrated[7] = 0;
controllerStatus.poseMatrixCalibrated[8] = matPose.m[0][2];
controllerStatus.poseMatrixCalibrated[9] = matPose.m[1][2];
controllerStatus.poseMatrixCalibrated[10] = matPose.m[2][2];
controllerStatus.poseMatrixCalibrated[11] = 0;
controllerStatus.poseMatrixCalibrated[12] = matPose.m[0][3];
controllerStatus.poseMatrixCalibrated[13] = matPose.m[1][3];
controllerStatus.poseMatrixCalibrated[14] = matPose.m[2][3];
controllerStatus.poseMatrixCalibrated[15] = 1.0f;
m_PoseMatrix.set(controllerStatus.poseMatrixCalibrated);
}
std::string ViveControllerManager::GetStringProperty(vr::TrackedDeviceIndex_t unDevice, vr::TrackedDeviceProperty prop, vr::TrackedPropertyError *peError)
{
uint32_t unRequiredBufferLen = vr::VRSystem()->GetStringTrackedDeviceProperty(unDevice, prop, NULL, 0, peError);
if (unRequiredBufferLen == 0)
return "";
char *pchBuffer = new char[unRequiredBufferLen];
unRequiredBufferLen = vr::VRSystem()->GetStringTrackedDeviceProperty(unDevice, prop, pchBuffer, unRequiredBufferLen, peError);
std::string sResult = pchBuffer;
delete[] pchBuffer;
return sResult;
}
void ViveControllerManager::UpdateControllerStatus()
{
// Process SteamVR action state
// UpdateActionState is called each frame to update the state of the actions themselves. The application
// controls which action sets are active with the provided array of VRActiveActionSet_t structs.
vr::VRActiveActionSet_t actionSet = { 0 };
actionSet.ulActionSet = actionSetDriver;
actionSet.ulRestrictedToDevice;
vr::EVRInputError eError = vr::VRInput()->UpdateActionState(&actionSet, sizeof(actionSet), 1);
if (eError != vr::VRInputError_None) {
std::cout << "UpdateActionState error: " << eError << std::endl;
return;
}
vr::InputPoseActionData_t poseData;
float predictedSecondsFromNow = 0;
eError = vr::VRInput()->GetPoseActionDataForNextFrame(deviceHandles.actionPose, vr::TrackingUniverseStanding, &poseData, sizeof(poseData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && poseData.bActive && poseData.pose.bPoseIsValid)
{
ConvertPoseMatrix(poseData.pose.mDeviceToAbsoluteTracking, ControllerStatus);
vr::InputOriginInfo_t originInfo;
if (vr::VRInput()->GetOriginTrackedDeviceInfo(poseData.activeOrigin, &originInfo, sizeof(originInfo)) == vr::VRInputError_None
&& originInfo.trackedDeviceIndex != vr::k_unTrackedDeviceIndexInvalid)
{
std::string sRenderModelName = GetStringProperty(m_nDeviceIndex, vr::Prop_RenderModelName_String);
if (sRenderModelName != RenderModelName) {
RenderModelName = sRenderModelName;
LoadRenderModel(RenderModelName);
}
}
}
else {
return;
}
//Trackpad position
vr::InputAnalogActionData_t analogData;
eError = vr::VRInput()->GetAnalogActionData(deviceHandles.actionTrackpadAnalogValue, &analogData, sizeof(analogData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && analogData.bActive)
{
ControllerStatus.trackpadX = analogData.x;
ControllerStatus.trackpadY = analogData.y;
}
//Trackpad Touch
vr::InputDigitalActionData_t digitalData;
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionTrackpadDigitalTouch, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && digitalData.bActive)
{
ControllerStatus.trackpadTouch = digitalData.bState;
}
//Trackpad Click
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionTrackpadDigitalClick, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && digitalData.bActive)
{
ControllerStatus.trackpadClick = digitalData.bState;
}
//Trigger Value
vr::InputAnalogActionData_t triggerData;
eError = vr::VRInput()->GetAnalogActionData(deviceHandles.actionPrimaryAnalogValue, &triggerData, sizeof(triggerData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && triggerData.bActive)
{
ControllerStatus.triggerValue = triggerData.x;
}
//Trigger Touch
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionPrimaryDigitalTouch, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && digitalData.bActive)
{
ControllerStatus.triggerTouch = digitalData.bState;
}
//Trigger Click
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionPrimaryDigitalClick, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && digitalData.bActive)
{
ControllerStatus.triggerClick = digitalData.bState;
}
//Grip Click
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionGripDigitalClick, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && digitalData.bActive)
{
ControllerStatus.grip_click = digitalData.bState;
}
//Grip Touch
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionGripDigitalTouch, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && digitalData.bActive)
{
ControllerStatus.grip_touch = digitalData.bState;
}
//Application Menu
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionApplicationMenuDigitalClick, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None && digitalData.bActive)
{
ControllerStatus.applicationMenu = digitalData.bState;
}
//System button
eError = vr::VRInput()->GetDigitalActionData(deviceHandles.actionSystemDigitalClick, &digitalData, sizeof(digitalData), deviceHandles.sourceHandle);
if (eError == vr::VRInputError_None)
{
ControllerStatus.system = digitalData.bState;
}
UpdateButtonHighlights();
}
void ViveControllerManager::UpdateButtonHighlights()
{
if (m_ModelComponents.size() > 0) {
UpdateButton({ "trigger" }, ControllerStatus.triggerValue);
UpdateButton({ "lgrip" }, ControllerStatus.grip_click);
UpdateButton({ "rgrip" }, ControllerStatus.grip_click);
UpdateButton({ "button" }, ControllerStatus.applicationMenu);
UpdateButton({ "trackpad_touch" }, ControllerStatus.trackpadTouch);
}
}