oculusVRDevice.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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::smUseChromaticAberrationCorrection = true;
  53. bool OculusVRDevice::smGenerateAngleAxisRotationEvents = true;
  54. bool OculusVRDevice::smGenerateEulerRotationEvents = false;
  55. bool OculusVRDevice::smGenerateRotationAsAxisEvents = false;
  56. F32 OculusVRDevice::smMaximumAxisAngle = 25.0f;
  57. bool OculusVRDevice::smGenerateSensorRawEvents = false;
  58. bool OculusVRDevice::smGenerateWholeFrameEvents = false;
  59. OculusVRDevice::OculusVRDevice()
  60. {
  61. // From IInputDevice
  62. dStrcpy(mName, "oculusvr");
  63. mDeviceType = INPUTMGR->getNextDeviceType();
  64. //
  65. mEnabled = false;
  66. mActive = false;
  67. // We don't current support scaling of the input texture. The graphics pipeline will
  68. // need to be modified for this.
  69. mScaleInputTexture = false;
  70. mDeviceManager = NULL;
  71. mListener = NULL;
  72. buildCodeTable();
  73. }
  74. OculusVRDevice::~OculusVRDevice()
  75. {
  76. cleanUp();
  77. }
  78. void OculusVRDevice::staticInit()
  79. {
  80. Con::addVariable("pref::OculusVR::EnableDevice", TypeBool, &smEnableDevice,
  81. "@brief If true, the Oculus VR device will be enabled, if present.\n\n"
  82. "@ingroup Game");
  83. Con::addVariable("pref::OculusVR::UseChromaticAberrationCorrection", TypeBool, &smUseChromaticAberrationCorrection,
  84. "@brief If true, Use the chromatic aberration correction version of the Oculus VR barrel distortion shader.\n\n"
  85. "@ingroup Game");
  86. Con::addVariable("OculusVR::GenerateAngleAxisRotationEvents", TypeBool, &smGenerateAngleAxisRotationEvents,
  87. "@brief If true, broadcast sensor rotation events as angled axis.\n\n"
  88. "@ingroup Game");
  89. Con::addVariable("OculusVR::GenerateEulerRotationEvents", TypeBool, &smGenerateEulerRotationEvents,
  90. "@brief If true, broadcast sensor rotation events as Euler angles about the X, Y and Z axis.\n\n"
  91. "@ingroup Game");
  92. Con::addVariable("OculusVR::GenerateRotationAsAxisEvents", TypeBool, &smGenerateRotationAsAxisEvents,
  93. "@brief If true, broadcast sensor rotation as axis events.\n\n"
  94. "@ingroup Game");
  95. Con::addVariable("OculusVR::MaximumAxisAngle", TypeF32, &smMaximumAxisAngle,
  96. "@brief The maximum sensor angle when used as an axis event as measured from a vector pointing straight up (in degrees).\n\n"
  97. "Should range from 0 to 90 degrees.\n\n"
  98. "@ingroup Game");
  99. Con::addVariable("OculusVR::GenerateSensorRawEvents", TypeBool, &smGenerateSensorRawEvents,
  100. "@brief If ture, broadcast sensor raw data: acceleration, angular velocity, magnetometer reading.\n\n"
  101. "@ingroup Game");
  102. Con::addVariable("OculusVR::GenerateWholeFrameEvents", TypeBool, &smGenerateWholeFrameEvents,
  103. "@brief Indicates that a whole frame event should be generated and frames should be buffered.\n\n"
  104. "@ingroup Game");
  105. }
  106. void OculusVRDevice::cleanUp()
  107. {
  108. disable();
  109. }
  110. void OculusVRDevice::buildCodeTable()
  111. {
  112. // Build the sensor device code table
  113. OculusVRSensorDevice::buildCodeTable();
  114. }
  115. void OculusVRDevice::addHMDDevice(OVR::HMDDevice* hmd)
  116. {
  117. if(!hmd)
  118. return;
  119. OVR::HMDInfo hmdInfo;
  120. if(!hmd->GetDeviceInfo(&hmdInfo))
  121. return;
  122. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  123. hmdd->set(hmd, hmdInfo, mScaleInputTexture);
  124. mHMDDevices.push_back(hmdd);
  125. Con::printf(" HMD found: %s by %s [v%d]", hmdInfo.ProductName, hmdInfo.Manufacturer, hmdInfo.Version);
  126. }
  127. void OculusVRDevice::createSimulatedHMD()
  128. {
  129. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  130. hmdd->createSimulation(OculusVRHMDDevice::ST_RIFT_PREVIEW, mScaleInputTexture);
  131. mHMDDevices.push_back(hmdd);
  132. Con::printf(" HMD simulated: %s by %s [v%d]", hmdd->getProductName(), hmdd->getManufacturer(), hmdd->getVersion());
  133. }
  134. void OculusVRDevice::addSensorDevice(OVR::SensorDevice* sensor)
  135. {
  136. if(!sensor)
  137. return;
  138. OVR::SensorInfo sensorInfo;
  139. if(!sensor->GetDeviceInfo(&sensorInfo))
  140. return;
  141. OculusVRSensorDevice* sensord = new OculusVRSensorDevice();
  142. sensord->set(sensor, sensorInfo, mSensorDevices.size());
  143. mSensorDevices.push_back(sensord);
  144. Con::printf(" Sensor found: %s by %s [v%d] %s", sensorInfo.ProductName, sensorInfo.Manufacturer, sensorInfo.Version, sensorInfo.SerialNumber);
  145. }
  146. void OculusVRDevice::createSimulatedSensor()
  147. {
  148. OculusVRSensorDevice* sensord = new OculusVRSensorDevice();
  149. sensord->createSimulation(OculusVRSensorDevice::ST_RIFT_PREVIEW, mSensorDevices.size());
  150. mSensorDevices.push_back(sensord);
  151. Con::printf(" Sensor simulated: %s by %s [v%d] %s", sensord->getProductName(), sensord->getManufacturer(), sensord->getVersion(), sensord->getSerialNumber());
  152. }
  153. bool OculusVRDevice::enable()
  154. {
  155. // Start off with disabling the device if it is already enabled
  156. disable();
  157. Con::printf("Oculus VR Device Init:");
  158. OVR::System::Init(OVR::Log::ConfigureDefaultLog(OVR::LogMask_All));
  159. if(OVR::System::IsInitialized())
  160. {
  161. mEnabled = true;
  162. // Create the OVR device manager
  163. mDeviceManager = OVR::DeviceManager::Create();
  164. if(!mDeviceManager)
  165. {
  166. if(smSimulateHMD)
  167. {
  168. Con::printf(" Could not create a HMD device manager. Simulating a HMD.");
  169. Con::printf(" ");
  170. createSimulatedHMD();
  171. createSimulatedSensor();
  172. setActive(true);
  173. return true;
  174. }
  175. else
  176. {
  177. Con::printf(" Could not create a HMD device manager.");
  178. Con::printf(" ");
  179. mEnabled = false;
  180. OVR::System::Destroy();
  181. return false;
  182. }
  183. }
  184. // Provide a message listener
  185. // NOTE: Commented out as non-functional in 0.1.2
  186. //mListener = new DeviceListener(this);
  187. //mDeviceManager->SetMessageHandler(mListener);
  188. // Enumerate HMDs and pick the first one
  189. OVR::HMDDevice* hmd = mDeviceManager->EnumerateDevices<OVR::HMDDevice>().CreateDevice();
  190. if(hmd)
  191. {
  192. // Add the HMD to our list
  193. addHMDDevice(hmd);
  194. // Detect and add any sensor on the HMD
  195. OVR::SensorDevice* sensor = hmd->GetSensor();
  196. if(sensor)
  197. {
  198. addSensorDevice(sensor);
  199. }
  200. else
  201. {
  202. Con::printf(" No sensor device on HMD.");
  203. }
  204. setActive(true);
  205. }
  206. else
  207. {
  208. if(smSimulateHMD)
  209. {
  210. Con::printf(" Could not enumerate a HMD device. Simulating a HMD.");
  211. createSimulatedHMD();
  212. createSimulatedSensor();
  213. setActive(true);
  214. }
  215. else
  216. {
  217. Con::printf(" Could not enumerate a HMD device.");
  218. }
  219. }
  220. }
  221. Con::printf(" ");
  222. return false;
  223. }
  224. void OculusVRDevice::disable()
  225. {
  226. for(U32 i=0; i<mSensorDevices.size(); ++i)
  227. {
  228. delete mSensorDevices[i];
  229. }
  230. mSensorDevices.clear();
  231. for(U32 i=0; i<mHMDDevices.size(); ++i)
  232. {
  233. delete mHMDDevices[i];
  234. }
  235. mHMDDevices.clear();
  236. if(mDeviceManager)
  237. {
  238. mDeviceManager->Release();
  239. mDeviceManager = NULL;
  240. }
  241. if(mEnabled)
  242. {
  243. OVR::System::Destroy();
  244. }
  245. if(mListener)
  246. {
  247. delete mListener;
  248. mListener = NULL;
  249. }
  250. setActive(false);
  251. mEnabled = false;
  252. }
  253. bool OculusVRDevice::process()
  254. {
  255. if(!mEnabled)
  256. return false;
  257. if(!getActive())
  258. return false;
  259. //Build the maximum axis angle to be passed into the sensor process()
  260. F32 maxAxisRadius = mSin(mDegToRad(smMaximumAxisAngle));
  261. // Process each sensor
  262. for(U32 i=0; i<mSensorDevices.size(); ++i)
  263. {
  264. mSensorDevices[i]->process(mDeviceType, smGenerateAngleAxisRotationEvents, smGenerateEulerRotationEvents, smGenerateRotationAsAxisEvents, maxAxisRadius, smGenerateSensorRawEvents);
  265. }
  266. return true;
  267. }
  268. //-----------------------------------------------------------------------------
  269. bool OculusVRDevice::providesYFOV() const
  270. {
  271. if(!mHMDDevices.size())
  272. return false;
  273. return true;
  274. }
  275. F32 OculusVRDevice::getYFOV() const
  276. {
  277. if(!mHMDDevices.size())
  278. return 0.0f;
  279. const OculusVRHMDDevice* hmd = getHMDDevice(0);
  280. if(!hmd)
  281. return 0.0f;
  282. return hmd->getYFOV();
  283. }
  284. bool OculusVRDevice::providesEyeOffset() const
  285. {
  286. if(!mHMDDevices.size())
  287. return false;
  288. return true;
  289. }
  290. const Point3F& OculusVRDevice::getEyeOffset() const
  291. {
  292. if(!mHMDDevices.size())
  293. return Point3F::Zero;
  294. const OculusVRHMDDevice* hmd = getHMDDevice(0);
  295. if(!hmd)
  296. return Point3F::Zero;
  297. return hmd->getEyeWorldOffset();
  298. }
  299. bool OculusVRDevice::providesProjectionOffset() const
  300. {
  301. if(!mHMDDevices.size())
  302. return false;
  303. return true;
  304. }
  305. const Point2F& OculusVRDevice::getProjectionOffset() const
  306. {
  307. if(!mHMDDevices.size())
  308. return Point2F::Zero;
  309. const OculusVRHMDDevice* hmd = getHMDDevice(0);
  310. if(!hmd)
  311. return Point2F::Zero;
  312. return hmd->getProjectionCenterOffset();
  313. }
  314. //-----------------------------------------------------------------------------
  315. const OculusVRHMDDevice* OculusVRDevice::getHMDDevice(U32 index) const
  316. {
  317. if(index >= mHMDDevices.size())
  318. return NULL;
  319. return mHMDDevices[index];
  320. }
  321. F32 OculusVRDevice::getHMDCurrentIPD(U32 index)
  322. {
  323. if(index >= mHMDDevices.size())
  324. return -1.0f;
  325. return mHMDDevices[index]->getIPD();
  326. }
  327. void OculusVRDevice::setHMDCurrentIPD(U32 index, F32 ipd)
  328. {
  329. if(index >= mHMDDevices.size())
  330. return;
  331. return mHMDDevices[index]->setIPD(ipd, mScaleInputTexture);
  332. }
  333. //-----------------------------------------------------------------------------
  334. const OculusVRSensorDevice* OculusVRDevice::getSensorDevice(U32 index) const
  335. {
  336. if(index >= mSensorDevices.size())
  337. return NULL;
  338. return mSensorDevices[index];
  339. }
  340. EulerF OculusVRDevice::getSensorEulerRotation(U32 index)
  341. {
  342. if(index >= mSensorDevices.size())
  343. return Point3F::Zero;
  344. return mSensorDevices[index]->getEulerRotation();
  345. }
  346. VectorF OculusVRDevice::getSensorAcceleration(U32 index)
  347. {
  348. if(index >= mSensorDevices.size())
  349. return Point3F::Zero;
  350. return mSensorDevices[index]->getAcceleration();
  351. }
  352. EulerF OculusVRDevice::getSensorAngularVelocity(U32 index)
  353. {
  354. if(index >= mSensorDevices.size())
  355. return Point3F::Zero;
  356. return mSensorDevices[index]->getAngularVelocity();
  357. }
  358. VectorF OculusVRDevice::getSensorMagnetometer(U32 index)
  359. {
  360. if(index >= mSensorDevices.size())
  361. return Point3F::Zero;
  362. return mSensorDevices[index]->getMagnetometer();
  363. }
  364. F32 OculusVRDevice::getSensorPredictionTime(U32 index)
  365. {
  366. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  367. if(!sensor || !sensor->isValid())
  368. return 0.0f;
  369. return sensor->getPredictionTime();
  370. }
  371. void OculusVRDevice::setSensorPredictionTime(U32 index, F32 dt)
  372. {
  373. if(index >= mSensorDevices.size())
  374. return;
  375. OculusVRSensorDevice* sensor = mSensorDevices[index];
  376. if(!sensor->isValid())
  377. return;
  378. sensor->setPredictionTime(dt);
  379. }
  380. void OculusVRDevice::setAllSensorPredictionTime(F32 dt)
  381. {
  382. for(U32 i=0; i<mSensorDevices.size(); ++i)
  383. {
  384. mSensorDevices[i]->setPredictionTime(dt);
  385. }
  386. }
  387. bool OculusVRDevice::getSensorGravityCorrection(U32 index)
  388. {
  389. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  390. if(!sensor || !sensor->isValid())
  391. return false;
  392. return sensor->getGravityCorrection();
  393. }
  394. void OculusVRDevice::setSensorGravityCorrection(U32 index, bool state)
  395. {
  396. if(index >= mSensorDevices.size())
  397. return;
  398. OculusVRSensorDevice* sensor = mSensorDevices[index];
  399. if(!sensor->isValid())
  400. return;
  401. sensor->setGravityCorrection(state);
  402. }
  403. bool OculusVRDevice::getSensorYawCorrection(U32 index)
  404. {
  405. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  406. if(!sensor || !sensor->isValid())
  407. return false;
  408. return sensor->getYawCorrection();
  409. }
  410. void OculusVRDevice::setSensorYawCorrection(U32 index, bool state)
  411. {
  412. if(index >= mSensorDevices.size())
  413. return;
  414. OculusVRSensorDevice* sensor = mSensorDevices[index];
  415. if(!sensor->isValid())
  416. return;
  417. sensor->setYawCorrection(state);
  418. }
  419. bool OculusVRDevice::getSensorMagnetometerCalibrated(U32 index)
  420. {
  421. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  422. if(!sensor || !sensor->isValid())
  423. return false;
  424. return sensor->getMagnetometerCalibrationAvailable();
  425. }
  426. void OculusVRDevice::resetAllSensors()
  427. {
  428. // Reset each sensor
  429. for(U32 i=0; i<mSensorDevices.size(); ++i)
  430. {
  431. mSensorDevices[i]->reset();
  432. }
  433. }
  434. //-----------------------------------------------------------------------------
  435. void OculusVRDevice::DeviceListener::OnMessage(const OVR::Message& msg)
  436. {
  437. switch(msg.Type)
  438. {
  439. case OVR::Message_DeviceAdded:
  440. {
  441. const OVR::MessageDeviceStatus* status = static_cast<const OVR::MessageDeviceStatus*>(&msg);
  442. Con::printf("OVR: Device added of type: %d", status->Handle.GetType());
  443. }
  444. break;
  445. case OVR::Message_DeviceRemoved:
  446. Con::printf("OVR: Device removed of type: %d", msg.pDevice->GetType());
  447. break;
  448. default:
  449. break;
  450. }
  451. }
  452. //-----------------------------------------------------------------------------
  453. DefineEngineFunction(isOculusVRDeviceActive, bool, (),,
  454. "@brief Used to determine if the Oculus VR input device is active\n\n"
  455. "The Oculus VR device is considered active when the library has been "
  456. "initialized and either a real of simulated HMD is present.\n\n"
  457. "@return True if the Oculus VR input device is active.\n"
  458. "@ingroup Game")
  459. {
  460. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  461. {
  462. return false;
  463. }
  464. return OCULUSVRDEV->getActive();
  465. }
  466. //-----------------------------------------------------------------------------
  467. DefineEngineFunction(setOVRHMDAsGameConnectionDisplayDevice, bool, (GameConnection* conn),,
  468. "@brief Sets the first HMD to be a GameConnection's display device\n\n"
  469. "@param conn The GameConnection to set.\n"
  470. "@return True if the GameConnection display device was set.\n"
  471. "@ingroup Game")
  472. {
  473. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  474. {
  475. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): No Oculus VR Device present.");
  476. return false;
  477. }
  478. if(!conn)
  479. {
  480. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): Invalid GameConnection.");
  481. return false;
  482. }
  483. conn->setDisplayDevice(OCULUSVRDEV);
  484. return true;
  485. }
  486. //-----------------------------------------------------------------------------
  487. DefineEngineFunction(getOVRHMDCount, S32, (),,
  488. "@brief Get the number of HMD devices that are currently connected.\n\n"
  489. "@return The number of Oculus VR HMD devices that are currently connected.\n"
  490. "@ingroup Game")
  491. {
  492. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  493. {
  494. return 0;
  495. }
  496. return OCULUSVRDEV->getHMDCount();
  497. }
  498. DefineEngineFunction(isOVRHMDSimulated, bool, (S32 index),,
  499. "@brief Determines if the requested OVR HMD is simulated or real.\n\n"
  500. "@param index The HMD index.\n"
  501. "@return True if the HMD is simulated.\n"
  502. "@ingroup Game")
  503. {
  504. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  505. {
  506. return true;
  507. }
  508. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  509. if(!hmd)
  510. {
  511. return true;
  512. }
  513. return hmd->isSimulated();
  514. }
  515. DefineEngineFunction(getOVRHMDProductName, const char*, (S32 index),,
  516. "@brief Retrieves the HMD product name.\n\n"
  517. "@param index The HMD index.\n"
  518. "@return The name of the HMD product.\n"
  519. "@ingroup Game")
  520. {
  521. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  522. {
  523. return "";
  524. }
  525. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  526. if(!hmd)
  527. {
  528. return "";
  529. }
  530. return hmd->getProductName();
  531. }
  532. DefineEngineFunction(getOVRHMDManufacturer, const char*, (S32 index),,
  533. "@brief Retrieves the HMD manufacturer name.\n\n"
  534. "@param index The HMD index.\n"
  535. "@return The manufacturer of the HMD product.\n"
  536. "@ingroup Game")
  537. {
  538. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  539. {
  540. return "";
  541. }
  542. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  543. if(!hmd)
  544. {
  545. return "";
  546. }
  547. return hmd->getManufacturer();
  548. }
  549. DefineEngineFunction(getOVRHMDVersion, S32, (S32 index),,
  550. "@brief Retrieves the HMD version number.\n\n"
  551. "@param index The HMD index.\n"
  552. "@return The version number of the HMD product.\n"
  553. "@ingroup Game")
  554. {
  555. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  556. {
  557. return -1;
  558. }
  559. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  560. if(!hmd)
  561. {
  562. return -1;
  563. }
  564. return hmd->getVersion();
  565. }
  566. DefineEngineFunction(getOVRHMDDisplayDeviceName, const char*, (S32 index),,
  567. "@brief Windows display device name used in EnumDisplaySettings/CreateDC.\n\n"
  568. "@param index The HMD index.\n"
  569. "@return The name of the HMD display device, if any.\n"
  570. "@ingroup Game")
  571. {
  572. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  573. {
  574. return "";
  575. }
  576. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  577. if(!hmd)
  578. {
  579. return "";
  580. }
  581. return hmd->getDisplayDeviceName();
  582. }
  583. DefineEngineFunction(getOVRHMDDisplayDeviceId, S32, (S32 index),,
  584. "@brief MacOS display ID.\n\n"
  585. "@param index The HMD index.\n"
  586. "@return The ID of the HMD display device, if any.\n"
  587. "@ingroup Game")
  588. {
  589. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  590. {
  591. return -1;
  592. }
  593. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  594. if(!hmd)
  595. {
  596. return -1;
  597. }
  598. return hmd->getDisplayDeviceId();
  599. }
  600. DefineEngineFunction(getOVRHMDDisplayDesktopPos, Point2I, (S32 index),,
  601. "@brief Desktop coordinate position of the screen (can be negative; may not be present on all platforms).\n\n"
  602. "@param index The HMD index.\n"
  603. "@return Position of the screen.\n"
  604. "@ingroup Game")
  605. {
  606. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  607. {
  608. return Point2I::Zero;
  609. }
  610. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  611. if(!hmd)
  612. {
  613. return Point2I::Zero;
  614. }
  615. return hmd->getDesktopPosition();
  616. }
  617. DefineEngineFunction(getOVRHMDResolution, Point2I, (S32 index),,
  618. "@brief Provides the OVR HMD screen resolution.\n\n"
  619. "@param index The HMD index.\n"
  620. "@return A two component string with the screen's resolution.\n"
  621. "@ingroup Game")
  622. {
  623. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  624. {
  625. return Point2I(1280, 800);
  626. }
  627. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  628. if(!hmd)
  629. {
  630. return Point2I(1280, 800);
  631. }
  632. return hmd->getResolution();
  633. }
  634. DefineEngineFunction(getOVRHMDDistortionCoefficients, String, (S32 index),,
  635. "@brief Provides the OVR HMD distortion coefficients.\n\n"
  636. "@param index The HMD index.\n"
  637. "@return A four component string with the distortion coefficients.\n"
  638. "@ingroup Game")
  639. {
  640. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  641. {
  642. return "0 0 0 0";
  643. }
  644. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  645. if(!hmd)
  646. {
  647. return "0 0 0 0";
  648. }
  649. const Point4F& k = hmd->getKDistortion();
  650. char buf[256];
  651. dSprintf(buf, 256, "%g %g %g %g", k.x, k.y, k.z, k.w);
  652. return buf;
  653. }
  654. DefineEngineFunction(getOVRHMDChromaticAbCorrection, String, (S32 index),,
  655. "@brief Provides the OVR HMD chromatic aberration correction values.\n\n"
  656. "@param index The HMD index.\n"
  657. "@return A four component string with the chromatic aberration correction values.\n"
  658. "@ingroup Game")
  659. {
  660. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  661. {
  662. return "1 0 1 0";
  663. }
  664. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  665. if(!hmd)
  666. {
  667. return "1 0 1 0";
  668. }
  669. const Point4F& c = hmd->getChromaticAbCorrection();
  670. char buf[256];
  671. dSprintf(buf, 256, "%g %g %g %g", c.x, c.y, c.z, c.w);
  672. return buf;
  673. }
  674. DefineEngineFunction(getOVRHMDProfileIPD, F32, (S32 index),,
  675. "@brief Physical distance between the user's eye centers as defined by the current profile.\n\n"
  676. "@param index The HMD index.\n"
  677. "@return The profile IPD.\n"
  678. "@ingroup Game")
  679. {
  680. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  681. {
  682. return -1.0f;
  683. }
  684. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  685. if(!hmd)
  686. {
  687. return -1.0f;
  688. }
  689. return hmd->getProfileIPD();
  690. }
  691. DefineEngineFunction(getOVRHMDCurrentIPD, F32, (S32 index),,
  692. "@brief Physical distance between the user's eye centers.\n\n"
  693. "@param index The HMD index.\n"
  694. "@return The current IPD.\n"
  695. "@ingroup Game")
  696. {
  697. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  698. {
  699. return -1.0f;
  700. }
  701. return OCULUSVRDEV->getHMDCurrentIPD(index);
  702. }
  703. DefineEngineFunction(setOVRHMDCurrentIPD, void, (S32 index, F32 ipd),,
  704. "@brief Set the physical distance between the user's eye centers.\n\n"
  705. "@param index The HMD index.\n"
  706. "@param ipd The IPD to use.\n"
  707. "@ingroup Game")
  708. {
  709. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  710. {
  711. return;
  712. }
  713. OCULUSVRDEV->setHMDCurrentIPD(index, ipd);
  714. }
  715. DefineEngineFunction(getOVRHMDEyeXOffsets, Point2F, (S32 index),,
  716. "@brief Provides the OVR HMD eye x offsets in uv coordinates.\n\n"
  717. "@param index The HMD index.\n"
  718. "@return A two component string with the left and right eye x offsets.\n"
  719. "@ingroup Game")
  720. {
  721. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  722. {
  723. return Point2F(0.5, 0.5);
  724. }
  725. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  726. if(!hmd)
  727. {
  728. return Point2F(0.5, 0.5);
  729. }
  730. // X component is left, Y component is right
  731. const Point2F& offset = hmd->getEyeUVOffset();
  732. return offset;
  733. }
  734. DefineEngineFunction(getOVRHMDXCenterOffset, F32, (S32 index),,
  735. "@brief Provides the OVR HMD calculated XCenterOffset.\n\n"
  736. "@param index The HMD index.\n"
  737. "@return The calculated XCenterOffset.\n"
  738. "@ingroup Game")
  739. {
  740. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  741. {
  742. return 0.0f;
  743. }
  744. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  745. if(!hmd)
  746. {
  747. return 0.0f;
  748. }
  749. F32 offset = hmd->getCenterOffset();
  750. return offset;
  751. }
  752. DefineEngineFunction(getOVRHMDDistortionScale, F32, (S32 index),,
  753. "@brief Provides the OVR HMD calculated distortion scale.\n\n"
  754. "@param index The HMD index.\n"
  755. "@return The calculated distortion scale.\n"
  756. "@ingroup Game")
  757. {
  758. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  759. {
  760. return 1.0f;
  761. }
  762. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  763. if(!hmd)
  764. {
  765. return 1.0f;
  766. }
  767. F32 scale = hmd->getDistortionScale();
  768. return scale;
  769. }
  770. DefineEngineFunction(getOVRHMDYFOV, F32, (S32 index),,
  771. "@brief Provides the OVR HMD calculated Y FOV.\n\n"
  772. "@param index The HMD index.\n"
  773. "@return The calculated Y FOV.\n"
  774. "@ingroup Game")
  775. {
  776. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  777. {
  778. return 1.0f;
  779. }
  780. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  781. if(!hmd)
  782. {
  783. return 1.0f;
  784. }
  785. F32 fov = hmd->getYFOV();
  786. return mRadToDeg(fov);
  787. }
  788. //-----------------------------------------------------------------------------
  789. DefineEngineFunction(getOVRSensorCount, S32, (),,
  790. "@brief Get the number of sensor devices that are currently connected.\n\n"
  791. "@return The number of Oculus VR sensor devices that are currently connected.\n"
  792. "@ingroup Game")
  793. {
  794. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  795. {
  796. return 0;
  797. }
  798. return OCULUSVRDEV->getSensorCount();
  799. }
  800. DefineEngineFunction(getOVRSensorEulerRotation, Point3F, (S32 index),,
  801. "@brief Get the Euler rotation values for the given sensor index.\n\n"
  802. "@param index The sensor index.\n"
  803. "@return The Euler rotation values of the Oculus VR sensor, in degrees.\n"
  804. "@ingroup Game")
  805. {
  806. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  807. {
  808. return Point3F::Zero;
  809. }
  810. EulerF rot = OCULUSVRDEV->getSensorEulerRotation(index);
  811. return Point3F(mRadToDeg(rot.x), mRadToDeg(rot.y), mRadToDeg(rot.z));
  812. }
  813. DefineEngineFunction(getOVRSensorAcceleration, Point3F, (S32 index),,
  814. "@brief Get the acceleration values for the given sensor index.\n\n"
  815. "@param index The sensor index.\n"
  816. "@return The acceleration values of the Oculus VR sensor, in m/s^2.\n"
  817. "@ingroup Game")
  818. {
  819. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  820. {
  821. return Point3F::Zero;
  822. }
  823. return OCULUSVRDEV->getSensorAcceleration(index);
  824. }
  825. DefineEngineFunction(getOVRSensorAngVelocity, Point3F, (S32 index),,
  826. "@brief Get the angular velocity values for the given sensor index.\n\n"
  827. "@param index The sensor index.\n"
  828. "@return The angular velocity values of the Oculus VR sensor, in degrees/s.\n"
  829. "@ingroup Game")
  830. {
  831. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  832. {
  833. return Point3F::Zero;
  834. }
  835. EulerF rot = OCULUSVRDEV->getSensorAngularVelocity(index);
  836. return Point3F(mRadToDeg(rot.x), mRadToDeg(rot.y), mRadToDeg(rot.z));
  837. }
  838. DefineEngineFunction(getOVRSensorMagnetometer, Point3F, (S32 index),,
  839. "@brief Get the magnetometer reading (direction and field strength) for the given sensor index.\n\n"
  840. "@param index The sensor index.\n"
  841. "@return The magnetometer reading (direction and field strength) of the Oculus VR sensor, in Gauss.\n"
  842. "@ingroup Game")
  843. {
  844. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  845. {
  846. return Point3F::Zero;
  847. }
  848. return OCULUSVRDEV->getSensorMagnetometer(index);
  849. }
  850. DefineEngineFunction(getOVRSensorPredictionTime, F32, (S32 index),,
  851. "@brief Get the prediction time set for the given sensor index.\n\n"
  852. "@param index The sensor index.\n"
  853. "@return The prediction time of the Oculus VR sensor, given in seconds.\n"
  854. "@ingroup Game")
  855. {
  856. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  857. {
  858. return 0;
  859. }
  860. return OCULUSVRDEV->getSensorPredictionTime(index);
  861. }
  862. DefineEngineFunction(setSensorPredictionTime, void, (S32 index, F32 dt),,
  863. "@brief Set the prediction time set for the given sensor index.\n\n"
  864. "@param index The sensor index.\n"
  865. "@param dt The prediction time to set given in seconds. Setting to 0 disables prediction.\n"
  866. "@ingroup Game")
  867. {
  868. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  869. {
  870. return;
  871. }
  872. OCULUSVRDEV->setSensorPredictionTime(index, dt);
  873. }
  874. DefineEngineFunction(setAllSensorPredictionTime, void, (F32 dt),,
  875. "@brief Set the prediction time set for all sensors.\n\n"
  876. "@param dt The prediction time to set given in seconds. Setting to 0 disables prediction.\n"
  877. "@ingroup Game")
  878. {
  879. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  880. {
  881. return;
  882. }
  883. OCULUSVRDEV->setAllSensorPredictionTime(dt);
  884. }
  885. DefineEngineFunction(getOVRSensorGravityCorrection, bool, (S32 index),,
  886. "@brief Get the gravity correction state for the given sensor index.\n\n"
  887. "@param index The sensor index.\n"
  888. "@return True if gravity correction (for pitch and roll) is active.\n"
  889. "@ingroup Game")
  890. {
  891. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  892. {
  893. return false;
  894. }
  895. return OCULUSVRDEV->getSensorGravityCorrection(index);
  896. }
  897. DefineEngineFunction(setOVRSensorGravityCorrection, void, (S32 index, bool state),,
  898. "@brief Set the gravity correction state for the given sensor index.\n\n"
  899. "@param index The sensor index.\n"
  900. "@param state The gravity correction state to change to.\n"
  901. "@ingroup Game")
  902. {
  903. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  904. {
  905. return;
  906. }
  907. OCULUSVRDEV->setSensorGravityCorrection(index, state);
  908. }
  909. DefineEngineFunction(getOVRSensorYawCorrection, bool, (S32 index),,
  910. "@brief Get the yaw correction state for the given sensor index.\n\n"
  911. "@param index The sensor index.\n"
  912. "@return True if yaw correction (using magnetometer calibration data) is active.\n"
  913. "@ingroup Game")
  914. {
  915. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  916. {
  917. return false;
  918. }
  919. return OCULUSVRDEV->getSensorYawCorrection(index);
  920. }
  921. DefineEngineFunction(setOVRSensorYawCorrection, void, (S32 index, bool state),,
  922. "@brief Set the yaw correction state for the given sensor index.\n\n"
  923. "@param index The sensor index.\n"
  924. "@param state The yaw correction state to change to.\n"
  925. "@note Yaw correction cannot be enabled if the user has disabled it through "
  926. "the Oculus VR control panel.\n"
  927. "@ingroup Game")
  928. {
  929. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  930. {
  931. return;
  932. }
  933. OCULUSVRDEV->setSensorYawCorrection(index, state);
  934. }
  935. DefineEngineFunction(getOVRSensorMagnetometerCalibrated, bool, (S32 index),,
  936. "@brief Get the magnetometer calibrated data state for the given sensor index.\n\n"
  937. "@param index The sensor index.\n"
  938. "@return True if magnetometer calibration data is available.\n"
  939. "@ingroup Game")
  940. {
  941. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  942. {
  943. return false;
  944. }
  945. return OCULUSVRDEV->getSensorMagnetometerCalibrated(index);
  946. }
  947. DefineEngineFunction(ovrResetAllSensors, void, (),,
  948. "@brief Resets all Oculus VR sensors.\n\n"
  949. "This resets all sensor orientations such that their 'normal' rotation "
  950. "is defined when this function is called. This defines an HMD's forwards "
  951. "and up direction, for example."
  952. "@ingroup Game")
  953. {
  954. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  955. {
  956. return;
  957. }
  958. OCULUSVRDEV->resetAllSensors();
  959. }