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