oculusVRUtil.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/oculusVRUtil.h"
  23. namespace OculusVRUtil
  24. {
  25. void convertRotation(const F32 inRotMat[4][4], MatrixF& outRotation)
  26. {
  27. // Set rotation. We need to convert from sensor coordinates to
  28. // Torque coordinates. The sensor matrix is stored row-major.
  29. // The conversion is:
  30. //
  31. // Sensor Torque
  32. // a b c a b c a -c b
  33. // d e f --> -g -h -i --> -g i -h
  34. // g h i d e f d -f e
  35. outRotation.setColumn(0, Point4F( inRotMat[0][0], -inRotMat[2][0], inRotMat[1][0], 0.0f));
  36. outRotation.setColumn(1, Point4F(-inRotMat[0][2], inRotMat[2][2], -inRotMat[1][2], 0.0f));
  37. outRotation.setColumn(2, Point4F( inRotMat[0][1], -inRotMat[2][1], inRotMat[1][1], 0.0f));
  38. outRotation.setPosition(Point3F::Zero);
  39. }
  40. void convertRotation(OVR::Quatf& inRotation, EulerF& outRotation)
  41. {
  42. F32 yaw, pitch, roll;
  43. inRotation.GetEulerAngles<OVR::Axis_Y, OVR::Axis_X, OVR::Axis_Z>(&yaw, &pitch, &roll);
  44. outRotation.x = -pitch;
  45. outRotation.y = roll;
  46. outRotation.z = -yaw;
  47. }
  48. void calculateAxisRotation(const MatrixF& inRotation, const F32& maxAxisRadius, Point2F& outRotation)
  49. {
  50. const VectorF& controllerUp = inRotation.getUpVector();
  51. Point2F axis(0,0);
  52. axis.x = controllerUp.x;
  53. axis.y = controllerUp.y;
  54. // Limit the axis angle to that given to us
  55. if(axis.len() > maxAxisRadius)
  56. {
  57. axis.normalize(maxAxisRadius);
  58. }
  59. // Renormalize to the range of 0..1
  60. if(maxAxisRadius != 0.0f)
  61. {
  62. axis /= maxAxisRadius;
  63. }
  64. outRotation.x = axis.x;
  65. outRotation.y = axis.y;
  66. }
  67. void convertAcceleration(OVR::Vector3f& inAcceleration, VectorF& outAcceleration)
  68. {
  69. outAcceleration.set(inAcceleration.x, -inAcceleration.z, inAcceleration.y);
  70. }
  71. void convertAngularVelocity(OVR::Vector3f& inAngVel, EulerF& outAngVel)
  72. {
  73. outAngVel.set(-inAngVel.x, inAngVel.z, -inAngVel.y);
  74. }
  75. void convertMagnetometer(OVR::Vector3f& inMagnetometer, VectorF& outMagnetometer)
  76. {
  77. outMagnetometer.set(inMagnetometer.x, -inMagnetometer.z, inMagnetometer.y);
  78. }
  79. }