oculusVRDevice.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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/input/oculusVR/oculusVRSensorDevice.h"
  24. #include "platform/platformInput.h"
  25. #include "core/module.h"
  26. #include "console/engineAPI.h"
  27. #include "T3D/gameBase/gameConnection.h"
  28. #include "gui/core/guiCanvas.h"
  29. bool sOcculusEnabled = false;
  30. MODULE_BEGIN( OculusVRDevice )
  31. MODULE_INIT_AFTER( InputEventManager )
  32. MODULE_SHUTDOWN_BEFORE( InputEventManager )
  33. MODULE_INIT
  34. {
  35. sOcculusEnabled = true; // TODO: init the dll
  36. OculusVRDevice::staticInit();
  37. ManagedSingleton< OculusVRDevice >::createSingleton();
  38. if(OculusVRDevice::smEnableDevice)
  39. {
  40. OCULUSVRDEV->enable();
  41. }
  42. // Register the device with the Input Event Manager
  43. INPUTMGR->registerDevice(OCULUSVRDEV);
  44. }
  45. MODULE_SHUTDOWN
  46. {
  47. INPUTMGR->unregisterDevice(OCULUSVRDEV);
  48. ManagedSingleton< OculusVRDevice >::deleteSingleton();
  49. sOcculusEnabled = false;
  50. }
  51. MODULE_END;
  52. //-----------------------------------------------------------------------------
  53. // OculusVRDevice
  54. //-----------------------------------------------------------------------------
  55. bool OculusVRDevice::smEnableDevice = true;
  56. bool OculusVRDevice::smSimulateHMD = true;
  57. bool OculusVRDevice::smGenerateAngleAxisRotationEvents = true;
  58. bool OculusVRDevice::smGenerateEulerRotationEvents = false;
  59. bool OculusVRDevice::smGeneratePositionEvents = true;
  60. bool OculusVRDevice::smGenerateRotationAsAxisEvents = false;
  61. F32 OculusVRDevice::smMaximumAxisAngle = 25.0f;
  62. bool OculusVRDevice::smGenerateSensorRawEvents = false;
  63. bool OculusVRDevice::smGenerateWholeFrameEvents = false;
  64. F32 OculusVRDevice::smDesiredPixelDensity = 1.0f;
  65. bool OculusVRDevice::smWindowDebug = false;
  66. F32 OculusVRDevice::smPositionTrackingScale = 1.0f;
  67. OculusVRDevice::OculusVRDevice()
  68. {
  69. // From IInputDevice
  70. dStrcpy(mName, "oculusvr");
  71. mDeviceType = INPUTMGR->getNextDeviceType();
  72. //
  73. mEnabled = false;
  74. mActive = false;
  75. mActiveDeviceId = 0;
  76. buildCodeTable();
  77. GFXDevice::getDeviceEventSignal().notify( this, &OculusVRDevice::_handleDeviceEvent );
  78. }
  79. OculusVRDevice::~OculusVRDevice()
  80. {
  81. GFXDevice::getDeviceEventSignal().remove( this, &OculusVRDevice::_handleDeviceEvent );
  82. cleanUp();
  83. }
  84. void OculusVRDevice::staticInit()
  85. {
  86. Con::addVariable("pref::OculusVR::EnableDevice", TypeBool, &smEnableDevice,
  87. "@brief If true, the Oculus VR device will be enabled, if present.\n\n"
  88. "@ingroup Game");
  89. Con::addVariable("OculusVR::GenerateAngleAxisRotationEvents", TypeBool, &smGenerateAngleAxisRotationEvents,
  90. "@brief If true, broadcast sensor rotation events as angled axis.\n\n"
  91. "@ingroup Game");
  92. Con::addVariable("OculusVR::GenerateEulerRotationEvents", TypeBool, &smGenerateEulerRotationEvents,
  93. "@brief If true, broadcast sensor rotation events as Euler angles about the X, Y and Z axis.\n\n"
  94. "@ingroup Game");
  95. Con::addVariable("OculusVR::GeneratePositionEvents", TypeBool, &smGeneratePositionEvents,
  96. "@brief If true, broadcast sensor rotation events as Euler angles about the X, Y and Z axis.\n\n"
  97. "@ingroup Game");
  98. Con::addVariable("OculusVR::GenerateRotationAsAxisEvents", TypeBool, &smGenerateRotationAsAxisEvents,
  99. "@brief If true, broadcast sensor rotation as axis events.\n\n"
  100. "@ingroup Game");
  101. Con::addVariable("OculusVR::MaximumAxisAngle", TypeF32, &smMaximumAxisAngle,
  102. "@brief The maximum sensor angle when used as an axis event as measured from a vector pointing straight up (in degrees).\n\n"
  103. "Should range from 0 to 90 degrees.\n\n"
  104. "@ingroup Game");
  105. Con::addVariable("OculusVR::GenerateSensorRawEvents", TypeBool, &smGenerateSensorRawEvents,
  106. "@brief If ture, broadcast sensor raw data: acceleration, angular velocity, magnetometer reading.\n\n"
  107. "@ingroup Game");
  108. Con::addVariable("OculusVR::GenerateWholeFrameEvents", TypeBool, &smGenerateWholeFrameEvents,
  109. "@brief Indicates that a whole frame event should be generated and frames should be buffered.\n\n"
  110. "@ingroup Game");
  111. Con::addVariable("OculusVR::desiredPixelDensity", TypeF32, &smDesiredPixelDensity,
  112. "@brief Specifies the desired pixel density of the render target. \n\n"
  113. "@ingroup Game");
  114. Con::addVariable("OculusVR::windowDebug", TypeBool, &smWindowDebug,
  115. "@brief Specifies if the window should stay on the main display for debugging. \n\n"
  116. "@ingroup Game");
  117. }
  118. void OculusVRDevice::cleanUp()
  119. {
  120. disable();
  121. }
  122. void OculusVRDevice::buildCodeTable()
  123. {
  124. // Build the sensor device code table
  125. OculusVRSensorDevice::buildCodeTable();
  126. }
  127. void OculusVRDevice::addHMDDevice(ovrHmd hmd)
  128. {
  129. if(!hmd)
  130. return;
  131. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  132. hmdd->set(hmd,mHMDDevices.size());
  133. mHMDDevices.push_back(hmdd);
  134. Con::printf(" HMD found: %s by %s [v%d]", hmd->ProductName, hmd->Manufacturer, hmd->Type);
  135. }
  136. void OculusVRDevice::createSimulatedHMD()
  137. {
  138. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  139. ovrHmd hmd = ovrHmd_CreateDebug(ovrHmd_DK2);
  140. hmdd->set(hmd,mHMDDevices.size());
  141. mHMDDevices.push_back(hmdd);
  142. Con::printf(" HMD simulated: %s by %s [v%d]", hmdd->getProductName(), hmdd->getManufacturer(), hmdd->getVersion());
  143. }
  144. bool OculusVRDevice::enable()
  145. {
  146. // Start off with disabling the device if it is already enabled
  147. disable();
  148. Con::printf("Oculus VR Device Init:");
  149. if(sOcculusEnabled && ovr_Initialize())
  150. {
  151. mEnabled = true;
  152. // Enumerate HMDs and pick the first one
  153. ovrHmd hmd = ovrHmd_Create(0);
  154. if(hmd)
  155. {
  156. // Add the HMD to our list
  157. addHMDDevice(hmd);
  158. setActive(true);
  159. }
  160. else
  161. {
  162. if(smSimulateHMD)
  163. {
  164. Con::printf(" Could not enumerate a HMD device. Simulating a HMD.");
  165. createSimulatedHMD();
  166. setActive(true);
  167. }
  168. else
  169. {
  170. Con::printf(" Could not enumerate a HMD device.");
  171. }
  172. }
  173. }
  174. else
  175. {
  176. if(smSimulateHMD)
  177. {
  178. Con::printf(" Could not enumerate a HMD device. Simulating a HMD.");
  179. createSimulatedHMD();
  180. setActive(true);
  181. }
  182. else
  183. {
  184. Con::printf(" Could not enumerate a HMD device.");
  185. }
  186. }
  187. Con::printf(" ");
  188. return false;
  189. }
  190. void OculusVRDevice::disable()
  191. {
  192. for(U32 i=0; i<mHMDDevices.size(); ++i)
  193. {
  194. delete mHMDDevices[i];
  195. }
  196. mHMDDevices.clear();
  197. if(mEnabled)
  198. {
  199. ovr_Shutdown();
  200. }
  201. setActive(false);
  202. mEnabled = false;
  203. }
  204. bool OculusVRDevice::process()
  205. {
  206. if(!mEnabled)
  207. return false;
  208. if(!getActive())
  209. return false;
  210. //Build the maximum axis angle to be passed into the sensor process()
  211. F32 maxAxisRadius = mSin(mDegToRad(smMaximumAxisAngle));
  212. // Process each sensor
  213. for(U32 i=0; i<mHMDDevices.size(); ++i)
  214. {
  215. mHMDDevices[i]->getSensorDevice()->process(mDeviceType, smGenerateAngleAxisRotationEvents, smGenerateEulerRotationEvents, smGenerateRotationAsAxisEvents, smGeneratePositionEvents, maxAxisRadius, smGenerateSensorRawEvents);
  216. }
  217. return true;
  218. }
  219. //-----------------------------------------------------------------------------
  220. bool OculusVRDevice::providesFrameEyePose() const
  221. {
  222. if(!mHMDDevices.size())
  223. return false;
  224. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  225. if(!hmd)
  226. return false;
  227. return true;
  228. }
  229. void OculusVRDevice::getFrameEyePose(DisplayPose *outPose, U32 eyeId) const
  230. {
  231. if(!mHMDDevices.size())
  232. return;
  233. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  234. if(!hmd)
  235. return;
  236. hmd->getFrameEyePose(outPose, eyeId);
  237. }
  238. bool OculusVRDevice::providesEyeOffsets() const
  239. {
  240. if(!mHMDDevices.size())
  241. return false;
  242. return true;
  243. }
  244. void OculusVRDevice::getEyeOffsets(Point3F *dest) const
  245. {
  246. if(!mHMDDevices.size())
  247. return;
  248. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  249. if(!hmd)
  250. return;
  251. hmd->getEyeOffsets(dest);
  252. }
  253. bool OculusVRDevice::providesFovPorts() const
  254. {
  255. if(!mHMDDevices.size())
  256. return false;
  257. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  258. if(!hmd)
  259. return Point3F::Zero;
  260. return true;
  261. }
  262. void OculusVRDevice::getFovPorts(FovPort *out) const
  263. {
  264. if(!mHMDDevices.size())
  265. return;
  266. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  267. if(!hmd)
  268. return;
  269. return hmd->getFovPorts(out);
  270. }
  271. bool OculusVRDevice::providesProjectionOffset() const
  272. {
  273. if(!mHMDDevices.size())
  274. return false;
  275. return false;
  276. }
  277. const Point2F& OculusVRDevice::getProjectionOffset() const
  278. {
  279. if(!mHMDDevices.size())
  280. return Point2F::Zero;
  281. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  282. if(!hmd)
  283. return Point2F::Zero;
  284. return hmd->getProjectionCenterOffset();
  285. }
  286. void OculusVRDevice::getStereoViewports(RectI *out) const
  287. {
  288. if(!mHMDDevices.size())
  289. return;
  290. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  291. if(!hmd)
  292. return;
  293. hmd->getStereoViewports(out);
  294. }
  295. void OculusVRDevice::getStereoTargets(GFXTextureTarget **out) const
  296. {
  297. if(!mHMDDevices.size())
  298. return;
  299. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  300. if(!hmd)
  301. return;
  302. hmd->getStereoTargets(out);
  303. }
  304. void OculusVRDevice::onStartFrame()
  305. {
  306. _handleDeviceEvent(GFXDevice::deStartOfFrame);
  307. }
  308. //-----------------------------------------------------------------------------
  309. OculusVRHMDDevice* OculusVRDevice::getHMDDevice(U32 index) const
  310. {
  311. if(index >= mHMDDevices.size())
  312. return NULL;
  313. return mHMDDevices[index];
  314. }
  315. F32 OculusVRDevice::getHMDCurrentIPD(U32 index)
  316. {
  317. if(index >= mHMDDevices.size())
  318. return -1.0f;
  319. return mHMDDevices[index]->getIPD();
  320. }
  321. void OculusVRDevice::setHMDCurrentIPD(U32 index, F32 ipd)
  322. {
  323. if(index >= mHMDDevices.size())
  324. return;
  325. return mHMDDevices[index]->setIPD(ipd);
  326. }
  327. void OculusVRDevice::setOptimalDisplaySize(U32 index, GuiCanvas *canvas)
  328. {
  329. if(index >= mHMDDevices.size())
  330. return;
  331. mHMDDevices[index]->setOptimalDisplaySize(canvas);
  332. }
  333. //-----------------------------------------------------------------------------
  334. const OculusVRSensorDevice* OculusVRDevice::getSensorDevice(U32 index) const
  335. {
  336. if(index >= mHMDDevices.size())
  337. return NULL;
  338. return mHMDDevices[index]->getSensorDevice();
  339. }
  340. EulerF OculusVRDevice::getSensorEulerRotation(U32 index)
  341. {
  342. if(index >= mHMDDevices.size())
  343. return Point3F::Zero;
  344. return mHMDDevices[index]->getSensorDevice()->getEulerRotation();
  345. }
  346. VectorF OculusVRDevice::getSensorAcceleration(U32 index)
  347. {
  348. if(index >= mHMDDevices.size())
  349. return Point3F::Zero;
  350. return mHMDDevices[index]->getSensorDevice()->getAcceleration();
  351. }
  352. EulerF OculusVRDevice::getSensorAngularVelocity(U32 index)
  353. {
  354. if(index >= mHMDDevices.size())
  355. return Point3F::Zero;
  356. return mHMDDevices[index]->getSensorDevice()->getAngularVelocity();
  357. }
  358. bool OculusVRDevice::getSensorYawCorrection(U32 index)
  359. {
  360. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  361. if(!sensor || !sensor->isValid())
  362. return false;
  363. return sensor->getYawCorrection();
  364. }
  365. void OculusVRDevice::setSensorYawCorrection(U32 index, bool state)
  366. {
  367. if(index >= mHMDDevices.size())
  368. return;
  369. OculusVRSensorDevice* sensor = mHMDDevices[index]->getSensorDevice();
  370. if(!sensor->isValid())
  371. return;
  372. sensor->setYawCorrection(state);
  373. }
  374. bool OculusVRDevice::getSensorMagnetometerCalibrated(U32 index)
  375. {
  376. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  377. if(!sensor || !sensor->isValid())
  378. return false;
  379. return sensor->getMagnetometerCalibrationAvailable();
  380. }
  381. void OculusVRDevice::resetAllSensors()
  382. {
  383. // Reset each sensor
  384. for(U32 i=0; i<mHMDDevices.size(); ++i)
  385. {
  386. mHMDDevices[i]->getSensorDevice()->reset();
  387. }
  388. }
  389. bool OculusVRDevice::isDiplayingWarning()
  390. {
  391. for(U32 i=0; i<mHMDDevices.size(); ++i)
  392. {
  393. if (mHMDDevices[i]->isDisplayingWarning())
  394. return true;
  395. }
  396. return false;
  397. }
  398. void OculusVRDevice::dismissWarning()
  399. {
  400. for(U32 i=0; i<mHMDDevices.size(); ++i)
  401. {
  402. mHMDDevices[i]->dismissWarning();
  403. }
  404. }
  405. String OculusVRDevice::dumpMetrics(U32 idx)
  406. {
  407. return mHMDDevices[idx]->dumpMetrics();
  408. }
  409. void OculusVRDevice::setDrawCanvas(GuiCanvas *canvas)
  410. {
  411. if(!mHMDDevices.size())
  412. return;
  413. OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  414. if(!hmd)
  415. return;
  416. hmd->setDrawCanvas(canvas);
  417. }
  418. void OculusVRDevice::setCurrentConnection(GameConnection *connection)
  419. {
  420. if(!mHMDDevices.size())
  421. return;
  422. OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  423. if(!hmd)
  424. return;
  425. hmd->setCurrentConnection(connection);
  426. }
  427. GameConnection* OculusVRDevice::getCurrentConnection()
  428. {
  429. if(!mHMDDevices.size())
  430. return NULL;
  431. OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  432. if(!hmd)
  433. return NULL;
  434. return hmd->getCurrentConnection();
  435. }
  436. //-----------------------------------------------------------------------------
  437. DefineEngineFunction(isOculusVRDeviceActive, bool, (),,
  438. "@brief Used to determine if the Oculus VR input device is active\n\n"
  439. "The Oculus VR device is considered active when the library has been "
  440. "initialized and either a real of simulated HMD is present.\n\n"
  441. "@return True if the Oculus VR input device is active.\n"
  442. "@ingroup Game")
  443. {
  444. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  445. {
  446. return false;
  447. }
  448. return OCULUSVRDEV->getActive();
  449. }
  450. //-----------------------------------------------------------------------------
  451. DefineEngineFunction(setOVRHMDAsGameConnectionDisplayDevice, bool, (GameConnection* conn),,
  452. "@brief Sets the first HMD to be a GameConnection's display device\n\n"
  453. "@param conn The GameConnection to set.\n"
  454. "@return True if the GameConnection display device was set.\n"
  455. "@ingroup Game")
  456. {
  457. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  458. {
  459. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): No Oculus VR Device present.");
  460. return false;
  461. }
  462. if(!conn)
  463. {
  464. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): Invalid GameConnection.");
  465. return false;
  466. }
  467. conn->setDisplayDevice(OCULUSVRDEV);
  468. return true;
  469. }
  470. //-----------------------------------------------------------------------------
  471. DefineEngineFunction(setOptimalOVRCanvasSize, bool, (GuiCanvas* canvas),,
  472. "@brief Sets the first HMD to be a GameConnection's display device\n\n"
  473. "@param conn The GameConnection to set.\n"
  474. "@return True if the GameConnection display device was set.\n"
  475. "@ingroup Game")
  476. {
  477. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  478. {
  479. Con::errorf("setOptimalOVRCanvasSize(): No Oculus VR Device present.");
  480. return false;
  481. }
  482. if(!canvas)
  483. {
  484. Con::errorf("setOptimalOVRCanvasSize(): Invalid Canvas.");
  485. return false;
  486. }
  487. OCULUSVRDEV->setOptimalDisplaySize(0, canvas);
  488. return true;
  489. }
  490. //-----------------------------------------------------------------------------
  491. DefineEngineFunction(getOVRHMDCount, S32, (),,
  492. "@brief Get the number of HMD devices that are currently connected.\n\n"
  493. "@return The number of Oculus VR HMD devices that are currently connected.\n"
  494. "@ingroup Game")
  495. {
  496. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  497. {
  498. return 0;
  499. }
  500. return OCULUSVRDEV->getHMDCount();
  501. }
  502. DefineEngineFunction(getOVRHMDProductName, const char*, (S32 index),,
  503. "@brief Retrieves the HMD product name.\n\n"
  504. "@param index The HMD index.\n"
  505. "@return The name of the HMD product.\n"
  506. "@ingroup Game")
  507. {
  508. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  509. {
  510. return "";
  511. }
  512. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  513. if(!hmd)
  514. {
  515. return "";
  516. }
  517. return hmd->getProductName();
  518. }
  519. DefineEngineFunction(getOVRHMDManufacturer, const char*, (S32 index),,
  520. "@brief Retrieves the HMD manufacturer name.\n\n"
  521. "@param index The HMD index.\n"
  522. "@return The manufacturer of the HMD product.\n"
  523. "@ingroup Game")
  524. {
  525. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  526. {
  527. return "";
  528. }
  529. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  530. if(!hmd)
  531. {
  532. return "";
  533. }
  534. return hmd->getManufacturer();
  535. }
  536. DefineEngineFunction(getOVRHMDVersion, S32, (S32 index),,
  537. "@brief Retrieves the HMD version number.\n\n"
  538. "@param index The HMD index.\n"
  539. "@return The version number of the HMD product.\n"
  540. "@ingroup Game")
  541. {
  542. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  543. {
  544. return -1;
  545. }
  546. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  547. if(!hmd)
  548. {
  549. return -1;
  550. }
  551. return hmd->getVersion();
  552. }
  553. DefineEngineFunction(getOVRHMDDisplayDeviceName, const char*, (S32 index),,
  554. "@brief Windows display device name used in EnumDisplaySettings/CreateDC.\n\n"
  555. "@param index The HMD index.\n"
  556. "@return The name of the HMD display device, if any.\n"
  557. "@ingroup Game")
  558. {
  559. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  560. {
  561. return "";
  562. }
  563. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  564. if(!hmd)
  565. {
  566. return "";
  567. }
  568. return hmd->getDisplayDeviceName();
  569. }
  570. DefineEngineFunction(getOVRHMDDisplayDeviceId, S32, (S32 index),,
  571. "@brief MacOS display ID.\n\n"
  572. "@param index The HMD index.\n"
  573. "@return The ID of the HMD display device, if any.\n"
  574. "@ingroup Game")
  575. {
  576. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  577. {
  578. return -1;
  579. }
  580. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  581. if(!hmd)
  582. {
  583. return -1;
  584. }
  585. return hmd->getDisplayDeviceId();
  586. }
  587. DefineEngineFunction(getOVRHMDDisplayDesktopPos, Point2I, (S32 index),,
  588. "@brief Desktop coordinate position of the screen (can be negative; may not be present on all platforms).\n\n"
  589. "@param index The HMD index.\n"
  590. "@return Position of the screen.\n"
  591. "@ingroup Game")
  592. {
  593. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  594. {
  595. return Point2I::Zero;
  596. }
  597. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  598. if(!hmd)
  599. {
  600. return Point2I::Zero;
  601. }
  602. return hmd->getDesktopPosition();
  603. }
  604. DefineEngineFunction(getOVRHMDResolution, Point2I, (S32 index),,
  605. "@brief Provides the OVR HMD screen resolution.\n\n"
  606. "@param index The HMD index.\n"
  607. "@return A two component string with the screen's resolution.\n"
  608. "@ingroup Game")
  609. {
  610. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  611. {
  612. return Point2I(1280, 800);
  613. }
  614. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  615. if(!hmd)
  616. {
  617. return Point2I(1280, 800);
  618. }
  619. return hmd->getResolution();
  620. }
  621. DefineEngineFunction(getOVRHMDProfileIPD, F32, (S32 index),,
  622. "@brief Physical distance between the user's eye centers as defined by the current profile.\n\n"
  623. "@param index The HMD index.\n"
  624. "@return The profile IPD.\n"
  625. "@ingroup Game")
  626. {
  627. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  628. {
  629. return -1.0f;
  630. }
  631. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  632. if(!hmd)
  633. {
  634. return -1.0f;
  635. }
  636. return hmd->getProfileIPD();
  637. }
  638. DefineEngineFunction(getOVRHMDCurrentIPD, F32, (S32 index),,
  639. "@brief Physical distance between the user's eye centers.\n\n"
  640. "@param index The HMD index.\n"
  641. "@return The current IPD.\n"
  642. "@ingroup Game")
  643. {
  644. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  645. {
  646. return -1.0f;
  647. }
  648. return OCULUSVRDEV->getHMDCurrentIPD(index);
  649. }
  650. DefineEngineFunction(setOVRHMDCurrentIPD, void, (S32 index, F32 ipd),,
  651. "@brief Set the physical distance between the user's eye centers.\n\n"
  652. "@param index The HMD index.\n"
  653. "@param ipd The IPD to use.\n"
  654. "@ingroup Game")
  655. {
  656. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  657. {
  658. return;
  659. }
  660. OCULUSVRDEV->setHMDCurrentIPD(index, ipd);
  661. }
  662. //-----------------------------------------------------------------------------
  663. DefineEngineFunction(getOVRSensorCount, S32, (),,
  664. "@brief Get the number of sensor devices that are currently connected.\n\n"
  665. "@return The number of Oculus VR sensor devices that are currently connected.\n"
  666. "@ingroup Game")
  667. {
  668. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  669. {
  670. return 0;
  671. }
  672. return OCULUSVRDEV->getSensorCount();
  673. }
  674. DefineEngineFunction(getOVRSensorEulerRotation, Point3F, (S32 index),,
  675. "@brief Get the Euler rotation values for the given sensor index.\n\n"
  676. "@param index The sensor index.\n"
  677. "@return The Euler rotation values of the Oculus VR sensor, in degrees.\n"
  678. "@ingroup Game")
  679. {
  680. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  681. {
  682. return Point3F::Zero;
  683. }
  684. EulerF rot = OCULUSVRDEV->getSensorEulerRotation(index);
  685. return Point3F(mRadToDeg(rot.x), mRadToDeg(rot.y), mRadToDeg(rot.z));
  686. }
  687. DefineEngineFunction(getOVRSensorAcceleration, Point3F, (S32 index),,
  688. "@brief Get the acceleration values for the given sensor index.\n\n"
  689. "@param index The sensor index.\n"
  690. "@return The acceleration values of the Oculus VR sensor, in m/s^2.\n"
  691. "@ingroup Game")
  692. {
  693. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  694. {
  695. return Point3F::Zero;
  696. }
  697. return OCULUSVRDEV->getSensorAcceleration(index);
  698. }
  699. DefineEngineFunction(getOVRSensorAngVelocity, Point3F, (S32 index),,
  700. "@brief Get the angular velocity values for the given sensor index.\n\n"
  701. "@param index The sensor index.\n"
  702. "@return The angular velocity values of the Oculus VR sensor, in degrees/s.\n"
  703. "@ingroup Game")
  704. {
  705. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  706. {
  707. return Point3F::Zero;
  708. }
  709. EulerF rot = OCULUSVRDEV->getSensorAngularVelocity(index);
  710. return Point3F(mRadToDeg(rot.x), mRadToDeg(rot.y), mRadToDeg(rot.z));
  711. }
  712. DefineEngineFunction(getOVRSensorYawCorrection, bool, (S32 index),,
  713. "@brief Get the yaw correction state for the given sensor index.\n\n"
  714. "@param index The sensor index.\n"
  715. "@return True if yaw correction (using magnetometer calibration data) is active.\n"
  716. "@ingroup Game")
  717. {
  718. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  719. {
  720. return false;
  721. }
  722. return OCULUSVRDEV->getSensorYawCorrection(index);
  723. }
  724. DefineEngineFunction(setOVRSensorYawCorrection, void, (S32 index, bool state),,
  725. "@brief Set the yaw correction state for the given sensor index.\n\n"
  726. "@param index The sensor index.\n"
  727. "@param state The yaw correction state to change to.\n"
  728. "@note Yaw correction cannot be enabled if the user has disabled it through "
  729. "the Oculus VR control panel.\n"
  730. "@ingroup Game")
  731. {
  732. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  733. {
  734. return;
  735. }
  736. OCULUSVRDEV->setSensorYawCorrection(index, state);
  737. }
  738. DefineEngineFunction(getOVRSensorMagnetometerCalibrated, bool, (S32 index),,
  739. "@brief Get the magnetometer calibrated data state for the given sensor index.\n\n"
  740. "@param index The sensor index.\n"
  741. "@return True if magnetometer calibration data is available.\n"
  742. "@ingroup Game")
  743. {
  744. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  745. {
  746. return false;
  747. }
  748. return OCULUSVRDEV->getSensorMagnetometerCalibrated(index);
  749. }
  750. DefineEngineFunction(ovrResetAllSensors, void, (),,
  751. "@brief Resets all Oculus VR sensors.\n\n"
  752. "This resets all sensor orientations such that their 'normal' rotation "
  753. "is defined when this function is called. This defines an HMD's forwards "
  754. "and up direction, for example."
  755. "@ingroup Game")
  756. {
  757. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  758. {
  759. return;
  760. }
  761. OCULUSVRDEV->resetAllSensors();
  762. }
  763. DefineEngineFunction(ovrIsDisplayingWarning, bool, (),,
  764. "@brief returns is warning is being displayed.\n\n"
  765. "@ingroup Game")
  766. {
  767. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  768. {
  769. return false;
  770. }
  771. return OCULUSVRDEV->isDiplayingWarning();
  772. }
  773. DefineEngineFunction(ovrDismissWarnings, void, (),,
  774. "@brief dismisses warnings.\n\n"
  775. "@ingroup Game")
  776. {
  777. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  778. {
  779. return;
  780. }
  781. OCULUSVRDEV->dismissWarning();
  782. }
  783. DefineEngineFunction(ovrDumpMetrics, String, (S32 idx),(0),
  784. "@brief dumps sensor metrics.\n\n"
  785. "@ingroup Game")
  786. {
  787. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  788. {
  789. return "";
  790. }
  791. return OCULUSVRDEV->dumpMetrics(idx);
  792. }
  793. bool OculusVRDevice::_handleDeviceEvent( GFXDevice::GFXDeviceEventType evt )
  794. {
  795. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  796. {
  797. return true;
  798. }
  799. switch( evt )
  800. {
  801. case GFXDevice::deStartOfFrame:
  802. for (U32 i=0; i<OCULUSVRDEV->mHMDDevices.size(); i++)
  803. {
  804. OCULUSVRDEV->mHMDDevices[i]->onStartFrame();
  805. }
  806. // Fall through
  807. break;
  808. case GFXDevice::dePostFrame:
  809. for (U32 i=0; i<OCULUSVRDEV->mHMDDevices.size(); i++)
  810. {
  811. OCULUSVRDEV->mHMDDevices[i]->onEndFrame();
  812. }
  813. break;
  814. case GFXDevice::deDestroy:
  815. for (U32 i=0; i<OCULUSVRDEV->mHMDDevices.size(); i++)
  816. {
  817. OCULUSVRDEV->mHMDDevices[i]->onDeviceDestroy();
  818. }
  819. default:
  820. break;
  821. }
  822. return true;
  823. }