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.

285 lines
12 KiB

  1. //=== === Copyright 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. using System;
  7. using System.Runtime.InteropServices;
  8. namespace Valve.SteamVRInterop
  9. {
  10. class NativeEntrypoints
  11. {
  12. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetWindowBounds")]
  13. internal static extern void SteamAPI_IHmd_GetWindowBounds(IntPtr instancePtr, ref int32_t pnX, ref int32_t pnY, ref uint32_t pnWidth, ref uint32_t pnHeight);
  14. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetRecommendedRenderTargetSize")]
  15. internal static extern void SteamAPI_IHmd_GetRecommendedRenderTargetSize(IntPtr instancePtr, ref uint32_t pnWidth, ref uint32_t pnHeight);
  16. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetEyeOutputViewport")]
  17. internal static extern void SteamAPI_IHmd_GetEyeOutputViewport(IntPtr instancePtr, vr::Hmd_Eye eEye, ref uint32_t pnX, ref uint32_t pnY, ref uint32_t pnWidth, ref uint32_t pnHeight);
  18. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetProjectionMatrix")]
  19. internal static extern vr::HmdMatrix44_t SteamAPI_IHmd_GetProjectionMatrix(IntPtr instancePtr, vr::Hmd_Eye eEye, float fNearZ, float fFarZ, vr::GraphicsAPIConvention eProjType);
  20. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetProjectionRaw")]
  21. internal static extern void SteamAPI_IHmd_GetProjectionRaw(IntPtr instancePtr, vr::Hmd_Eye eEye, ref float pfLeft, ref float pfRight, ref float pfTop, ref float pfBottom);
  22. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_ComputeDistortion")]
  23. internal static extern vr::DistortionCoordinates_t SteamAPI_IHmd_ComputeDistortion(IntPtr instancePtr, vr::Hmd_Eye eEye, float fU, float fV);
  24. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetHeadFromEyePose")]
  25. internal static extern vr::HmdMatrix34_t SteamAPI_IHmd_GetHeadFromEyePose(IntPtr instancePtr, vr::Hmd_Eye eEye);
  26. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetViewMatrix")]
  27. internal static extern bool SteamAPI_IHmd_GetViewMatrix(IntPtr instancePtr, float fSecondsFromNow, ref vr::HmdMatrix44_t pMatLeftView, ref vr::HmdMatrix44_t pMatRightView, ref vr::HmdTrackingResult peResult);
  28. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetD3D9AdapterIndex")]
  29. internal static extern int32_t SteamAPI_IHmd_GetD3D9AdapterIndex(IntPtr instancePtr);
  30. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetDXGIOutputInfo")]
  31. internal static extern void SteamAPI_IHmd_GetDXGIOutputInfo(IntPtr instancePtr, ref int32_t pnAdapterIndex, ref int32_t pnAdapterOutputIndex);
  32. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_AttachToWindow")]
  33. internal static extern void SteamAPI_IHmd_AttachToWindow(IntPtr instancePtr, IntPtr hWnd);
  34. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetTrackerFromHeadPose")]
  35. internal static extern bool SteamAPI_IHmd_GetTrackerFromHeadPose(IntPtr instancePtr, float fPredictedSecondsFromNow, ref vr::HmdMatrix34_t pmPose, ref vr::HmdTrackingResult peResult);
  36. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetLastTrackerFromHeadPose")]
  37. internal static extern bool SteamAPI_IHmd_GetLastTrackerFromHeadPose(IntPtr instancePtr, ref vr::HmdMatrix34_t pmPose);
  38. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_WillDriftInYaw")]
  39. internal static extern bool SteamAPI_IHmd_WillDriftInYaw(IntPtr instancePtr);
  40. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_ZeroTracker")]
  41. internal static extern void SteamAPI_IHmd_ZeroTracker(IntPtr instancePtr);
  42. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetTrackerZeroPose")]
  43. internal static extern vr::HmdMatrix34_t SteamAPI_IHmd_GetTrackerZeroPose(IntPtr instancePtr);
  44. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetDriverId")]
  45. internal static extern uint32_t SteamAPI_IHmd_GetDriverId(IntPtr instancePtr, string pchBuffer, uint32_t unBufferLen);
  46. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_IHmd_GetDisplayId")]
  47. internal static extern uint32_t SteamAPI_IHmd_GetDisplayId(IntPtr instancePtr, string pchBuffer, uint32_t unBufferLen);
  48. }
  49. public abstract class IHmd
  50. {
  51. public abstract void GetWindowBounds(out int32_t pnX,out int32_t pnY,out uint32_t pnWidth,out uint32_t pnHeight);
  52. public abstract void GetRecommendedRenderTargetSize(out uint32_t pnWidth,out uint32_t pnHeight);
  53. public abstract void GetEyeOutputViewport(vr::Hmd_Eye eEye,out uint32_t pnX,out uint32_t pnY,out uint32_t pnWidth,out uint32_t pnHeight);
  54. public abstract vr::HmdMatrix44_t GetProjectionMatrix(vr::Hmd_Eye eEye,float fNearZ,float fFarZ,vr::GraphicsAPIConvention eProjType);
  55. public abstract void GetProjectionRaw(vr::Hmd_Eye eEye,out float pfLeft,out float pfRight,out float pfTop,out float pfBottom);
  56. public abstract vr::DistortionCoordinates_t ComputeDistortion(vr::Hmd_Eye eEye,float fU,float fV);
  57. public abstract vr::HmdMatrix34_t GetHeadFromEyePose(vr::Hmd_Eye eEye);
  58. public abstract bool GetViewMatrix(float fSecondsFromNow,out vr::HmdMatrix44_t pMatLeftView,out vr::HmdMatrix44_t pMatRightView,out vr::HmdTrackingResult peResult);
  59. public abstract int32_t GetD3D9AdapterIndex();
  60. public abstract void GetDXGIOutputInfo(out int32_t pnAdapterIndex,out int32_t pnAdapterOutputIndex);
  61. public abstract void AttachToWindow(IntPtr hWnd);
  62. public abstract bool GetTrackerFromHeadPose(float fPredictedSecondsFromNow,out vr::HmdMatrix34_t pmPose,out vr::HmdTrackingResult peResult);
  63. public abstract bool GetLastTrackerFromHeadPose(out vr::HmdMatrix34_t pmPose);
  64. public abstract bool WillDriftInYaw();
  65. public abstract void ZeroTracker();
  66. public abstract vr::HmdMatrix34_t GetTrackerZeroPose();
  67. public abstract uint32_t GetDriverId(string pchBuffer,uint32_t unBufferLen);
  68. public abstract uint32_t GetDisplayId(string pchBuffer,uint32_t unBufferLen);
  69. }
  70. public class CHmd : IHmd
  71. {
  72. public CHmd(IntPtr hmd)
  73. {
  74. m_hmd = hmd;
  75. }
  76. IntPtr m_hmd;
  77. private void CheckIfUsable()
  78. {
  79. if (m_hmd == IntPtr.Zero)
  80. {
  81. throw new Exception("Steam Pointer not configured");
  82. }
  83. }
  84. public override void GetWindowBounds(out int32_t pnX,out int32_t pnY,out uint32_t pnWidth,out uint32_t pnHeight)
  85. {
  86. CheckIfUsable();
  87. NativeEntrypoints.SteamAPI_IHmd_GetWindowBounds(m_hmd,ref pnX,ref pnY,ref pnWidth,ref pnHeight);
  88. }
  89. public override void GetRecommendedRenderTargetSize(out uint32_t pnWidth,out uint32_t pnHeight)
  90. {
  91. CheckIfUsable();
  92. NativeEntrypoints.SteamAPI_IHmd_GetRecommendedRenderTargetSize(m_hmd,ref pnWidth,ref pnHeight);
  93. }
  94. public override void GetEyeOutputViewport(vr::Hmd_Eye eEye,out uint32_t pnX,out uint32_t pnY,out uint32_t pnWidth,out uint32_t pnHeight)
  95. {
  96. CheckIfUsable();
  97. NativeEntrypoints.SteamAPI_IHmd_GetEyeOutputViewport(m_hmd,eEye,ref pnX,ref pnY,ref pnWidth,ref pnHeight);
  98. }
  99. public override vr::HmdMatrix44_t GetProjectionMatrix(vr::Hmd_Eye eEye,float fNearZ,float fFarZ,vr::GraphicsAPIConvention eProjType)
  100. {
  101. CheckIfUsable();
  102. vr::HmdMatrix44_t result = NativeEntrypoints.SteamAPI_IHmd_GetProjectionMatrix(m_hmd,eEye,fNearZ,fFarZ,eProjType);
  103. return result;
  104. }
  105. public override void GetProjectionRaw(vr::Hmd_Eye eEye,out float pfLeft,out float pfRight,out float pfTop,out float pfBottom)
  106. {
  107. CheckIfUsable();
  108. pfLeft = 0;
  109. pfRight = 0;
  110. pfTop = 0;
  111. pfBottom = 0;
  112. NativeEntrypoints.SteamAPI_IHmd_GetProjectionRaw(m_hmd,eEye,ref pfLeft,ref pfRight,ref pfTop,ref pfBottom);
  113. }
  114. public override vr::DistortionCoordinates_t ComputeDistortion(vr::Hmd_Eye eEye,float fU,float fV)
  115. {
  116. CheckIfUsable();
  117. vr::DistortionCoordinates_t result = NativeEntrypoints.SteamAPI_IHmd_ComputeDistortion(m_hmd,eEye,fU,fV);
  118. return result;
  119. }
  120. public override vr::HmdMatrix34_t GetHeadFromEyePose(vr::Hmd_Eye eEye)
  121. {
  122. CheckIfUsable();
  123. vr::HmdMatrix34_t result = NativeEntrypoints.SteamAPI_IHmd_GetHeadFromEyePose(m_hmd,eEye);
  124. return result;
  125. }
  126. public override bool GetViewMatrix(float fSecondsFromNow,out vr::HmdMatrix44_t pMatLeftView,out vr::HmdMatrix44_t pMatRightView,out vr::HmdTrackingResult peResult)
  127. {
  128. CheckIfUsable();
  129. bool result = NativeEntrypoints.SteamAPI_IHmd_GetViewMatrix(m_hmd,fSecondsFromNow,ref pMatLeftView,ref pMatRightView,ref peResult);
  130. return result;
  131. }
  132. public override int32_t GetD3D9AdapterIndex()
  133. {
  134. CheckIfUsable();
  135. int32_t result = NativeEntrypoints.SteamAPI_IHmd_GetD3D9AdapterIndex(m_hmd);
  136. return result;
  137. }
  138. public override void GetDXGIOutputInfo(out int32_t pnAdapterIndex,out int32_t pnAdapterOutputIndex)
  139. {
  140. CheckIfUsable();
  141. NativeEntrypoints.SteamAPI_IHmd_GetDXGIOutputInfo(m_hmd,ref pnAdapterIndex,ref pnAdapterOutputIndex);
  142. }
  143. public override void AttachToWindow(IntPtr hWnd)
  144. {
  145. CheckIfUsable();
  146. NativeEntrypoints.SteamAPI_IHmd_AttachToWindow(m_hmd,hWnd);
  147. }
  148. public override bool GetTrackerFromHeadPose(float fPredictedSecondsFromNow,out vr::HmdMatrix34_t pmPose,out vr::HmdTrackingResult peResult)
  149. {
  150. CheckIfUsable();
  151. bool result = NativeEntrypoints.SteamAPI_IHmd_GetTrackerFromHeadPose(m_hmd,fPredictedSecondsFromNow,ref pmPose,ref peResult);
  152. return result;
  153. }
  154. public override bool GetLastTrackerFromHeadPose(out vr::HmdMatrix34_t pmPose)
  155. {
  156. CheckIfUsable();
  157. bool result = NativeEntrypoints.SteamAPI_IHmd_GetLastTrackerFromHeadPose(m_hmd,ref pmPose);
  158. return result;
  159. }
  160. public override bool WillDriftInYaw()
  161. {
  162. CheckIfUsable();
  163. bool result = NativeEntrypoints.SteamAPI_IHmd_WillDriftInYaw(m_hmd);
  164. return result;
  165. }
  166. public override void ZeroTracker()
  167. {
  168. CheckIfUsable();
  169. NativeEntrypoints.SteamAPI_IHmd_ZeroTracker(m_hmd);
  170. }
  171. public override vr::HmdMatrix34_t GetTrackerZeroPose()
  172. {
  173. CheckIfUsable();
  174. vr::HmdMatrix34_t result = NativeEntrypoints.SteamAPI_IHmd_GetTrackerZeroPose(m_hmd);
  175. return result;
  176. }
  177. public override uint32_t GetDriverId(string pchBuffer,uint32_t unBufferLen)
  178. {
  179. CheckIfUsable();
  180. uint32_t result = NativeEntrypoints.SteamAPI_IHmd_GetDriverId(m_hmd,pchBuffer,unBufferLen);
  181. return result;
  182. }
  183. public override uint32_t GetDisplayId(string pchBuffer,uint32_t unBufferLen)
  184. {
  185. CheckIfUsable();
  186. uint32_t result = NativeEntrypoints.SteamAPI_IHmd_GetDisplayId(m_hmd,pchBuffer,unBufferLen);
  187. return result;
  188. }
  189. }
  190. public class SteamVRInterop
  191. {
  192. [DllImportAttribute("Steam_api", EntryPoint = "VR_Init")]
  193. internal static extern IntPtr VR_Init(out HmdError peError);
  194. [DllImportAttribute("Steam_api", EntryPoint = "VR_Shutdown")]
  195. internal static extern void VR_Shutdown();
  196. [DllImportAttribute("Steam_api", EntryPoint = "VR_IsHmdPresent")]
  197. internal static extern bool VR_IsHmdPresent();
  198. [DllImportAttribute("Steam_api", EntryPoint = "SteamAPI_UnregisterCallback")]
  199. internal static extern string VR_GetStringForHmdError(HmdError error);
  200. [DllImportAttribute("Steam_api", EntryPoint = "Hmd")]
  201. internal static extern IntPtr Hmd();
  202. }
  203. public enum Hmd_Eye
  204. {
  205. Eye_Left = 0,
  206. Eye_Right = 1,
  207. }
  208. public enum GraphicsAPIConvention
  209. {
  210. API_DirectX = 0,
  211. API_OpenGL = 1,
  212. }
  213. public enum HmdTrackingResult
  214. {
  215. TrackingResult_Uninitialized = 1,
  216. TrackingResult_Calibrating_InProgress = 100,
  217. TrackingResult_Calibrating_OutOfRange = 101,
  218. TrackingResult_Running_OK = 200,
  219. TrackingResult_Running_OutOfRange = 201,
  220. }
  221. public enum HmdError
  222. {
  223. HmdError_None = 0,
  224. HmdError_Init_InstallationNotFound = 100,
  225. HmdError_Init_InstallationCorrupt = 101,
  226. HmdError_Init_VRClientDLLNotFound = 102,
  227. HmdError_Init_FileNotFound = 103,
  228. HmdError_Init_FactoryNotFound = 104,
  229. HmdError_Init_InterfaceNotFound = 105,
  230. HmdError_Init_InvalidInterface = 106,
  231. HmdError_Init_UserConfigDirectoryInvalid = 107,
  232. HmdError_Init_HmdNotFound = 108,
  233. HmdError_Init_NotInitialized = 109,
  234. HmdError_Driver_Failed = 200,
  235. HmdError_Driver_Unknown = 201,
  236. HmdError_Driver_HmdUnknown = 202,
  237. HmdError_Driver_NotLoaded = 203,
  238. HmdError_IPC_ServerInitFailed = 300,
  239. HmdError_IPC_ConnectFailed = 301,
  240. HmdError_IPC_SharedStateInitFailed = 302,
  241. HmdError_VendorSpecific_UnableToConnectToOculusRuntime = 1000,
  242. }
  243. [StructLayout(LayoutKind.Sequential)] public struct HmdMatrix34_t
  244. {
  245. public float m;
  246. }
  247. [StructLayout(LayoutKind.Sequential)] public struct HmdMatrix44_t
  248. {
  249. public float m;
  250. }
  251. [StructLayout(LayoutKind.Sequential)] public struct DistortionCoordinates_t
  252. {
  253. public float rfRed;
  254. public float rfGreen;
  255. public float rfBlue;
  256. }
  257. public class SteamVR
  258. {
  259. public static IHMD Init(out HmdError peError)
  260. {
  261. return new CHMD(VR_Init(out peError));
  262. }
  263. }
  264. }