oculusVRHMDDevice.cpp 6.6 KB

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