forked from meruune/plugin_TouchLink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackingHandler.cpp
More file actions
275 lines (228 loc) · 8.58 KB
/
TrackingHandler.cpp
File metadata and controls
275 lines (228 loc) · 8.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "pch.h"
#include "TrackingHandler.h"
#include "TrackingHandler.g.cpp"
namespace winrt::DeviceHandler::implementation
{
void TrackingHandler::Update()
{
// Update joints' poses here
// Note: this is fired up every loop
// Run the update loop
if (initialized && statusResult == S_OK)
{
if (keepAlive && !ODTKRAstop && !is_ODTKRA_started)
{
ODTKRAThread = std::thread([this] { this->keepRiftAlive(); });
is_ODTKRA_started = true;
}
else if (!keepAlive && is_ODTKRA_started)
{
ODTKRAstop = true;
is_ODTKRA_started = false;
killODT();
ODTKRAThread.join();
ODTKRAstop = false;
}
guardian->Render();
const auto tracking_state = ovr_GetTrackingState(
guardian->mSession, ovr_GetTimeInSeconds() +
static_cast<float>(extraPrediction) * 0.001, ovrTrue);
// Grab controller poses
for (int i = 0; i <= 1; i++)
{
trackedJoints.at(i).Position = {
tracking_state.HandPoses[i].ThePose.Position.x,
tracking_state.HandPoses[i].ThePose.Position.y,
tracking_state.HandPoses[i].ThePose.Position.z
};
trackedJoints.at(i).Orientation = {
tracking_state.HandPoses[i].ThePose.Orientation.x,
tracking_state.HandPoses[i].ThePose.Orientation.y,
tracking_state.HandPoses[i].ThePose.Orientation.z,
tracking_state.HandPoses[i].ThePose.Orientation.w
};
trackedJoints.at(i).Velocity = {
tracking_state.HandPoses[i].LinearVelocity.x,
tracking_state.HandPoses[i].LinearVelocity.y,
tracking_state.HandPoses[i].LinearVelocity.z
};
trackedJoints.at(i).Acceleration = {
tracking_state.HandPoses[i].LinearAcceleration.x,
tracking_state.HandPoses[i].LinearAcceleration.y,
tracking_state.HandPoses[i].LinearAcceleration.z
};
trackedJoints.at(i).AngularVelocity = {
tracking_state.HandPoses[i].AngularVelocity.x,
tracking_state.HandPoses[i].AngularVelocity.y,
tracking_state.HandPoses[i].AngularVelocity.z
};
trackedJoints.at(i).AngularAcceleration = {
tracking_state.HandPoses[i].AngularAcceleration.x,
tracking_state.HandPoses[i].AngularAcceleration.y,
tracking_state.HandPoses[i].AngularAcceleration.z
};
}
for (size_t i = 0; i < guardian->vrObjects; i++)
{
auto deviceType = static_cast<ovrTrackedDeviceType>(ovrTrackedDevice_Object0 + i);
ovrPoseStatef ovr_pose;
ovr_GetDevicePoses(guardian->mSession, &deviceType, 1,
(ovr_GetTimeInSeconds() + (static_cast<float>(extraPrediction) * 0.001)),
&ovr_pose);
if ((ovr_pose.ThePose.Orientation.x != 0) && (ovr_pose.ThePose.Orientation.y != 0) && (ovr_pose.ThePose.
Orientation.z != 0))
{
trackedJoints.at(2).Position = {
ovr_pose.ThePose.Position.x,
ovr_pose.ThePose.Position.y,
ovr_pose.ThePose.Position.z
};
trackedJoints.at(2).Orientation = {
ovr_pose.ThePose.Orientation.x,
ovr_pose.ThePose.Orientation.y,
ovr_pose.ThePose.Orientation.z,
ovr_pose.ThePose.Orientation.w
};
trackedJoints.at(2).Velocity = {
ovr_pose.LinearVelocity.x,
ovr_pose.LinearVelocity.y,
ovr_pose.LinearVelocity.z
};
trackedJoints.at(2).Acceleration = {
ovr_pose.LinearAcceleration.x,
ovr_pose.LinearAcceleration.y,
ovr_pose.LinearAcceleration.z
};
trackedJoints.at(2).AngularVelocity = {
ovr_pose.AngularVelocity.x,
ovr_pose.AngularVelocity.y,
ovr_pose.AngularVelocity.z
};
trackedJoints.at(2).AngularAcceleration = {
ovr_pose.AngularAcceleration.x,
ovr_pose.AngularAcceleration.y,
ovr_pose.AngularAcceleration.z
};
}
}
frame++; // Hit the frame counter
}
}
int32_t TrackingHandler::Initialize()
{
// Shutdown if already initialized
if (initialized)
{
Log(L"Handler already initialized, shutting down prior to reinitialization!", 1);
this->Shutdown(); // Try shutting down before reinitializing
}
// Find out the size of the buffer required to store the value
DWORD dwBufSize = 0;
LONG lRetVal = RegGetValue(
HKEY_LOCAL_MACHINE,
L"SOFTWARE\\WOW6432Node\\Oculus VR, LLC\\Oculus",
L"Base",
RRF_RT_ANY,
nullptr,
nullptr,
&dwBufSize);
if (ERROR_SUCCESS != lRetVal || dwBufSize <= 0)
Log(L"ODT could not be found! Some things may refuse to work!", 2);
// If we're ok
else
{
std::wstring data;
data.resize(dwBufSize / sizeof(wchar_t));
RegGetValue(HKEY_LOCAL_MACHINE, L"SOFTWARE\\WOW6432Node\\Oculus VR, LLC\\Oculus",
L"Base", RRF_RT_ANY, nullptr, &data[0], &dwBufSize);
ODTPath = data + L"Support\\oculus-diagnostics\\";
}
// Create a new guardian instance
guardian = new(_aligned_malloc(sizeof(GuardianSystem), 16)) GuardianSystem(statusResult, Log);
// Assume success
statusResult = S_OK;
// Setup Oculus Stuff
guardian->start_ovr();
// Check the yield result
if (statusResult == S_OK)
{
// Not used anymore - joints are assigned in static context
/*for (size_t i = 0; i < guardian->vrObjects; i++)
trackedJoints.push_back(Joint{ .Name = std::format(L"VR Object {}", i + 1).c_str() });*/
}
// Mark the device as initialized
initialized = true;
// Return the result
return statusResult;
}
int32_t TrackingHandler::Shutdown()
{
initialized = false;
__try
{
if (keepAlive)
{
ODTKRAstop = true;
ODTKRAThread.join();
}
guardian->DIRECTX.ReleaseDevice();
ovr_Destroy(guardian->mSession);
ovr_Shutdown();
delete[] guardian;
return 0;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
[&, this]
{
Log(L"OVR shutdown failure!", 2);
statusResult = R_E_INIT_FAILED;
}();
return -1;
}
}
bool TrackingHandler::KeepAlive() const
{
return keepAlive;
}
void TrackingHandler::KeepAlive(bool value)
{
keepAlive = value;
}
bool TrackingHandler::ReduceRes() const
{
return resEnabled;
}
void TrackingHandler::ReduceRes(bool value)
{
resEnabled = value;
}
int32_t TrackingHandler::PredictionMs() const
{
return extraPrediction;
}
void TrackingHandler::PredictionMs(int32_t value)
{
extraPrediction = value;
}
bool TrackingHandler::IsInitialized() const
{
return initialized;
}
int32_t TrackingHandler::StatusResult() const
{
return statusResult;
}
event_token TrackingHandler::LogEvent(const Windows::Foundation::EventHandler<hstring>& handler)
{
return logEvent.add(handler);
}
void TrackingHandler::LogEvent(const event_token& token) noexcept
{
logEvent.remove(token);
}
com_array<Joint> TrackingHandler::TrackedJoints() const
{
return winrt::com_array<Joint>{trackedJoints};
}
}