Showing the changes to libraries/input-plugins/src/input-plugins/ViveControllerManager.cpp (1 of 20 files). Show all
Unified · Split
| @@ -22,6 +22,7 @@ · 21 unmodified lines | |||
| 22 | #include "UserActivityLogger.h" | 22 | #include "UserActivityLogger.h" |
| 23 | 23 | ||
| 24 | extern vr::IVRSystem *_hmd; | 24 | extern vr::IVRSystem *_hmd; |
| 25 | extern int hmdRefCount; | ||
| 25 | extern vr::TrackedDevicePose_t _trackedDevicePose[vr::k_unMaxTrackedDeviceCount]; | 26 | extern vr::TrackedDevicePose_t _trackedDevicePose[vr::k_unMaxTrackedDeviceCount]; |
| 26 | extern mat4 _trackedDevicePoseMat4[vr::k_unMaxTrackedDeviceCount]; | 27 | extern mat4 _trackedDevicePoseMat4[vr::k_unMaxTrackedDeviceCount]; |
| 27 | 28 | ||
| @@ -43,15 +44,8 @@ const QString CONTROLLER_MODEL_STRING = "vr_controller_05_wireless_b"; · 15 unmodified lines | |||
| 43 | 44 | ||
| 44 | const QString ViveControllerManager::NAME = "OpenVR"; | 45 | const QString ViveControllerManager::NAME = "OpenVR"; |
| 45 | 46 | ||
| 46 | ViveControllerManager& ViveControllerManager::getInstance() { | ||
| 47 | static ViveControllerManager sharedInstance; | ||
| 48 | return sharedInstance; | ||
| 49 | } | ||
| 50 | |||
| 51 | ViveControllerManager::ViveControllerManager() : | 47 | ViveControllerManager::ViveControllerManager() : |
| 52 | InputDevice("SteamVR Controller"), | 48 | InputDevice("SteamVR Controller"), |
| 53 | _isInitialized(false), | ||
| 54 | _isEnabled(true), | ||
| 55 | _trackedControllers(0), | 49 | _trackedControllers(0), |
| 56 | _modelLoaded(false), | 50 | _modelLoaded(false), |
| 57 | _leftHandRenderID(0), | 51 | _leftHandRenderID(0), |
| @@ -60,19 +54,12 @@ ViveControllerManager::ViveControllerManager() : · 2 unmodified lines | |||
| 60 | 54 | ||
| 61 | } | 55 | } |
| 62 | 56 | ||
| 63 | ViveControllerManager::~ViveControllerManager() { | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | bool ViveControllerManager::isSupported() const { | 57 | bool ViveControllerManager::isSupported() const { |
| 68 | return vr::VR_IsHmdPresent(); | 58 | return vr::VR_IsHmdPresent(); |
| 69 | } | 59 | } |
| 70 | 60 | ||
| 71 | void ViveControllerManager::deinit() { | 61 | void ViveControllerManager::activate(PluginContainer* container) { |
| 72 | // TODO: deinit openvr? avoid conflicts with display plugin | 62 | hmdRefCount++; |
| 73 | } | ||
| 74 | |||
| 75 | void ViveControllerManager::init() { | ||
| 76 | if (!_hmd) { | 63 | if (!_hmd) { |
| 77 | vr::HmdError eError = vr::HmdError_None; | 64 | vr::HmdError eError = vr::HmdError_None; |
| 78 | _hmd = vr::VR_Init(&eError); | 65 | _hmd = vr::VR_Init(&eError); |
| @@ -125,8 +112,16 @@ void ViveControllerManager::init() { · 46 unmodified lines | |||
| 125 | 112 | ||
| 126 | _modelLoaded = true; | 113 | _modelLoaded = true; |
| 127 | } | 114 | } |
| 115 | } | ||
| 116 | |||
| 117 | void ViveControllerManager::deactivate() { | ||
| 118 | hmdRefCount--; | ||
| 128 | 119 | ||
| 129 | _isInitialized = true; | 120 | if (hmdRefCount == 0 && _hmd) { |
| 121 | vr::VR_Shutdown(); | ||
| 122 | _hmd = nullptr; | ||
| 123 | } | ||
| 124 | _poseStateMap.clear(); | ||
| 130 | } | 125 | } |
| 131 | 126 | ||
| 132 | void ViveControllerManager::updateRendering(RenderArgs* args, render::ScenePointer scene, render::PendingChanges pendingChanges) { | 127 | void ViveControllerManager::updateRendering(RenderArgs* args, render::ScenePointer scene, render::PendingChanges pendingChanges) { |
| @@ -192,68 +187,68 @@ void ViveControllerManager::renderHand(UserInputMapper::PoseValue pose, gpu::Bat · 59 unmodified lines | |||
| 192 | } | 187 | } |
| 193 | 188 | ||
| 194 | void ViveControllerManager::update(float deltaTime) { | 189 | void ViveControllerManager::update(float deltaTime) { |
| 190 | // TODO: This shouldn't be necessary | ||
| 195 | if (!_hmd) { | 191 | if (!_hmd) { |
| 196 | return; | 192 | return; |
| 197 | } | 193 | } |
| 194 | |||
| 198 | _buttonPressedMap.clear(); | 195 | _buttonPressedMap.clear(); |
| 199 | if (_isInitialized && _isEnabled) { | ||
| 200 | 196 | ||
| 201 | PerformanceTimer perfTimer("ViveControllerManager::update"); | 197 | PerformanceTimer perfTimer("ViveControllerManager::update"); |
| 202 | 198 | ||
| 203 | int numTrackedControllers = 0; | 199 | int numTrackedControllers = 0; |
| 204 | 200 | ||
| 205 | for (vr::TrackedDeviceIndex_t device = vr::k_unTrackedDeviceIndex_Hmd + 1; | 201 | for (vr::TrackedDeviceIndex_t device = vr::k_unTrackedDeviceIndex_Hmd + 1; |
| 206 | device < vr::k_unMaxTrackedDeviceCount && numTrackedControllers < 2; ++device) { | 202 | device < vr::k_unMaxTrackedDeviceCount && numTrackedControllers < 2; ++device) { |
| 207 | 203 | ||
| 208 | if (!_hmd->IsTrackedDeviceConnected(device)) { | 204 | if (!_hmd->IsTrackedDeviceConnected(device)) { |
| 209 | continue; | 205 | continue; |
| 210 | } | 206 | } |
| 211 | 207 | ||
| 212 | if(_hmd->GetTrackedDeviceClass(device) != vr::TrackedDeviceClass_Controller) { | 208 | if(_hmd->GetTrackedDeviceClass(device) != vr::TrackedDeviceClass_Controller) { |
| 213 | continue; | 209 | continue; |
| 214 | } | 210 | } |
| 215 | 211 | ||
| 216 | if (!_trackedDevicePose[device].bPoseIsValid) { | 212 | if (!_trackedDevicePose[device].bPoseIsValid) { |
| 217 | continue; | 213 | continue; |
| 218 | } | 214 | } |
| 219 | 215 | ||
| 220 | numTrackedControllers++; | 216 | numTrackedControllers++; |
| 221 | 217 | ||
| 222 | const mat4& mat = _trackedDevicePoseMat4[device]; | 218 | const mat4& mat = _trackedDevicePoseMat4[device]; |
| 223 | 219 | ||
| 224 | handlePoseEvent(mat, numTrackedControllers - 1); | 220 | handlePoseEvent(mat, numTrackedControllers - 1); |
| 225 | 221 | ||
| 226 | // handle inputs | 222 | // handle inputs |
| 227 | vr::VRControllerState_t controllerState = vr::VRControllerState_t(); | 223 | vr::VRControllerState_t controllerState = vr::VRControllerState_t(); |
| 228 | if (_hmd->GetControllerState(device, &controllerState)) { | 224 | if (_hmd->GetControllerState(device, &controllerState)) { |
| 229 | //qDebug() << (numTrackedControllers == 1 ? "Left: " : "Right: "); | 225 | //qDebug() << (numTrackedControllers == 1 ? "Left: " : "Right: "); |
| 230 | //qDebug() << "Trackpad: " << controllerState.rAxis[0].x << " " << controllerState.rAxis[0].y; | 226 | //qDebug() << "Trackpad: " << controllerState.rAxis[0].x << " " << controllerState.rAxis[0].y; |
| 231 | //qDebug() << "Trigger: " << controllerState.rAxis[1].x << " " << controllerState.rAxis[1].y; | 227 | //qDebug() << "Trigger: " << controllerState.rAxis[1].x << " " << controllerState.rAxis[1].y; |
| 232 | handleButtonEvent(controllerState.ulButtonPressed, numTrackedControllers - 1); | 228 | handleButtonEvent(controllerState.ulButtonPressed, numTrackedControllers - 1); |
| 233 | for (int i = 0; i < vr::k_unControllerStateAxisCount; i++) { | 229 | for (int i = 0; i < vr::k_unControllerStateAxisCount; i++) { |
| 234 | handleAxisEvent(Axis(i), controllerState.rAxis[i].x, controllerState.rAxis[i].y, numTrackedControllers - 1); | 230 | handleAxisEvent(Axis(i), controllerState.rAxis[i].x, controllerState.rAxis[i].y, numTrackedControllers - 1); |
| 235 | } | ||
| 236 | } | 231 | } |
| 237 | } | 232 | } |
| 233 | } | ||
| 238 | 234 | ||
| 239 | auto userInputMapper = DependencyManager::get<UserInputMapper>(); | 235 | auto userInputMapper = DependencyManager::get<UserInputMapper>(); |
| 240 | |||
| 241 | if (numTrackedControllers == 0) { | ||
| 242 | if (_deviceID != 0) { | ||
| 243 | userInputMapper->removeDevice(_deviceID); | ||
| 244 | _deviceID = 0; | ||
| 245 | _poseStateMap.clear(); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | 236 | ||
| 249 | if (_trackedControllers == 0 && numTrackedControllers > 0) { | 237 | if (numTrackedControllers == 0) { |
| 250 | registerToUserInputMapper(*userInputMapper); | 238 | if (_deviceID != 0) { |
| 251 | assignDefaultInputMapping(*userInputMapper); | 239 | userInputMapper->removeDevice(_deviceID); |
| 252 | UserActivityLogger::getInstance().connectedDevice("spatial_controller", "steamVR"); | 240 | _deviceID = 0; |
| 241 | _poseStateMap.clear(); | ||
| 253 | } | 242 | } |
| 243 | } | ||
| 254 | 244 | ||
| 255 | _trackedControllers = numTrackedControllers; | 245 | if (_trackedControllers == 0 && numTrackedControllers > 0) { |
| 246 | registerToUserInputMapper(*userInputMapper); | ||
| 247 | assignDefaultInputMapping(*userInputMapper); | ||
| 248 | UserActivityLogger::getInstance().connectedDevice("spatial_controller", "steamVR"); | ||
| 256 | } | 249 | } |
| 250 | |||
| 251 | _trackedControllers = numTrackedControllers; | ||
| 257 | } | 252 | } |
| 258 | 253 | ||
| 259 | void ViveControllerManager::focusOutEvent() { | 254 | void ViveControllerManager::focusOutEvent() { |