oculusVRDevice.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. #include "platform/input/oculusVR/oculusVRDevice.h"
  23. #include "platform/platformInput.h"
  24. #include "core/module.h"
  25. #include "console/engineAPI.h"
  26. #include "T3D/gameBase/gameConnection.h"
  27. MODULE_BEGIN( OculusVRDevice )
  28. MODULE_INIT_AFTER( InputEventManager )
  29. MODULE_SHUTDOWN_BEFORE( InputEventManager )
  30. MODULE_INIT
  31. {
  32. OculusVRDevice::staticInit();
  33. ManagedSingleton< OculusVRDevice >::createSingleton();
  34. if(OculusVRDevice::smEnableDevice)
  35. {
  36. OCULUSVRDEV->enable();
  37. }
  38. // Register the device with the Input Event Manager
  39. INPUTMGR->registerDevice(OCULUSVRDEV);
  40. }
  41. MODULE_SHUTDOWN
  42. {
  43. INPUTMGR->unregisterDevice(OCULUSVRDEV);
  44. ManagedSingleton< OculusVRDevice >::deleteSingleton();
  45. }
  46. MODULE_END;
  47. //-----------------------------------------------------------------------------
  48. // OculusVRDevice
  49. //-----------------------------------------------------------------------------
  50. bool OculusVRDevice::smEnableDevice = true;
  51. bool OculusVRDevice::smSimulateHMD = true;
  52. bool OculusVRDevice::smGenerateAngleAxisRotationEvents = true;
  53. bool OculusVRDevice::smGenerateEulerRotationEvents = false;
  54. bool OculusVRDevice::smGenerateRotationAsAxisEvents = false;
  55. F32 OculusVRDevice::smMaximumAxisAngle = 25.0f;
  56. bool OculusVRDevice::smGenerateWholeFrameEvents = false;
  57. OculusVRDevice::OculusVRDevice()
  58. {
  59. // From IInputDevice
  60. dStrcpy(mName, "oculusvr");
  61. mDeviceType = INPUTMGR->getNextDeviceType();
  62. //
  63. mEnabled = false;
  64. mActive = false;
  65. // We don't current support scaling of the input texture. The graphics pipeline will
  66. // need to be modified for this.
  67. mScaleInputTexture = false;
  68. mDeviceManager = NULL;
  69. mListener = NULL;
  70. buildCodeTable();
  71. }
  72. OculusVRDevice::~OculusVRDevice()
  73. {
  74. cleanUp();
  75. }
  76. void OculusVRDevice::staticInit()
  77. {
  78. Con::addVariable("pref::OculusVR::EnableDevice", TypeBool, &smEnableDevice,
  79. "@brief If true, the Oculus VR device will be enabled, if present.\n\n"
  80. "@ingroup Game");
  81. Con::addVariable("OculusVR::GenerateAngleAxisRotationEvents", TypeBool, &smGenerateAngleAxisRotationEvents,
  82. "@brief If true, broadcast sensor rotation events as angled axis.\n\n"
  83. "@ingroup Game");
  84. Con::addVariable("OculusVR::GenerateEulerRotationEvents", TypeBool, &smGenerateEulerRotationEvents,
  85. "@brief If true, broadcast sensor rotation events as Euler angles about the X, Y and Z axis.\n\n"
  86. "@ingroup Game");
  87. Con::addVariable("OculusVR::GenerateRotationAsAxisEvents", TypeBool, &smGenerateRotationAsAxisEvents,
  88. "@brief If true, broadcast sensor rotation as axis events.\n\n"
  89. "@ingroup Game");
  90. Con::addVariable("OculusVR::MaximumAxisAngle", TypeF32, &smMaximumAxisAngle,
  91. "@brief The maximum sensor angle when used as an axis event as measured from a vector pointing straight up (in degrees).\n\n"
  92. "Should range from 0 to 90 degrees.\n\n"
  93. "@ingroup Game");
  94. Con::addVariable("OculusVR::GenerateWholeFrameEvents", TypeBool, &smGenerateWholeFrameEvents,
  95. "@brief Indicates that a whole frame event should be generated and frames should be buffered.\n\n"
  96. "@ingroup Game");
  97. }
  98. void OculusVRDevice::cleanUp()
  99. {
  100. disable();
  101. }
  102. void OculusVRDevice::buildCodeTable()
  103. {
  104. // Build the sensor device code table
  105. OculusVRSensorDevice::buildCodeTable();
  106. }
  107. void OculusVRDevice::addHMDDevice(OVR::HMDDevice* hmd)
  108. {
  109. if(!hmd)
  110. return;
  111. OVR::HMDInfo hmdInfo;
  112. if(!hmd->GetDeviceInfo(&hmdInfo))
  113. return;
  114. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  115. hmdd->set(hmd, hmdInfo, mScaleInputTexture);
  116. mHMDDevices.push_back(hmdd);
  117. Con::printf(" HMD found: %s by %s [v%d]", hmdInfo.ProductName, hmdInfo.Manufacturer, hmdInfo.Version);
  118. }
  119. void OculusVRDevice::createSimulatedHMD()
  120. {
  121. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  122. hmdd->createSimulation(OculusVRHMDDevice::ST_RIFT_PREVIEW, mScaleInputTexture);
  123. mHMDDevices.push_back(hmdd);
  124. Con::printf(" HMD simulated: %s by %s [v%d]", hmdd->getProductName(), hmdd->getManufacturer(), hmdd->getVersion());
  125. }
  126. void OculusVRDevice::addSensorDevice(OVR::SensorDevice* sensor)
  127. {
  128. if(!sensor)
  129. return;
  130. OVR::SensorInfo sensorInfo;
  131. if(!sensor->GetDeviceInfo(&sensorInfo))
  132. return;
  133. OculusVRSensorDevice* sensord = new OculusVRSensorDevice();
  134. sensord->set(sensor, sensorInfo, mSensorDevices.size());
  135. mSensorDevices.push_back(sensord);
  136. Con::printf(" Sensor found: %s by %s [v%d] %s", sensorInfo.ProductName, sensorInfo.Manufacturer, sensorInfo.Version, sensorInfo.SerialNumber);
  137. }
  138. void OculusVRDevice::createSimulatedSensor()
  139. {
  140. OculusVRSensorDevice* sensord = new OculusVRSensorDevice();
  141. sensord->createSimulation(OculusVRSensorDevice::ST_RIFT_PREVIEW, mSensorDevices.size());
  142. mSensorDevices.push_back(sensord);
  143. Con::printf(" Sensor simulated: %s by %s [v%d] %s", sensord->getProductName(), sensord->getManufacturer(), sensord->getVersion(), sensord->getSerialNumber());
  144. }
  145. bool OculusVRDevice::enable()
  146. {
  147. // Start off with disabling the device if it is already enabled
  148. disable();
  149. Con::printf("Oculus VR Device Init:");
  150. OVR::System::Init(OVR::Log::ConfigureDefaultLog(OVR::LogMask_All));
  151. if(OVR::System::IsInitialized())
  152. {
  153. mEnabled = true;
  154. // Create the OVR device manager
  155. mDeviceManager = OVR::DeviceManager::Create();
  156. if(!mDeviceManager)
  157. {
  158. if(smSimulateHMD)
  159. {
  160. Con::printf(" Could not create a HMD device manager. Simulating a HMD.");
  161. Con::printf(" ");
  162. createSimulatedHMD();
  163. createSimulatedSensor();
  164. setActive(true);
  165. return true;
  166. }
  167. else
  168. {
  169. Con::printf(" Could not create a HMD device manager.");
  170. Con::printf(" ");
  171. mEnabled = false;
  172. OVR::System::Destroy();
  173. return false;
  174. }
  175. }
  176. // Provide a message listener
  177. // NOTE: Commented out as non-functional in 0.1.2
  178. //mListener = new DeviceListener(this);
  179. //mDeviceManager->SetMessageHandler(mListener);
  180. // Enumerate HMDs and pick the first one
  181. OVR::HMDDevice* hmd = mDeviceManager->EnumerateDevices<OVR::HMDDevice>().CreateDevice();
  182. if(hmd)
  183. {
  184. // Add the HMD to our list
  185. addHMDDevice(hmd);
  186. // Detect and add any sensor on the HMD
  187. OVR::SensorDevice* sensor = hmd->GetSensor();
  188. if(sensor)
  189. {
  190. addSensorDevice(sensor);
  191. }
  192. else
  193. {
  194. Con::printf(" No sensor device on HMD.");
  195. }
  196. setActive(true);
  197. }
  198. else
  199. {
  200. if(smSimulateHMD)
  201. {
  202. Con::printf(" Could not enumerate a HMD device. Simulating a HMD.");
  203. createSimulatedHMD();
  204. createSimulatedSensor();
  205. setActive(true);
  206. }
  207. else
  208. {
  209. Con::printf(" Could not enumerate a HMD device.");
  210. }
  211. }
  212. }
  213. Con::printf(" ");
  214. return false;
  215. }
  216. void OculusVRDevice::disable()
  217. {
  218. for(U32 i=0; i<mSensorDevices.size(); ++i)
  219. {
  220. delete mSensorDevices[i];
  221. }
  222. mSensorDevices.clear();
  223. for(U32 i=0; i<mHMDDevices.size(); ++i)
  224. {
  225. delete mHMDDevices[i];
  226. }
  227. mHMDDevices.clear();
  228. if(mDeviceManager)
  229. {
  230. mDeviceManager->Release();
  231. mDeviceManager = NULL;
  232. }
  233. if(mEnabled)
  234. {
  235. OVR::System::Destroy();
  236. }
  237. if(mListener)
  238. {
  239. delete mListener;
  240. mListener = NULL;
  241. }
  242. setActive(false);
  243. mEnabled = false;
  244. }
  245. bool OculusVRDevice::process()
  246. {
  247. if(!mEnabled)
  248. return false;
  249. if(!getActive())
  250. return false;
  251. //Build the maximum axis angle to be passed into the sensor process()
  252. F32 maxAxisRadius = mSin(mDegToRad(smMaximumAxisAngle));
  253. // Process each sensor
  254. for(U32 i=0; i<mSensorDevices.size(); ++i)
  255. {
  256. mSensorDevices[i]->process(mDeviceType, smGenerateAngleAxisRotationEvents, smGenerateEulerRotationEvents, smGenerateRotationAsAxisEvents, maxAxisRadius);
  257. }
  258. return true;
  259. }
  260. //-----------------------------------------------------------------------------
  261. bool OculusVRDevice::providesYFOV() const
  262. {
  263. if(!mHMDDevices.size())
  264. return false;
  265. return true;
  266. }
  267. F32 OculusVRDevice::getYFOV() const
  268. {
  269. if(!mHMDDevices.size())
  270. return 0.0f;
  271. const OculusVRHMDDevice* hmd = getHMDDevice(0);
  272. if(!hmd)
  273. return 0.0f;
  274. return hmd->getYFOV();
  275. }
  276. bool OculusVRDevice::providesEyeOffset() const
  277. {
  278. if(!mHMDDevices.size())
  279. return false;
  280. return true;
  281. }
  282. const Point3F& OculusVRDevice::getEyeOffset() const
  283. {
  284. if(!mHMDDevices.size())
  285. return Point3F::Zero;
  286. const OculusVRHMDDevice* hmd = getHMDDevice(0);
  287. if(!hmd)
  288. return Point3F::Zero;
  289. return hmd->getEyeWorldOffset();
  290. }
  291. bool OculusVRDevice::providesProjectionOffset() const
  292. {
  293. if(!mHMDDevices.size())
  294. return false;
  295. return true;
  296. }
  297. const Point2F& OculusVRDevice::getProjectionOffset() const
  298. {
  299. if(!mHMDDevices.size())
  300. return Point2F::Zero;
  301. const OculusVRHMDDevice* hmd = getHMDDevice(0);
  302. if(!hmd)
  303. return Point2F::Zero;
  304. return hmd->getProjectionCenterOffset();
  305. }
  306. //-----------------------------------------------------------------------------
  307. const OculusVRHMDDevice* OculusVRDevice::getHMDDevice(U32 index) const
  308. {
  309. if(index >= mHMDDevices.size())
  310. return NULL;
  311. return mHMDDevices[index];
  312. }
  313. //-----------------------------------------------------------------------------
  314. const OculusVRSensorDevice* OculusVRDevice::getSensorDevice(U32 index) const
  315. {
  316. if(index >= mSensorDevices.size())
  317. return NULL;
  318. return mSensorDevices[index];
  319. }
  320. EulerF OculusVRDevice::getSensorEulerRotation(U32 index)
  321. {
  322. if(index >= mSensorDevices.size())
  323. return Point3F::Zero;
  324. return mSensorDevices[index]->getEulerRotation();
  325. }
  326. F32 OculusVRDevice::getSensorPredictionTime(U32 index)
  327. {
  328. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  329. if(!sensor || !sensor->isValid())
  330. return 0.0f;
  331. return sensor->getPredictionTime();
  332. }
  333. void OculusVRDevice::setSensorPredictionTime(U32 index, F32 dt)
  334. {
  335. if(index >= mSensorDevices.size())
  336. return;
  337. OculusVRSensorDevice* sensor = mSensorDevices[index];
  338. if(!sensor->isValid())
  339. return;
  340. sensor->setPredictionTime(dt);
  341. }
  342. void OculusVRDevice::setAllSensorPredictionTime(F32 dt)
  343. {
  344. for(U32 i=0; i<mSensorDevices.size(); ++i)
  345. {
  346. mSensorDevices[i]->setPredictionTime(dt);
  347. }
  348. }
  349. void OculusVRDevice::resetAllSensors()
  350. {
  351. // Reset each sensor
  352. for(U32 i=0; i<mSensorDevices.size(); ++i)
  353. {
  354. mSensorDevices[i]->reset();
  355. }
  356. }
  357. //-----------------------------------------------------------------------------
  358. void OculusVRDevice::DeviceListener::OnMessage(const OVR::Message& msg)
  359. {
  360. switch(msg.Type)
  361. {
  362. case OVR::Message_DeviceAdded:
  363. {
  364. const OVR::MessageDeviceStatus* status = static_cast<const OVR::MessageDeviceStatus*>(&msg);
  365. Con::printf("OVR: Device added of type: %d", status->Handle.GetType());
  366. }
  367. break;
  368. case OVR::Message_DeviceRemoved:
  369. Con::printf("OVR: Device removed of type: %d", msg.pDevice->GetType());
  370. break;
  371. default:
  372. break;
  373. }
  374. }
  375. //-----------------------------------------------------------------------------
  376. DefineEngineFunction(isOculusVRDeviceActive, bool, (),,
  377. "@brief Used to determine if the Oculus VR input device is active\n\n"
  378. "The Oculus VR device is considered active when the library has been "
  379. "initialized and either a real of simulated HMD is present.\n\n"
  380. "@return True if the Oculus VR input device is active.\n"
  381. "@ingroup Game")
  382. {
  383. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  384. {
  385. return false;
  386. }
  387. return OCULUSVRDEV->getActive();
  388. }
  389. //-----------------------------------------------------------------------------
  390. DefineEngineFunction(setOVRHMDAsGameConnectionDisplayDevice, bool, (GameConnection* conn),,
  391. "@brief Sets the first HMD to be a GameConnection's display device\n\n"
  392. "@param conn The GameConnection to set.\n"
  393. "@return True if the GameConnection display device was set.\n"
  394. "@ingroup Game")
  395. {
  396. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  397. {
  398. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): No Oculus VR Device present.");
  399. return false;
  400. }
  401. if(!conn)
  402. {
  403. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): Invalid GameConnection.");
  404. return false;
  405. }
  406. conn->setDisplayDevice(OCULUSVRDEV);
  407. return true;
  408. }
  409. //-----------------------------------------------------------------------------
  410. DefineEngineFunction(getOVRHMDCount, S32, (),,
  411. "@brief Get the number of HMD devices that are currently connected.\n\n"
  412. "@return The number of Oculus VR HMD devices that are currently connected.\n"
  413. "@ingroup Game")
  414. {
  415. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  416. {
  417. return 0;
  418. }
  419. return OCULUSVRDEV->getHMDCount();
  420. }
  421. DefineEngineFunction(isOVRHMDSimulated, bool, (S32 index),,
  422. "@brief Determines if the requested OVR HMD is simulated or real.\n\n"
  423. "@param index The HMD index.\n"
  424. "@return True if the HMD is simulated.\n"
  425. "@ingroup Game")
  426. {
  427. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  428. {
  429. return true;
  430. }
  431. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  432. if(!hmd)
  433. {
  434. return true;
  435. }
  436. return hmd->isSimulated();
  437. }
  438. DefineEngineFunction(getOVRHMDProductName, const char*, (S32 index),,
  439. "@brief Retrieves the HMD product name.\n\n"
  440. "@param index The HMD index.\n"
  441. "@return The name of the HMD product.\n"
  442. "@ingroup Game")
  443. {
  444. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  445. {
  446. return "";
  447. }
  448. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  449. if(!hmd)
  450. {
  451. return "";
  452. }
  453. return hmd->getProductName();
  454. }
  455. DefineEngineFunction(getOVRHMDManufacturer, const char*, (S32 index),,
  456. "@brief Retrieves the HMD manufacturer name.\n\n"
  457. "@param index The HMD index.\n"
  458. "@return The manufacturer of the HMD product.\n"
  459. "@ingroup Game")
  460. {
  461. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  462. {
  463. return "";
  464. }
  465. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  466. if(!hmd)
  467. {
  468. return "";
  469. }
  470. return hmd->getManufacturer();
  471. }
  472. DefineEngineFunction(getOVRHMDVersion, S32, (S32 index),,
  473. "@brief Retrieves the HMD version number.\n\n"
  474. "@param index The HMD index.\n"
  475. "@return The version number of the HMD product.\n"
  476. "@ingroup Game")
  477. {
  478. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  479. {
  480. return -1;
  481. }
  482. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  483. if(!hmd)
  484. {
  485. return -1;
  486. }
  487. return hmd->getVersion();
  488. }
  489. DefineEngineFunction(getOVRHMDDisplayDeviceName, const char*, (S32 index),,
  490. "@brief Windows display device name used in EnumDisplaySettings/CreateDC.\n\n"
  491. "@param index The HMD index.\n"
  492. "@return The name of the HMD display device, if any.\n"
  493. "@ingroup Game")
  494. {
  495. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  496. {
  497. return "";
  498. }
  499. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  500. if(!hmd)
  501. {
  502. return "";
  503. }
  504. return hmd->getDisplayDeviceName();
  505. }
  506. DefineEngineFunction(getOVRHMDResolution, Point2I, (S32 index),,
  507. "@brief Provides the OVR HMD screen resolution.\n\n"
  508. "@param index The HMD index.\n"
  509. "@return A two component string with the screen's resolution.\n"
  510. "@ingroup Game")
  511. {
  512. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  513. {
  514. return Point2I(1280, 800);
  515. }
  516. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  517. if(!hmd)
  518. {
  519. return Point2I(1280, 800);
  520. }
  521. return hmd->getResolution();
  522. }
  523. DefineEngineFunction(getOVRHMDDistortionCoefficients, String, (S32 index),,
  524. "@brief Provides the OVR HMD distortion coefficients.\n\n"
  525. "@param index The HMD index.\n"
  526. "@return A four component string with the distortion coefficients.\n"
  527. "@ingroup Game")
  528. {
  529. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  530. {
  531. return "0 0 0 0";
  532. }
  533. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  534. if(!hmd)
  535. {
  536. return "0 0 0 0";
  537. }
  538. const Point4F& k = hmd->getKDistortion();
  539. char buf[256];
  540. dSprintf(buf, 256, "%g %g %g %g", k.x, k.y, k.z, k.w);
  541. return buf;
  542. }
  543. DefineEngineFunction(getOVRHMDEyeXOffsets, Point2F, (S32 index),,
  544. "@brief Provides the OVR HMD eye x offsets in uv coordinates.\n\n"
  545. "@param index The HMD index.\n"
  546. "@return A two component string with the left and right eye x offsets.\n"
  547. "@ingroup Game")
  548. {
  549. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  550. {
  551. return Point2F(0.5, 0.5);
  552. }
  553. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  554. if(!hmd)
  555. {
  556. return Point2F(0.5, 0.5);
  557. }
  558. // X component is left, Y component is right
  559. const Point2F& offset = hmd->getEyeUVOffset();
  560. return offset;
  561. }
  562. DefineEngineFunction(getOVRHMDXCenterOffset, F32, (S32 index),,
  563. "@brief Provides the OVR HMD calculated XCenterOffset.\n\n"
  564. "@param index The HMD index.\n"
  565. "@return The calculated XCenterOffset.\n"
  566. "@ingroup Game")
  567. {
  568. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  569. {
  570. return 0.0f;
  571. }
  572. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  573. if(!hmd)
  574. {
  575. return 0.0f;
  576. }
  577. F32 offset = hmd->getCenterOffset();
  578. return offset;
  579. }
  580. DefineEngineFunction(getOVRHMDDistortionScale, F32, (S32 index),,
  581. "@brief Provides the OVR HMD calculated distortion scale.\n\n"
  582. "@param index The HMD index.\n"
  583. "@return The calculated distortion scale.\n"
  584. "@ingroup Game")
  585. {
  586. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  587. {
  588. return 1.0f;
  589. }
  590. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  591. if(!hmd)
  592. {
  593. return 1.0f;
  594. }
  595. F32 scale = hmd->getDistortionScale();
  596. return scale;
  597. }
  598. DefineEngineFunction(getOVRHMDYFOV, F32, (S32 index),,
  599. "@brief Provides the OVR HMD calculated Y FOV.\n\n"
  600. "@param index The HMD index.\n"
  601. "@return The calculated Y FOV.\n"
  602. "@ingroup Game")
  603. {
  604. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  605. {
  606. return 1.0f;
  607. }
  608. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  609. if(!hmd)
  610. {
  611. return 1.0f;
  612. }
  613. F32 fov = hmd->getYFOV();
  614. return mRadToDeg(fov);
  615. }
  616. //-----------------------------------------------------------------------------
  617. DefineEngineFunction(getOVRSensorCount, S32, (),,
  618. "@brief Get the number of sensor devices that are currently connected.\n\n"
  619. "@return The number of Oculus VR sensor devices that are currently connected.\n"
  620. "@ingroup Game")
  621. {
  622. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  623. {
  624. return 0;
  625. }
  626. return OCULUSVRDEV->getSensorCount();
  627. }
  628. DefineEngineFunction(getOVRSensorEulerRotation, Point3F, (S32 index),,
  629. "@brief Get the Euler rotation values for the given sensor index.\n\n"
  630. "@param index The sensor index.\n"
  631. "@return The Euler rotation values of the Oculus VR sensor, in degrees.\n"
  632. "@ingroup Game")
  633. {
  634. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  635. {
  636. return Point3F::Zero;
  637. }
  638. EulerF rot = OCULUSVRDEV->getSensorEulerRotation(index);
  639. return Point3F(mRadToDeg(rot.x), mRadToDeg(rot.y), mRadToDeg(rot.z));
  640. }
  641. DefineEngineFunction(getOVRSensorPredictionTime, F32, (S32 index),,
  642. "@brief Get the prediction time set for the given sensor index.\n\n"
  643. "@param index The sensor index.\n"
  644. "@return The prediction time of the Oculus VR sensor, given in seconds.\n"
  645. "@ingroup Game")
  646. {
  647. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  648. {
  649. return 0;
  650. }
  651. return OCULUSVRDEV->getSensorPredictionTime(index);
  652. }
  653. DefineEngineFunction(setSensorPredictionTime, void, (S32 index, F32 dt),,
  654. "@brief Set the prediction time set for the given sensor index.\n\n"
  655. "@param index The sensor index.\n"
  656. "@param dt The prediction time to set given in seconds. Setting to 0 disables prediction.\n"
  657. "@ingroup Game")
  658. {
  659. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  660. {
  661. return;
  662. }
  663. OCULUSVRDEV->setSensorPredictionTime(index, dt);
  664. }
  665. DefineEngineFunction(setAllSensorPredictionTime, void, (F32 dt),,
  666. "@brief Set the prediction time set for all sensors.\n\n"
  667. "@param dt The prediction time to set given in seconds. Setting to 0 disables prediction.\n"
  668. "@ingroup Game")
  669. {
  670. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  671. {
  672. return;
  673. }
  674. OCULUSVRDEV->setAllSensorPredictionTime(dt);
  675. }
  676. DefineEngineFunction(ovrResetAllSensors, void, (),,
  677. "@brief Resets all Oculus VR sensors.\n\n"
  678. "This resets all sensor orientations such that their 'normal' rotation "
  679. "is defined when this function is called. This defines an HMD's forwards "
  680. "and up direction, for example."
  681. "@ingroup Game")
  682. {
  683. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  684. {
  685. return;
  686. }
  687. OCULUSVRDEV->resetAllSensors();
  688. }