Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: The implementation of ISourceVirtualReality, which provides utility
  4. // functions for VR including head tracking, window/viewport information,
  5. // rendering information, and distortion
  6. //
  7. //=============================================================================
  8. #ifndef SOURCEVIRTUALREALITY_H
  9. #define SOURCEVIRTUALREALITY_H
  10. #if defined( _WIN32 )
  11. #pragma once
  12. #endif
  13. #include "tier3/tier3.h"
  14. #include "sourcevr/isourcevirtualreality.h"
  15. #include "materialsystem/itexture.h"
  16. #include "materialsystem/MaterialSystemUtil.h"
  17. #include "openvr/openvr.h"
  18. // this is a callback so we can regenerate the distortion texture whenever we need to
  19. class CDistortionTextureRegen : public ITextureRegenerator
  20. {
  21. public:
  22. CDistortionTextureRegen( vr::Hmd_Eye eEye ) : m_eEye( eEye ) {}
  23. virtual void RegenerateTextureBits( ITexture *pTexture, IVTFTexture *pVTFTexture, Rect_t *pSubRect ) OVERRIDE;
  24. virtual void Release() OVERRIDE {}
  25. private:
  26. vr::Hmd_Eye m_eEye;
  27. };
  28. //-----------------------------------------------------------------------------
  29. // The implementation
  30. //-----------------------------------------------------------------------------
  31. class CSourceVirtualReality: public CTier3AppSystem< ISourceVirtualReality >
  32. {
  33. typedef CTier3AppSystem< ISourceVirtualReality > BaseClass;
  34. public:
  35. CSourceVirtualReality();
  36. ~CSourceVirtualReality();
  37. //---------------------------------------------------------
  38. // Initialization and shutdown
  39. //---------------------------------------------------------
  40. //
  41. // IAppSystem
  42. //
  43. virtual bool Connect( CreateInterfaceFn factory );
  44. virtual void Disconnect();
  45. virtual void * QueryInterface( const char *pInterfaceName );
  46. // these will come from the engine
  47. virtual InitReturnVal_t Init();
  48. virtual void Shutdown();
  49. //---------------------------------------------------------
  50. // ISourceVirtualReality implementation
  51. //---------------------------------------------------------
  52. virtual bool ShouldRunInVR() OVERRIDE;
  53. virtual bool IsHmdConnected() OVERRIDE;
  54. virtual void GetViewportBounds( VREye eEye, int *pnX, int *pnY, int *pnWidth, int *pnHeight ) OVERRIDE;
  55. virtual bool DoDistortionProcessing ( VREye eEye ) OVERRIDE;
  56. virtual bool CompositeHud ( VREye eEye, float ndcHudBounds[4], bool bDoUndistort, bool bBlackout, bool bTranslucent ) OVERRIDE;
  57. virtual VMatrix GetMideyePose() OVERRIDE;
  58. virtual bool SampleTrackingState ( float PlayerGameFov, float fPredictionSeconds ) OVERRIDE;
  59. virtual bool GetEyeProjectionMatrix ( VMatrix *pResult, VREye, float zNear, float zFar, float fovScale ) OVERRIDE;
  60. virtual bool WillDriftInYaw() OVERRIDE;
  61. virtual bool GetDisplayBounds( VRRect_t *pRect ) OVERRIDE;
  62. virtual VMatrix GetMidEyeFromEye( VREye eEye ) OVERRIDE;
  63. virtual int GetVRModeAdapter() OVERRIDE;
  64. virtual void CreateRenderTargets( IMaterialSystem *pMaterialSystem ) OVERRIDE;
  65. virtual void ShutdownRenderTargets() OVERRIDE;
  66. virtual ITexture *GetRenderTarget( VREye eEye, EWhichRenderTarget eWhich ) OVERRIDE;
  67. virtual void GetRenderTargetFrameBufferDimensions( int & nWidth, int & nHeight ) OVERRIDE;
  68. virtual bool Activate() OVERRIDE;
  69. virtual void Deactivate() OVERRIDE;
  70. virtual bool ShouldForceVRMode( ) OVERRIDE;
  71. virtual void SetShouldForceVRMode( ) OVERRIDE;
  72. void RefreshDistortionTexture();
  73. void AcquireNewZeroPose();
  74. bool StartTracker();
  75. void StopTracker();
  76. bool ResetTracking(); // Called to reset tracking
  77. // makes sure we've initialized OpenVR so we can use
  78. // m_pHmd
  79. bool EnsureOpenVRInited();
  80. // Prefer this to the convar so that convar will stick for the entire
  81. // VR activation. We can't lazy-crate render targets and don't
  82. // want to create the "just in case" somebody turns on this experimental
  83. // mode
  84. bool UsingOffscreenRenderTarget() const { return m_bUsingOffscreenRenderTarget; }
  85. vr::IVRSystem * GetHmd() { return m_pHmd; }
  86. private:
  87. bool m_bActive;
  88. bool m_bShouldForceVRMode;
  89. bool m_bUsingOffscreenRenderTarget;
  90. CDistortionTextureRegen m_textureGeneratorLeft;
  91. CDistortionTextureRegen m_textureGeneratorRight;
  92. CTextureReference g_StereoGuiTexture;
  93. CTextureReference m_pDistortionTextureLeft;
  94. CTextureReference m_pDistortionTextureRight;
  95. CTextureReference m_pPredistortRT;
  96. CTextureReference m_pPredistortRTDepth;
  97. CMaterialReference m_warpMaterial;
  98. CMaterialReference m_DistortLeftMaterial;
  99. CMaterialReference m_DistortRightMaterial;
  100. CMaterialReference m_DistortHUDLeftMaterial;
  101. CMaterialReference m_DistortHUDRightMaterial;
  102. CMaterialReference m_InWorldUIMaterial;
  103. CMaterialReference m_InWorldUIOpaqueMaterial;
  104. CMaterialReference m_blackMaterial;
  105. vr::IVRSystem *m_pHmd;
  106. bool m_bHaveValidPose;
  107. VMatrix m_ZeroFromHeadPose;
  108. };
  109. #endif // SOURCEVIRTUALREALITY_H