oculusVRHMDDevice.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifndef _OCULUSVRHMDDEVICE_H_
  23. #define _OCULUSVRHMDDEVICE_H_
  24. #include "core/util/str.h"
  25. #include "math/mQuat.h"
  26. #include "math/mPoint2.h"
  27. #include "math/mPoint3.h"
  28. #include "math/mPoint4.h"
  29. #include "platform/input/oculusVR/oculusVRConstants.h"
  30. #include "platform/types.h"
  31. #include "gfx/gfxTextureHandle.h"
  32. #include "math/mRect.h"
  33. #include "gfx/gfxDevice.h"
  34. #include "OVR_CAPI_0_5_0.h"
  35. class GuiCanvas;
  36. class GameConnection;
  37. struct DisplayPose;
  38. class OculusVRSensorDevice;
  39. class OculusVRHMDDevice
  40. {
  41. public:
  42. enum SimulationTypes {
  43. ST_RIFT_PREVIEW,
  44. };
  45. protected:
  46. bool mIsValid;
  47. bool mVsync;
  48. bool mTimewarp;
  49. bool mRenderConfigurationDirty;
  50. bool mFrameReady;
  51. ovrHmd mDevice;
  52. U32 mSupportedDistortionCaps;
  53. U32 mCurrentDistortionCaps;
  54. U32 mSupportedCaps;
  55. U32 mCurrentCaps;
  56. // From OVR::DeviceInfo
  57. String mProductName;
  58. String mManufacturer;
  59. U32 mVersion;
  60. // Windows display device name used in EnumDisplaySettings/CreateDC
  61. String mDisplayDeviceName;
  62. // MacOS display ID
  63. S32 mDisplayId;
  64. // Desktop coordinate position of the screen (can be negative; may not be present on all platforms)
  65. Point2I mDesktopPosition;
  66. // Whole screen resolution
  67. Point2I mResolution;
  68. // Physical screen size in meters
  69. Point2F mScreenSize;
  70. // Physical distance between lens centers, in meters
  71. F32 mLensSeparation;
  72. // Physical distance between the user's eye centers as defined in the current profile
  73. F32 mProfileInterpupillaryDistance;
  74. // Physical distance between the user's eye centers
  75. F32 mInterpupillaryDistance;
  76. // The amount to offset the projection matrix to account for the eye not being in the
  77. // center of the screen.
  78. Point2F mProjectionCenterOffset;
  79. // Current pose of eyes
  80. ovrPosef mCurrentEyePoses[2];
  81. ovrEyeRenderDesc mEyeRenderDesc[2];
  82. ovrFovPort mCurrentFovPorts[2];
  83. Point2I mWindowSize;
  84. GameConnection *mConnection;
  85. OculusVRSensorDevice *mSensor;
  86. U32 mActionCodeIndex;
  87. protected:
  88. void updateRenderInfo();
  89. public:
  90. OculusVRHMDDevice();
  91. ~OculusVRHMDDevice();
  92. void cleanUp();
  93. // Set the HMD properties based on information from the OVR device
  94. void set(ovrHmd hmd, U32 actionCodeIndex);
  95. // Sets optimal display size for canvas
  96. void setOptimalDisplaySize(GuiCanvas *canvas);
  97. bool isValid() const {return mIsValid;}
  98. const char* getProductName() const { return mProductName.c_str(); }
  99. const char* getManufacturer() const { return mManufacturer.c_str(); }
  100. U32 getVersion() const { return mVersion; }
  101. // Windows display device name used in EnumDisplaySettings/CreateDC
  102. const char* getDisplayDeviceName() const { return mDisplayDeviceName.c_str(); }
  103. // MacOS display ID
  104. S32 getDisplayDeviceId() const { return mDisplayId; }
  105. // Desktop coordinate position of the screen (can be negative; may not be present on all platforms)
  106. const Point2I& getDesktopPosition() const { return mDesktopPosition; }
  107. // Whole screen resolution
  108. const Point2I& getResolution() const { return mResolution; }
  109. // Physical screen size in meters
  110. const Point2F& getScreenSize() const { return mScreenSize; }
  111. // Physical distance between lens centers, in meters
  112. F32 getLensSeparation() const { return mLensSeparation; }
  113. // Physical distance between the user's eye centers as defined by the current profile
  114. F32 getProfileIPD() const { return mProfileInterpupillaryDistance; }
  115. // Physical distance between the user's eye centers
  116. F32 getIPD() const { return mInterpupillaryDistance; }
  117. // Set a new physical distance between the user's eye centers
  118. void setIPD(F32 ipd);
  119. // The amount to offset the projection matrix to account for the eye not being in the
  120. // center of the screen.
  121. const Point2F& getProjectionCenterOffset() const { return mProjectionCenterOffset; }
  122. void getStereoViewports(RectI *dest) const { dMemcpy(dest, mEyeViewport, sizeof(mEyeViewport)); }
  123. void getStereoTargets(GFXTextureTarget **dest) const { dest[0] = mEyeRT[0]; dest[1] = mEyeRT[1]; }
  124. void getFovPorts(FovPort *dest) const { dMemcpy(dest, mCurrentFovPorts, sizeof(mCurrentFovPorts)); }
  125. /// Returns eye offsets in torque coordinate space, i.e. z being up, x being left-right, and y being depth (forward).
  126. void getEyeOffsets(Point3F *offsets) const {
  127. offsets[0] = Point3F(-mEyeRenderDesc[0].HmdToEyeViewOffset.x, mEyeRenderDesc[0].HmdToEyeViewOffset.z, -mEyeRenderDesc[0].HmdToEyeViewOffset.y);
  128. offsets[1] = Point3F(-mEyeRenderDesc[1].HmdToEyeViewOffset.x, mEyeRenderDesc[1].HmdToEyeViewOffset.z, -mEyeRenderDesc[1].HmdToEyeViewOffset.y); }
  129. void getFrameEyePose(DisplayPose *outPose, U32 eyeId) const;
  130. void updateCaps();
  131. void onStartFrame();
  132. void onEndFrame();
  133. void onDeviceDestroy();
  134. Point2I generateRenderTarget(GFXTextureTargetRef &target, GFXTexHandle &texture, GFXTexHandle &depth, Point2I desiredSize);
  135. void clearRenderTargets();
  136. bool isDisplayingWarning();
  137. void dismissWarning();
  138. bool setupTargets();
  139. /// Designates canvas we are drawing to. Also updates render targets
  140. void setDrawCanvas(GuiCanvas *canvas) { if (mDrawCanvas != canvas) { mDrawCanvas = canvas; } updateRenderInfo(); }
  141. virtual void setCurrentConnection(GameConnection *connection) { mConnection = connection; }
  142. virtual GameConnection* getCurrentConnection() { return mConnection; }
  143. String dumpMetrics();
  144. // Stereo RT
  145. GFXTexHandle mStereoTexture;
  146. GFXTexHandle mStereoDepthTexture;
  147. GFXTextureTargetRef mStereoRT;
  148. // Eye RTs (if we are using separate targets)
  149. GFXTextureTargetRef mEyeRT[2];
  150. GFXTexHandle mEyeTexture[2];
  151. GFXTexHandle mEyeDepthTexture[2];
  152. // Current render target size for each eye
  153. Point2I mEyeRenderSize[2];
  154. // Recommended eye target size for each eye
  155. ovrSizei mRecomendedEyeTargetSize[2];
  156. // Desired viewport for each eye
  157. RectI mEyeViewport[2];
  158. F32 mCurrentPixelDensity;
  159. F32 smDesiredPixelDensity;
  160. ovrTrackingState mLastTrackingState;
  161. GFXDevice::GFXDeviceRenderStyles mDesiredRenderingMode;
  162. GFXFormat mRTFormat;
  163. // Canvas we should be drawing
  164. GuiCanvas *mDrawCanvas;
  165. OculusVRSensorDevice *getSensorDevice() { return mSensor; }
  166. };
  167. #endif // _OCULUSVRHMDDEVICE_H_