oculusVRSensorDevice.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/input/oculusVR/oculusVRSensorDevice.h"
  23. #include "platform/input/oculusVR/oculusVRSensorData.h"
  24. #include "platform/input/oculusVR/oculusVRUtil.h"
  25. #include "platform/platformInput.h"
  26. U32 OculusVRSensorDevice::OVR_SENSORROT[OculusVRConstants::MaxSensors] = {0};
  27. U32 OculusVRSensorDevice::OVR_SENSORROTANG[OculusVRConstants::MaxSensors] = {0};
  28. U32 OculusVRSensorDevice::OVR_SENSORROTAXISX[OculusVRConstants::MaxSensors] = {0};
  29. U32 OculusVRSensorDevice::OVR_SENSORROTAXISY[OculusVRConstants::MaxSensors] = {0};
  30. OculusVRSensorDevice::OculusVRSensorDevice()
  31. {
  32. mIsValid = false;
  33. mIsSimulation = false;
  34. mDevice = NULL;
  35. for(U32 i=0; i<2; ++i)
  36. {
  37. mDataBuffer[i] = new OculusVRSensorData();
  38. }
  39. mPrevData = mDataBuffer[0];
  40. }
  41. OculusVRSensorDevice::~OculusVRSensorDevice()
  42. {
  43. cleanUp();
  44. for(U32 i=0; i<2; ++i)
  45. {
  46. delete mDataBuffer[i];
  47. mDataBuffer[i] = NULL;
  48. }
  49. mPrevData = NULL;
  50. }
  51. void OculusVRSensorDevice::cleanUp()
  52. {
  53. mSensorFusion.AttachToSensor(NULL);
  54. if(mDevice)
  55. {
  56. mDevice->Release();
  57. mDevice = NULL;
  58. }
  59. mIsValid = false;
  60. }
  61. void OculusVRSensorDevice::set(OVR::SensorDevice* sensor, OVR::SensorInfo& info, S32 actionCodeIndex)
  62. {
  63. mIsValid = false;
  64. mDevice = sensor;
  65. mSensorFusion.AttachToSensor(sensor);
  66. // DeviceInfo
  67. mProductName = info.ProductName;
  68. mManufacturer = info.Manufacturer;
  69. mVersion = info.Version;
  70. // SensorInfo
  71. mVendorId = info.VendorId;
  72. mProductId = info.ProductId;
  73. mSerialNumber = info.SerialNumber;
  74. mActionCodeIndex = actionCodeIndex;
  75. if(mActionCodeIndex >= OculusVRConstants::MaxSensors)
  76. {
  77. // Cannot declare more sensors than we are able to handle
  78. mIsValid = false;
  79. }
  80. else
  81. {
  82. mIsValid = true;
  83. }
  84. }
  85. void OculusVRSensorDevice::createSimulation(SimulationTypes simulationType, S32 actionCodeIndex)
  86. {
  87. if(simulationType == ST_RIFT_PREVIEW)
  88. {
  89. createSimulatedPreviewRift(actionCodeIndex);
  90. }
  91. }
  92. void OculusVRSensorDevice::createSimulatedPreviewRift(S32 actionCodeIndex)
  93. {
  94. mIsValid = false;
  95. mIsSimulation = true;
  96. // DeviceInfo
  97. mProductName = "Tracker DK";
  98. mManufacturer = "Oculus VR, Inc.";
  99. mVersion = 0;
  100. // SensorInfo
  101. mVendorId = 10291;
  102. mProductId = 1;
  103. mSerialNumber = "000000000000";
  104. mActionCodeIndex = actionCodeIndex;
  105. if(mActionCodeIndex >= OculusVRConstants::MaxSensors)
  106. {
  107. // Cannot declare more sensors than we are able to handle
  108. mIsValid = false;
  109. }
  110. else
  111. {
  112. mIsValid = true;
  113. }
  114. }
  115. void OculusVRSensorDevice::buildCodeTable()
  116. {
  117. // Obtain all of the device codes
  118. for(U32 i=0; i<OculusVRConstants::MaxSensors; ++i)
  119. {
  120. OVR_SENSORROT[i] = INPUTMGR->getNextDeviceCode();
  121. OVR_SENSORROTANG[i] = INPUTMGR->getNextDeviceCode();
  122. OVR_SENSORROTAXISX[i] = INPUTMGR->getNextDeviceCode();
  123. OVR_SENSORROTAXISY[i] = INPUTMGR->getNextDeviceCode();
  124. }
  125. // Build out the virtual map
  126. char buffer[64];
  127. for(U32 i=0; i<OculusVRConstants::MaxSensors; ++i)
  128. {
  129. dSprintf(buffer, 64, "ovr_sensorrot%d", i);
  130. INPUTMGR->addVirtualMap( buffer, SI_ROT, OVR_SENSORROT[i] );
  131. dSprintf(buffer, 64, "ovr_sensorrotang%d", i);
  132. INPUTMGR->addVirtualMap( buffer, SI_ROT, OVR_SENSORROTANG[i] );
  133. dSprintf(buffer, 64, "ovr_sensorrotaxisx%d", i);
  134. INPUTMGR->addVirtualMap( buffer, SI_AXIS, OVR_SENSORROTAXISX[i] );
  135. dSprintf(buffer, 64, "ovr_sensorrotaxisy%d", i);
  136. INPUTMGR->addVirtualMap( buffer, SI_AXIS, OVR_SENSORROTAXISY[i] );
  137. }
  138. }
  139. bool OculusVRSensorDevice::process(U32 deviceType, bool generateRotAsAngAxis, bool generateRotAsEuler, bool generateRotationAsAxisEvents, F32 maxAxisRadius)
  140. {
  141. if(!mIsValid)
  142. return false;
  143. // Store the current data from the sensor and compare with previous data
  144. U32 diff;
  145. OculusVRSensorData* currentBuffer = (mPrevData == mDataBuffer[0]) ? mDataBuffer[1] : mDataBuffer[0];
  146. if(!mIsSimulation)
  147. {
  148. currentBuffer->setData(mSensorFusion, maxAxisRadius);
  149. }
  150. else
  151. {
  152. currentBuffer->simulateData(maxAxisRadius);
  153. }
  154. diff = mPrevData->compare(currentBuffer);
  155. // Update the previous data pointer. We do this here in case someone calls our
  156. // console functions during one of the input events below.
  157. mPrevData = currentBuffer;
  158. // Rotation event
  159. if(diff & OculusVRSensorData::DIFF_ROT)
  160. {
  161. if(generateRotAsAngAxis)
  162. {
  163. INPUTMGR->buildInputEvent(deviceType, OculusVRConstants::DefaultOVRBase, SI_ROT, OVR_SENSORROT[mActionCodeIndex], SI_MOVE, currentBuffer->mRotQuat);
  164. }
  165. if(generateRotAsEuler)
  166. {
  167. // Convert angles to degrees
  168. VectorF angles;
  169. for(U32 i=0; i<3; ++i)
  170. {
  171. angles[i] = mRadToDeg(currentBuffer->mRotEuler[i]);
  172. }
  173. INPUTMGR->buildInputEvent(deviceType, OculusVRConstants::DefaultOVRBase, SI_POS, OVR_SENSORROTANG[mActionCodeIndex], SI_MOVE, angles);
  174. }
  175. }
  176. // Rotation as axis event
  177. if(generateRotationAsAxisEvents && diff & OculusVRSensorData::DIFF_ROTAXIS)
  178. {
  179. if(diff & OculusVRSensorData::DIFF_ROTAXISX)
  180. INPUTMGR->buildInputEvent(deviceType, OculusVRConstants::DefaultOVRBase, SI_AXIS, OVR_SENSORROTAXISX[mActionCodeIndex], SI_MOVE, currentBuffer->mRotAxis.x);
  181. if(diff & OculusVRSensorData::DIFF_ROTAXISY)
  182. INPUTMGR->buildInputEvent(deviceType, OculusVRConstants::DefaultOVRBase, SI_AXIS, OVR_SENSORROTAXISY[mActionCodeIndex], SI_MOVE, currentBuffer->mRotAxis.y);
  183. }
  184. return true;
  185. }
  186. void OculusVRSensorDevice::reset()
  187. {
  188. if(!mIsValid)
  189. return;
  190. mSensorFusion.Reset();
  191. }
  192. F32 OculusVRSensorDevice::getPredictionTime() const
  193. {
  194. if(!mIsValid)
  195. return 0.0f;
  196. return mSensorFusion.GetPredictionDelta();
  197. }
  198. void OculusVRSensorDevice::setPredictionTime(F32 dt)
  199. {
  200. if(!mIsValid)
  201. return;
  202. mSensorFusion.SetPrediction(dt);
  203. }
  204. EulerF OculusVRSensorDevice::getEulerRotation()
  205. {
  206. if(!mIsValid)
  207. return Point3F::Zero;
  208. OVR::Quatf orientation;
  209. if(mSensorFusion.GetPredictionDelta() > 0)
  210. {
  211. orientation = mSensorFusion.GetPredictedOrientation();
  212. }
  213. else
  214. {
  215. orientation = mSensorFusion.GetOrientation();
  216. }
  217. // Sensor rotation in Euler format
  218. EulerF rot;
  219. OculusVRUtil::convertRotation(orientation, rot);
  220. return rot;
  221. }