oculusVRHMDDevice.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/oculusVRHMDDevice.h"
  23. OculusVRHMDDevice::OculusVRHMDDevice()
  24. {
  25. mIsValid = false;
  26. mIsSimulation = false;
  27. mDevice = NULL;
  28. }
  29. OculusVRHMDDevice::~OculusVRHMDDevice()
  30. {
  31. cleanUp();
  32. }
  33. void OculusVRHMDDevice::cleanUp()
  34. {
  35. if(mDevice)
  36. {
  37. mDevice->Release();
  38. mDevice = NULL;
  39. }
  40. mIsValid = false;
  41. }
  42. void OculusVRHMDDevice::set(OVR::HMDDevice* hmd, OVR::HMDInfo& info, bool calculateDistortionScale)
  43. {
  44. mIsValid = false;
  45. mIsSimulation = false;
  46. mDevice = hmd;
  47. // DeviceInfo
  48. mProductName = info.ProductName;
  49. mManufacturer = info.Manufacturer;
  50. mVersion = info.Version;
  51. mDisplayDeviceName = info.DisplayDeviceName;
  52. mDisplayId = info.DisplayId;
  53. mDesktopPosition.x = info.DesktopX;
  54. mDesktopPosition.y = info.DesktopY;
  55. mResolution.x = info.HResolution;
  56. mResolution.y = info.VResolution;
  57. mScreenSize.x = info.HScreenSize;
  58. mScreenSize.y = info.VScreenSize;
  59. mVerticalEyeCenter = info.VScreenCenter;
  60. mEyeToScreen = info.EyeToScreenDistance;
  61. mLensSeparation = info.LensSeparationDistance;
  62. mProfileInterpupillaryDistance = info.InterpupillaryDistance;
  63. mInterpupillaryDistance = mProfileInterpupillaryDistance;
  64. mKDistortion.x = info.DistortionK[0];
  65. mKDistortion.y = info.DistortionK[1];
  66. mKDistortion.z = info.DistortionK[2];
  67. mKDistortion.w = info.DistortionK[3];
  68. mChromaticAbCorrection.x = info.ChromaAbCorrection[0];
  69. mChromaticAbCorrection.y = info.ChromaAbCorrection[1];
  70. mChromaticAbCorrection.z = info.ChromaAbCorrection[2];
  71. mChromaticAbCorrection.w = info.ChromaAbCorrection[3];
  72. // Calculated values
  73. calculateValues(calculateDistortionScale);
  74. mIsValid = true;
  75. }
  76. void OculusVRHMDDevice::createSimulation(SimulationTypes simulationType, bool calculateDistortionScale)
  77. {
  78. if(simulationType == ST_RIFT_PREVIEW)
  79. {
  80. createSimulatedPreviewRift(calculateDistortionScale);
  81. }
  82. }
  83. void OculusVRHMDDevice::createSimulatedPreviewRift(bool calculateDistortionScale)
  84. {
  85. mIsValid = true;
  86. mIsSimulation = true;
  87. mProductName = "Oculus Rift DK1-SLA1";
  88. mManufacturer = "Oculus VR";
  89. mVersion = 0;
  90. mDisplayDeviceName = "";
  91. mResolution.x = 1280;
  92. mResolution.y = 800;
  93. mScreenSize.x = 0.14975999f;
  94. mScreenSize.y = 0.093599997f;
  95. mVerticalEyeCenter = 0.046799999f;
  96. mEyeToScreen = 0.041000001f;
  97. mLensSeparation = 0.064000003f;
  98. mProfileInterpupillaryDistance = 0.064000003f;
  99. mInterpupillaryDistance = mProfileInterpupillaryDistance;
  100. mKDistortion.x = 1.0000000f;
  101. mKDistortion.y = 0.22000000f;
  102. mKDistortion.z = 0.23999999f;
  103. mKDistortion.w = 0.00000000f;
  104. mChromaticAbCorrection.x = 0.995999f;
  105. mChromaticAbCorrection.y = -0.004f;
  106. mChromaticAbCorrection.z = 1.014f;
  107. mChromaticAbCorrection.w = 0.0f;
  108. calculateValues(calculateDistortionScale);
  109. }
  110. void OculusVRHMDDevice::setIPD(F32 ipd, bool calculateDistortionScale)
  111. {
  112. mInterpupillaryDistance = ipd;
  113. // Recalculate as some values rely on the IPD
  114. calculateValues(calculateDistortionScale);
  115. }
  116. // Computes scale that should be applied to the input render texture
  117. // before distortion to fit the result in the same screen size.
  118. // The 'fitRadius' parameter specifies the distance away from distortion center at
  119. // which the input and output coordinates will match, assuming [-1,1] range.
  120. F32 OculusVRHMDDevice::calcScale(F32 fitRadius)
  121. {
  122. F32 s = fitRadius;
  123. // This should match distortion equation used in shader.
  124. F32 ssq = s * s;
  125. F32 scale = s * (mKDistortion.x + mKDistortion.y * ssq + mKDistortion.z * ssq * ssq + mKDistortion.w * ssq * ssq * ssq);
  126. return scale;
  127. }
  128. void OculusVRHMDDevice::calculateValues(bool calculateDistortionScale)
  129. {
  130. F32 halfScreenX = mScreenSize.x * 0.5f;
  131. if(halfScreenX > 0)
  132. {
  133. F32 halfLensSeparation = mLensSeparation * 0.5;
  134. F32 offset = halfLensSeparation / halfScreenX;
  135. mEyeUVOffset.x = offset - 0.5;
  136. mEyeUVOffset.y = 1.0f - offset - 0.5;
  137. }
  138. else
  139. {
  140. mEyeUVOffset.x = 0.5f;
  141. mEyeUVOffset.y = 0.5f;
  142. }
  143. F32 lensOffset = mLensSeparation * 0.5f;
  144. F32 lensShift = mScreenSize.x * 0.25f - lensOffset;
  145. F32 lensViewportShift = 4.0f * lensShift / mScreenSize.x;
  146. mXCenterOffset= lensViewportShift;
  147. // Determine how the input texture should be scaled relative to the back buffer
  148. // so that we fit the distorted view to the backbuffer after calculating the
  149. // distortion. In reference to section 5.6.3 Distortion Scale and FOV in the
  150. // SDK docs.
  151. if(!calculateDistortionScale)
  152. {
  153. // Do not calculate a distortion scale for the input texture. This means that the input
  154. // texture and the backbuffer will be the same resolution.
  155. mDistortionFit.x = 0.0f;
  156. mDistortionFit.y = 0.0f;
  157. }
  158. else if (mScreenSize.x > 0.140f) // 7"
  159. {
  160. mDistortionFit.x = -1.0f;
  161. mDistortionFit.y = 0.0f;
  162. }
  163. else // 5"
  164. {
  165. mDistortionFit.x = 0.0f;
  166. mDistortionFit.y = 1.0f;
  167. }
  168. // Compute distortion scale from DistortionFitX & DistortionFitY.
  169. // Fit value of 0.0 means "no fit".
  170. if (mIsZero(mDistortionFit.x) && mIsZero(mDistortionFit.y))
  171. {
  172. mDistortionScale = 1.0f;
  173. }
  174. else
  175. {
  176. // Convert fit value to distortion-centered coordinates before fit radius
  177. // calculation.
  178. // NOTE: For now just assume a full view the same size as the HMD supports. It is
  179. // possible that this full view is smaller or larger.
  180. F32 stereoAspect = 0.5f * mResolution.x / mResolution.y;
  181. F32 dx = mDistortionFit.x - mXCenterOffset;
  182. F32 dy = mDistortionFit.y / stereoAspect;
  183. F32 fitRadius = sqrt(dx * dx + dy * dy);
  184. mDistortionScale = calcScale(fitRadius)/fitRadius;
  185. }
  186. // Calculate the vertical FOV for a single eye
  187. mAspectRatio = F32(mResolution.x * 0.5f) / F32(mResolution.y);
  188. F32 halfScreenDistance = mScreenSize.y * 0.5f * mDistortionScale;
  189. mYFOV = 2.0f * mAtan(halfScreenDistance / mEyeToScreen);
  190. F32 viewCenter = mScreenSize.x * 0.25f;
  191. F32 eyeProjectionShift = viewCenter - (mInterpupillaryDistance * 0.5f);
  192. mProjectionCenterOffset.set(4.0f * eyeProjectionShift / mScreenSize.x, 0.0f);
  193. mEyeWorldOffset.set(mInterpupillaryDistance * 0.5f, 0.0f, 0.0f);
  194. }