openVRProvider.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #ifndef _OPENVRDEVICE_H_
  2. #define _OPENVRDEVICE_H_
  3. #include "math/mQuat.h"
  4. #include "math/mPoint4.h"
  5. #include "math/util/frustum.h"
  6. #include "core/util/tSingleton.h"
  7. #include "gfx/gfxDevice.h"
  8. #include "gfx/gfxVertexBuffer.h"
  9. #include "gfx/gfxPrimitiveBuffer.h"
  10. #include "gfx/gfxTarget.h"
  11. #include "platform/input/IInputDevice.h"
  12. #include "platform/input/event.h"
  13. #include "platform/output/IDisplayDevice.h"
  14. #include <openvr.h>
  15. class OpenVRHMDDevice;
  16. class OpenVROverlay;
  17. class BaseMatInstance;
  18. class SceneRenderState;
  19. struct MeshRenderInst;
  20. class Namespace;
  21. class NamedTexTarget;
  22. typedef vr::VROverlayInputMethod OpenVROverlayInputMethod;
  23. typedef vr::VROverlayTransformType OpenVROverlayTransformType;
  24. typedef vr::EGamepadTextInputMode OpenVRGamepadTextInputMode;
  25. typedef vr::EGamepadTextInputLineMode OpenVRGamepadTextInputLineMode;
  26. typedef vr::ETrackingResult OpenVRTrackingResult;
  27. typedef vr::ETrackingUniverseOrigin OpenVRTrackingUniverseOrigin;
  28. typedef vr::EOverlayDirection OpenVROverlayDirection;
  29. typedef vr::EVRState OpenVRState;
  30. typedef vr::TrackedDeviceClass OpenVRTrackedDeviceClass;
  31. DefineEnumType(OpenVROverlayInputMethod);
  32. DefineEnumType(OpenVROverlayTransformType);
  33. DefineEnumType(OpenVRGamepadTextInputMode);
  34. DefineEnumType(OpenVRGamepadTextInputLineMode);
  35. DefineEnumType(OpenVRTrackingResult);
  36. DefineEnumType(OpenVRTrackingUniverseOrigin);
  37. DefineEnumType(OpenVROverlayDirection);
  38. DefineEnumType(OpenVRState);
  39. DefineEnumType(OpenVRTrackedDeviceClass);
  40. namespace OpenVRUtil
  41. {
  42. /// Convert a matrix in OVR space to torque space
  43. void convertTransformFromOVR(const MatrixF &inRotTMat, MatrixF& outRotation);
  44. /// Convert a matrix in torque space to OVR space
  45. void convertTransformToOVR(const MatrixF& inRotation, MatrixF& outRotation);
  46. /// Converts vr::HmdMatrix34_t to a MatrixF
  47. MatrixF convertSteamVRAffineMatrixToMatrixFPlain(const vr::HmdMatrix34_t &mat);
  48. /// Converts a MatrixF to a vr::HmdMatrix34_t
  49. void convertMatrixFPlainToSteamVRAffineMatrix(const MatrixF &inMat, vr::HmdMatrix34_t &outMat);
  50. U32 convertOpenVRButtonToTorqueButton(uint32_t vrButton);
  51. /// Converts a point to OVR coords
  52. inline Point3F convertPointToOVR(const Point3F &point)
  53. {
  54. return Point3F(-point.x, -point.z, point.y);
  55. }
  56. /// Converts a point from OVR coords
  57. inline Point3F convertPointFromOVR(const Point3F &point)
  58. {
  59. return Point3F(-point.x, point.z, -point.y);
  60. }
  61. // Converts a point from OVR coords, from an input float array
  62. inline Point3F convertPointFromOVR(const vr::HmdVector3_t& v)
  63. {
  64. return Point3F(-v.v[0], v.v[2], -v.v[1]);
  65. }
  66. };
  67. template<int TEXSIZE> class VRTextureSet
  68. {
  69. public:
  70. static const int TextureCount = TEXSIZE;
  71. GFXTexHandle mTextures[TEXSIZE];
  72. U32 mIndex;
  73. VRTextureSet() : mIndex(0)
  74. {
  75. }
  76. void init(U32 width, U32 height, GFXFormat fmt, GFXTextureProfile *profile, const String &desc)
  77. {
  78. for (U32 i = 0; i < TextureCount; i++)
  79. {
  80. mTextures[i].set(width, height, fmt, profile, desc);
  81. }
  82. }
  83. void clear()
  84. {
  85. for (U32 i = 0; i < TextureCount; i++)
  86. {
  87. mTextures[i] = NULL;
  88. }
  89. }
  90. void advance()
  91. {
  92. mIndex = (mIndex + 1) % TextureCount;
  93. }
  94. GFXTexHandle& getTextureHandle()
  95. {
  96. return mTextures[mIndex];
  97. }
  98. };
  99. /// Simple class to handle rendering native OpenVR model data
  100. class OpenVRRenderModel
  101. {
  102. public:
  103. typedef GFXVertexPNT VertexType;
  104. GFXVertexBufferHandle<VertexType> mVertexBuffer;
  105. GFXPrimitiveBufferHandle mPrimitiveBuffer;
  106. BaseMatInstance* mMaterialInstance; ///< Material to use for rendering. NOTE:
  107. Box3F mLocalBox;
  108. OpenVRRenderModel() : mMaterialInstance(NULL)
  109. {
  110. }
  111. ~OpenVRRenderModel()
  112. {
  113. SAFE_DELETE(mMaterialInstance);
  114. }
  115. Box3F getWorldBox(MatrixF &mat)
  116. {
  117. Box3F ret = mLocalBox;
  118. mat.mul(ret);
  119. return ret;
  120. }
  121. bool init(const vr::RenderModel_t & vrModel, StringTableEntry materialName);
  122. void draw(SceneRenderState *state, MeshRenderInst* renderInstance);
  123. };
  124. struct OpenVRRenderState
  125. {
  126. vr::IVRSystem *mHMD;
  127. FovPort mEyeFov[2];
  128. MatrixF mEyePose[2];
  129. MatrixF mHMDPose;
  130. RectI mEyeViewport[2];
  131. GFXTextureTargetRef mStereoRT;
  132. GFXTexHandle mStereoRenderTexture;
  133. GFXTexHandle mStereoDepthTexture;
  134. VRTextureSet<4> mOutputEyeTextures;
  135. GFXDevice::GFXDeviceRenderStyles mRenderMode;
  136. bool setupRenderTargets(GFXDevice::GFXDeviceRenderStyles mode);
  137. void renderPreview();
  138. void reset(vr::IVRSystem* hmd);
  139. void updateHMDProjection();
  140. };
  141. class OpenVRProvider : public IDisplayDevice, public IInputDevice
  142. {
  143. public:
  144. enum DataDifferences {
  145. DIFF_NONE = 0,
  146. DIFF_ROT = (1 << 0),
  147. DIFF_ROTAXISX = (1 << 1),
  148. DIFF_ROTAXISY = (1 << 2),
  149. DIFF_ACCEL = (1 << 3),
  150. DIFF_ANGVEL = (1 << 4),
  151. DIFF_MAG = (1 << 5),
  152. DIFF_POS = (1 << 6),
  153. DIFF_STATUS = (1 << 7),
  154. DIFF_ROTAXIS = (DIFF_ROTAXISX | DIFF_ROTAXISY),
  155. DIFF_RAW = (DIFF_ACCEL | DIFF_ANGVEL | DIFF_MAG),
  156. };
  157. struct LoadedRenderModel
  158. {
  159. StringTableEntry name;
  160. vr::RenderModel_t *vrModel;
  161. OpenVRRenderModel *model;
  162. vr::EVRRenderModelError modelError;
  163. S32 textureId;
  164. bool loadedTexture;
  165. };
  166. struct LoadedRenderTexture
  167. {
  168. U32 vrTextureId;
  169. vr::RenderModel_TextureMap_t *vrTexture;
  170. GFXTextureObject *texture;
  171. NamedTexTarget *targetTexture;
  172. vr::EVRRenderModelError textureError;
  173. };
  174. OpenVRProvider();
  175. ~OpenVRProvider();
  176. typedef Signal <void(const vr::VREvent_t &evt)> VREventSignal;
  177. VREventSignal& getVREventSignal() { return mVREventSignal; }
  178. static void staticInit();
  179. bool enable();
  180. bool disable();
  181. bool getActive() { return mHMD != NULL; }
  182. inline vr::IVRRenderModels* getRenderModels() { return mRenderModels; }
  183. /// @name Input handling
  184. /// {
  185. void buildInputCodeTable();
  186. virtual bool process();
  187. /// }
  188. /// @name Display handling
  189. /// {
  190. virtual bool providesFrameEyePose() const;
  191. virtual void getFrameEyePose(IDevicePose *pose, S32 eyeId) const;
  192. virtual bool providesEyeOffsets() const;
  193. /// Returns eye offset not taking into account any position tracking info
  194. virtual void getEyeOffsets(Point3F *dest) const;
  195. virtual bool providesFovPorts() const;
  196. virtual void getFovPorts(FovPort *out) const;
  197. virtual void getStereoViewports(RectI *out) const;
  198. virtual void getStereoTargets(GFXTextureTarget **out) const;
  199. virtual void setDrawCanvas(GuiCanvas *canvas);
  200. virtual void setDrawMode(GFXDevice::GFXDeviceRenderStyles style);
  201. virtual void setCurrentConnection(GameConnection *connection);
  202. virtual GameConnection* getCurrentConnection();
  203. virtual GFXTexHandle getPreviewTexture();
  204. virtual void onStartFrame();
  205. virtual void onEndFrame();
  206. virtual void onEyeRendered(U32 index);
  207. virtual void setRoomTracking(bool room);
  208. bool _handleDeviceEvent(GFXDevice::GFXDeviceEventType evt);
  209. S32 getDisplayDeviceId() const;
  210. /// }
  211. /// @name OpenVR handling
  212. /// {
  213. void processVREvent(const vr::VREvent_t & event);
  214. void updateTrackedPoses();
  215. void submitInputChanges();
  216. void resetSensors();
  217. void mapDeviceToEvent(U32 deviceIdx, S32 eventIdx);
  218. void resetEventMap();
  219. IDevicePose getTrackedDevicePose(U32 idx);
  220. /// }
  221. /// @name Overlay registration
  222. /// {
  223. void registerOverlay(OpenVROverlay* overlay);
  224. void unregisterOverlay(OpenVROverlay* overlay);
  225. /// }
  226. /// @name Model loading
  227. /// {
  228. const S32 preloadRenderModel(StringTableEntry name);
  229. const S32 preloadRenderModelTexture(U32 index);
  230. bool getRenderModel(S32 idx, OpenVRRenderModel **ret, bool &failed);
  231. bool getRenderModelTexture(S32 idx, GFXTextureObject **outTex, bool &failed);
  232. bool getRenderModelTextureName(S32 idx, String &outName);
  233. void resetRenderModels();
  234. /// }
  235. /// @name Console API
  236. /// {
  237. OpenVROverlay *getGamepadFocusOverlay();
  238. void setOverlayNeighbour(vr::EOverlayDirection dir, OpenVROverlay *overlay);
  239. bool isDashboardVisible();
  240. void showDashboard(const char *overlayToShow);
  241. vr::TrackedDeviceIndex_t getPrimaryDashboardDevice();
  242. void setKeyboardTransformAbsolute(const MatrixF &xfm);
  243. void setKeyboardPositionForOverlay(OpenVROverlay *overlay, const RectI &rect);
  244. void getControllerDeviceIndexes(vr::TrackedDeviceClass &deviceClass, Vector<S32> &outList);
  245. StringTableEntry getControllerModel(U32 idx);
  246. /// }
  247. /// @name OpenVR state
  248. /// {
  249. vr::IVRSystem *mHMD;
  250. vr::IVRRenderModels *mRenderModels;
  251. String mDriver;
  252. String mDisplay;
  253. vr::TrackedDevicePose_t mTrackedDevicePose[vr::k_unMaxTrackedDeviceCount];
  254. IDevicePose mCurrentDevicePose[vr::k_unMaxTrackedDeviceCount];
  255. IDevicePose mPreviousInputTrackedDevicePose[vr::k_unMaxTrackedDeviceCount];
  256. U32 mValidPoseCount;
  257. vr::VRControllerState_t mCurrentControllerState[vr::k_unMaxTrackedDeviceCount];
  258. vr::VRControllerState_t mPreviousCurrentControllerState[vr::k_unMaxTrackedDeviceCount];
  259. char mDeviceClassChar[vr::k_unMaxTrackedDeviceCount];
  260. OpenVRRenderState mHMDRenderState;
  261. GFXAdapterLUID mLUID;
  262. vr::ETrackingUniverseOrigin mTrackingSpace;
  263. Vector<OpenVROverlay*> mOverlays;
  264. VREventSignal mVREventSignal;
  265. Namespace *mOpenVRNS;
  266. Vector<LoadedRenderModel> mLoadedModels;
  267. Vector<LoadedRenderTexture> mLoadedTextures;
  268. Map<StringTableEntry, S32> mLoadedModelLookup;
  269. Map<U32, S32> mLoadedTextureLookup;
  270. Map<U32, S32> mDeviceEventMap;
  271. /// }
  272. GuiCanvas* mDrawCanvas;
  273. GameConnection* mGameConnection;
  274. static U32 OVR_SENSORROT[vr::k_unMaxTrackedDeviceCount];
  275. static U32 OVR_SENSORROTANG[vr::k_unMaxTrackedDeviceCount];
  276. static U32 OVR_SENSORVELOCITY[vr::k_unMaxTrackedDeviceCount];
  277. static U32 OVR_SENSORANGVEL[vr::k_unMaxTrackedDeviceCount];
  278. static U32 OVR_SENSORMAGNETOMETER[vr::k_unMaxTrackedDeviceCount];
  279. static U32 OVR_SENSORPOSITION[vr::k_unMaxTrackedDeviceCount];
  280. static U32 OVR_BUTTONPRESSED[vr::k_unMaxTrackedDeviceCount];
  281. static U32 OVR_BUTTONTOUCHED[vr::k_unMaxTrackedDeviceCount];
  282. static U32 OVR_AXISNONE[vr::k_unMaxTrackedDeviceCount];
  283. static U32 OVR_AXISTRACKPAD[vr::k_unMaxTrackedDeviceCount];
  284. static U32 OVR_AXISJOYSTICK[vr::k_unMaxTrackedDeviceCount];
  285. static U32 OVR_AXISTRIGGER[vr::k_unMaxTrackedDeviceCount];
  286. /// @name HMD Rotation offset
  287. /// {
  288. static EulerF smHMDRotOffset;
  289. static F32 smHMDmvYaw;
  290. static F32 smHMDmvPitch;
  291. static bool smRotateYawWithMoveActions;
  292. /// }
  293. public:
  294. // For ManagedSingleton.
  295. static const char* getSingletonName() { return "OpenVRProvider"; }
  296. };
  297. /// Returns the OculusVRDevice singleton.
  298. #define OPENVR ManagedSingleton<OpenVRProvider>::instance()
  299. #endif // _OCULUSVRDEVICE_H_