oculusVRDevice.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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, ovrGraphicsLuid luid)
  128. {
  129. if(!hmd)
  130. return;
  131. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  132. hmdd->set(hmd, luid, mHMDDevices.size());
  133. mHMDDevices.push_back(hmdd);
  134. ovrHmdDesc desc = ovr_GetHmdDesc(hmd);
  135. Con::printf(" HMD found: %s by %s [v%d]", desc.ProductName, desc.Manufacturer, desc.Type);
  136. }
  137. void OculusVRDevice::createSimulatedHMD()
  138. {/* TOFIX
  139. OculusVRHMDDevice* hmdd = new OculusVRHMDDevice();
  140. ovrHmd hmd = ovr_CreateDebug(ovrHmd_DK2);
  141. hmdd->set(hmd,mHMDDevices.size());
  142. mHMDDevices.push_back(hmdd);
  143. Con::printf(" HMD simulated: %s by %s [v%d]", hmdd->getProductName(), hmdd->getManufacturer(), hmdd->getVersion()); */
  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. if(sOcculusEnabled && OVR_SUCCESS(ovr_Initialize(0)))
  151. {
  152. mEnabled = true;
  153. // Enumerate HMDs and pick the first one
  154. ovrHmd hmd;
  155. ovrGraphicsLuid luid;
  156. if(OVR_SUCCESS(ovr_Create(&hmd, &luid)))
  157. {
  158. // Add the HMD to our list
  159. addHMDDevice(hmd, luid);
  160. setActive(true);
  161. }
  162. else
  163. {
  164. if(smSimulateHMD)
  165. {
  166. Con::printf(" Could not enumerate a HMD device. Simulating a HMD.");
  167. createSimulatedHMD();
  168. setActive(true);
  169. }
  170. else
  171. {
  172. Con::printf(" Could not enumerate a HMD device.");
  173. }
  174. }
  175. }
  176. else
  177. {
  178. if(smSimulateHMD)
  179. {
  180. Con::printf(" Could not enumerate a HMD device. Simulating a HMD.");
  181. createSimulatedHMD();
  182. setActive(true);
  183. }
  184. else
  185. {
  186. Con::printf(" Could not enumerate a HMD device.");
  187. }
  188. }
  189. Con::printf(" ");
  190. return false;
  191. }
  192. void OculusVRDevice::disable()
  193. {
  194. for(U32 i=0; i<mHMDDevices.size(); ++i)
  195. {
  196. delete mHMDDevices[i];
  197. }
  198. mHMDDevices.clear();
  199. if(mEnabled)
  200. {
  201. ovr_Shutdown();
  202. }
  203. setActive(false);
  204. mEnabled = false;
  205. }
  206. bool OculusVRDevice::process()
  207. {
  208. if(!mEnabled)
  209. return false;
  210. if(!getActive())
  211. return false;
  212. //Build the maximum axis angle to be passed into the sensor process()
  213. F32 maxAxisRadius = mSin(mDegToRad(smMaximumAxisAngle));
  214. // Process each sensor
  215. for(U32 i=0; i<mHMDDevices.size(); ++i)
  216. {
  217. mHMDDevices[i]->getSensorDevice()->process(mDeviceType, smGenerateAngleAxisRotationEvents, smGenerateEulerRotationEvents, smGenerateRotationAsAxisEvents, smGeneratePositionEvents, maxAxisRadius, smGenerateSensorRawEvents);
  218. }
  219. return true;
  220. }
  221. //-----------------------------------------------------------------------------
  222. bool OculusVRDevice::providesFrameEyePose() const
  223. {
  224. if(!mHMDDevices.size())
  225. return false;
  226. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  227. if(!hmd)
  228. return false;
  229. return true;
  230. }
  231. void OculusVRDevice::getFrameEyePose(DisplayPose *outPose, U32 eyeId) const
  232. {
  233. if(!mHMDDevices.size())
  234. return;
  235. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  236. if(!hmd)
  237. return;
  238. hmd->getFrameEyePose(outPose, eyeId);
  239. }
  240. bool OculusVRDevice::providesEyeOffsets() const
  241. {
  242. if(!mHMDDevices.size())
  243. return false;
  244. return true;
  245. }
  246. void OculusVRDevice::getEyeOffsets(Point3F *dest) const
  247. {
  248. if(!mHMDDevices.size())
  249. return;
  250. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  251. if(!hmd)
  252. return;
  253. hmd->getEyeOffsets(dest);
  254. }
  255. bool OculusVRDevice::providesFovPorts() const
  256. {
  257. if(!mHMDDevices.size())
  258. return false;
  259. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  260. if(!hmd)
  261. return Point3F::Zero;
  262. return true;
  263. }
  264. void OculusVRDevice::getFovPorts(FovPort *out) const
  265. {
  266. if(!mHMDDevices.size())
  267. return;
  268. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  269. if(!hmd)
  270. return;
  271. return hmd->getFovPorts(out);
  272. }
  273. bool OculusVRDevice::providesProjectionOffset() const
  274. {
  275. if(!mHMDDevices.size())
  276. return false;
  277. return false;
  278. }
  279. const Point2F& OculusVRDevice::getProjectionOffset() const
  280. {
  281. if(!mHMDDevices.size())
  282. return Point2F::Zero;
  283. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  284. if(!hmd)
  285. return Point2F::Zero;
  286. return hmd->getProjectionCenterOffset();
  287. }
  288. void OculusVRDevice::getStereoViewports(RectI *out) const
  289. {
  290. if(!mHMDDevices.size())
  291. return;
  292. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  293. if(!hmd)
  294. return;
  295. hmd->getStereoViewports(out);
  296. }
  297. void OculusVRDevice::getStereoTargets(GFXTextureTarget **out) const
  298. {
  299. if(!mHMDDevices.size())
  300. return;
  301. const OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  302. if(!hmd)
  303. return;
  304. hmd->getStereoTargets(out);
  305. }
  306. void OculusVRDevice::onStartFrame()
  307. {
  308. _handleDeviceEvent(GFXDevice::deStartOfFrame);
  309. }
  310. //-----------------------------------------------------------------------------
  311. OculusVRHMDDevice* OculusVRDevice::getHMDDevice(U32 index) const
  312. {
  313. if(index >= mHMDDevices.size())
  314. return NULL;
  315. return mHMDDevices[index];
  316. }
  317. F32 OculusVRDevice::getHMDCurrentIPD(U32 index)
  318. {
  319. if(index >= mHMDDevices.size())
  320. return -1.0f;
  321. return mHMDDevices[index]->getIPD();
  322. }
  323. void OculusVRDevice::setHMDCurrentIPD(U32 index, F32 ipd)
  324. {
  325. if(index >= mHMDDevices.size())
  326. return;
  327. return mHMDDevices[index]->setIPD(ipd);
  328. }
  329. void OculusVRDevice::setOptimalDisplaySize(U32 index, GuiCanvas *canvas)
  330. {
  331. if(index >= mHMDDevices.size())
  332. return;
  333. mHMDDevices[index]->setOptimalDisplaySize(canvas);
  334. }
  335. //-----------------------------------------------------------------------------
  336. const OculusVRSensorDevice* OculusVRDevice::getSensorDevice(U32 index) const
  337. {
  338. if(index >= mHMDDevices.size())
  339. return NULL;
  340. return mHMDDevices[index]->getSensorDevice();
  341. }
  342. EulerF OculusVRDevice::getSensorEulerRotation(U32 index)
  343. {
  344. if(index >= mHMDDevices.size())
  345. return Point3F::Zero;
  346. return mHMDDevices[index]->getSensorDevice()->getEulerRotation();
  347. }
  348. VectorF OculusVRDevice::getSensorAcceleration(U32 index)
  349. {
  350. if(index >= mHMDDevices.size())
  351. return Point3F::Zero;
  352. return mHMDDevices[index]->getSensorDevice()->getAcceleration();
  353. }
  354. EulerF OculusVRDevice::getSensorAngularVelocity(U32 index)
  355. {
  356. if(index >= mHMDDevices.size())
  357. return Point3F::Zero;
  358. return mHMDDevices[index]->getSensorDevice()->getAngularVelocity();
  359. }
  360. bool OculusVRDevice::getSensorYawCorrection(U32 index)
  361. {
  362. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  363. if(!sensor || !sensor->isValid())
  364. return false;
  365. return sensor->getYawCorrection();
  366. }
  367. void OculusVRDevice::setSensorYawCorrection(U32 index, bool state)
  368. {
  369. if(index >= mHMDDevices.size())
  370. return;
  371. OculusVRSensorDevice* sensor = mHMDDevices[index]->getSensorDevice();
  372. if(!sensor->isValid())
  373. return;
  374. sensor->setYawCorrection(state);
  375. }
  376. bool OculusVRDevice::getSensorMagnetometerCalibrated(U32 index)
  377. {
  378. const OculusVRSensorDevice* sensor = getSensorDevice(index);
  379. if(!sensor || !sensor->isValid())
  380. return false;
  381. return sensor->getMagnetometerCalibrationAvailable();
  382. }
  383. void OculusVRDevice::resetAllSensors()
  384. {
  385. // Reset each sensor
  386. for(U32 i=0; i<mHMDDevices.size(); ++i)
  387. {
  388. mHMDDevices[i]->getSensorDevice()->reset();
  389. }
  390. }
  391. bool OculusVRDevice::isDiplayingWarning()
  392. {
  393. for(U32 i=0; i<mHMDDevices.size(); ++i)
  394. {
  395. if (mHMDDevices[i]->isDisplayingWarning())
  396. return true;
  397. }
  398. return false;
  399. }
  400. void OculusVRDevice::dismissWarning()
  401. {
  402. for(U32 i=0; i<mHMDDevices.size(); ++i)
  403. {
  404. mHMDDevices[i]->dismissWarning();
  405. }
  406. }
  407. String OculusVRDevice::dumpMetrics(U32 idx)
  408. {
  409. return mHMDDevices[idx]->dumpMetrics();
  410. }
  411. void OculusVRDevice::setDrawCanvas(GuiCanvas *canvas)
  412. {
  413. if(!mHMDDevices.size())
  414. return;
  415. OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  416. if(!hmd)
  417. return;
  418. hmd->setDrawCanvas(canvas);
  419. }
  420. void OculusVRDevice::setCurrentConnection(GameConnection *connection)
  421. {
  422. if(!mHMDDevices.size())
  423. return;
  424. OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  425. if(!hmd)
  426. return;
  427. hmd->setCurrentConnection(connection);
  428. }
  429. GameConnection* OculusVRDevice::getCurrentConnection()
  430. {
  431. if(!mHMDDevices.size())
  432. return NULL;
  433. OculusVRHMDDevice* hmd = getHMDDevice(mActiveDeviceId);
  434. if(!hmd)
  435. return NULL;
  436. return hmd->getCurrentConnection();
  437. }
  438. //-----------------------------------------------------------------------------
  439. DefineEngineFunction(isOculusVRDeviceActive, bool, (),,
  440. "@brief Used to determine if the Oculus VR input device is active\n\n"
  441. "The Oculus VR device is considered active when the library has been "
  442. "initialized and either a real of simulated HMD is present.\n\n"
  443. "@return True if the Oculus VR input device is active.\n"
  444. "@ingroup Game")
  445. {
  446. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  447. {
  448. return false;
  449. }
  450. return OCULUSVRDEV->getActive();
  451. }
  452. //-----------------------------------------------------------------------------
  453. DefineEngineFunction(setOVRHMDAsGameConnectionDisplayDevice, bool, (GameConnection* conn),,
  454. "@brief Sets the first HMD to be a GameConnection's display device\n\n"
  455. "@param conn The GameConnection to set.\n"
  456. "@return True if the GameConnection display device was set.\n"
  457. "@ingroup Game")
  458. {
  459. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  460. {
  461. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): No Oculus VR Device present.");
  462. return false;
  463. }
  464. if(!conn)
  465. {
  466. Con::errorf("setOVRHMDAsGameConnectionDisplayDevice(): Invalid GameConnection.");
  467. return false;
  468. }
  469. conn->setDisplayDevice(OCULUSVRDEV);
  470. return true;
  471. }
  472. //-----------------------------------------------------------------------------
  473. DefineEngineFunction(setOptimalOVRCanvasSize, bool, (GuiCanvas* canvas),,
  474. "@brief Sets the first HMD to be a GameConnection's display device\n\n"
  475. "@param conn The GameConnection to set.\n"
  476. "@return True if the GameConnection display device was set.\n"
  477. "@ingroup Game")
  478. {
  479. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  480. {
  481. Con::errorf("setOptimalOVRCanvasSize(): No Oculus VR Device present.");
  482. return false;
  483. }
  484. if(!canvas)
  485. {
  486. Con::errorf("setOptimalOVRCanvasSize(): Invalid Canvas.");
  487. return false;
  488. }
  489. OCULUSVRDEV->setOptimalDisplaySize(0, canvas);
  490. return true;
  491. }
  492. //-----------------------------------------------------------------------------
  493. DefineEngineFunction(getOVRHMDCount, S32, (),,
  494. "@brief Get the number of HMD devices that are currently connected.\n\n"
  495. "@return The number of Oculus VR HMD devices that are currently connected.\n"
  496. "@ingroup Game")
  497. {
  498. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  499. {
  500. return 0;
  501. }
  502. return OCULUSVRDEV->getHMDCount();
  503. }
  504. DefineEngineFunction(getOVRHMDProductName, const char*, (S32 index),,
  505. "@brief Retrieves the HMD product name.\n\n"
  506. "@param index The HMD index.\n"
  507. "@return The name of the HMD product.\n"
  508. "@ingroup Game")
  509. {
  510. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  511. {
  512. return "";
  513. }
  514. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  515. if(!hmd)
  516. {
  517. return "";
  518. }
  519. return hmd->getProductName();
  520. }
  521. DefineEngineFunction(getOVRHMDManufacturer, const char*, (S32 index),,
  522. "@brief Retrieves the HMD manufacturer name.\n\n"
  523. "@param index The HMD index.\n"
  524. "@return The manufacturer of the HMD product.\n"
  525. "@ingroup Game")
  526. {
  527. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  528. {
  529. return "";
  530. }
  531. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  532. if(!hmd)
  533. {
  534. return "";
  535. }
  536. return hmd->getManufacturer();
  537. }
  538. DefineEngineFunction(getOVRHMDVersion, S32, (S32 index),,
  539. "@brief Retrieves the HMD version number.\n\n"
  540. "@param index The HMD index.\n"
  541. "@return The version number of the HMD product.\n"
  542. "@ingroup Game")
  543. {
  544. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  545. {
  546. return -1;
  547. }
  548. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  549. if(!hmd)
  550. {
  551. return -1;
  552. }
  553. return hmd->getVersion();
  554. }
  555. DefineEngineFunction(getOVRHMDDisplayDeviceType, const char*, (S32 index),,
  556. "@brief Windows display device name used in EnumDisplaySettings/CreateDC.\n\n"
  557. "@param index The HMD index.\n"
  558. "@return The name of the HMD display device, if any.\n"
  559. "@ingroup Game")
  560. {
  561. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  562. {
  563. return "";
  564. }
  565. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  566. if(!hmd)
  567. {
  568. return "";
  569. }
  570. return hmd->getDisplayDeviceType();
  571. }
  572. DefineEngineFunction(getOVRHMDDisplayDeviceId, S32, (S32 index),,
  573. "@brief MacOS display ID.\n\n"
  574. "@param index The HMD index.\n"
  575. "@return The ID of the HMD display device, if any.\n"
  576. "@ingroup Game")
  577. {
  578. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  579. {
  580. return -1;
  581. }
  582. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  583. if(!hmd)
  584. {
  585. return -1;
  586. }
  587. return hmd->getDisplayDeviceId();
  588. }
  589. DefineEngineFunction(getOVRHMDResolution, Point2I, (S32 index),,
  590. "@brief Provides the OVR HMD screen resolution.\n\n"
  591. "@param index The HMD index.\n"
  592. "@return A two component string with the screen's resolution.\n"
  593. "@ingroup Game")
  594. {
  595. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  596. {
  597. return Point2I(1280, 800);
  598. }
  599. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  600. if(!hmd)
  601. {
  602. return Point2I(1280, 800);
  603. }
  604. return hmd->getResolution();
  605. }
  606. DefineEngineFunction(getOVRHMDProfileIPD, F32, (S32 index),,
  607. "@brief Physical distance between the user's eye centers as defined by the current profile.\n\n"
  608. "@param index The HMD index.\n"
  609. "@return The profile IPD.\n"
  610. "@ingroup Game")
  611. {
  612. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  613. {
  614. return -1.0f;
  615. }
  616. const OculusVRHMDDevice* hmd = OCULUSVRDEV->getHMDDevice(index);
  617. if(!hmd)
  618. {
  619. return -1.0f;
  620. }
  621. return hmd->getProfileIPD();
  622. }
  623. DefineEngineFunction(getOVRHMDCurrentIPD, F32, (S32 index),,
  624. "@brief Physical distance between the user's eye centers.\n\n"
  625. "@param index The HMD index.\n"
  626. "@return The current IPD.\n"
  627. "@ingroup Game")
  628. {
  629. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  630. {
  631. return -1.0f;
  632. }
  633. return OCULUSVRDEV->getHMDCurrentIPD(index);
  634. }
  635. DefineEngineFunction(setOVRHMDCurrentIPD, void, (S32 index, F32 ipd),,
  636. "@brief Set the physical distance between the user's eye centers.\n\n"
  637. "@param index The HMD index.\n"
  638. "@param ipd The IPD to use.\n"
  639. "@ingroup Game")
  640. {
  641. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  642. {
  643. return;
  644. }
  645. OCULUSVRDEV->setHMDCurrentIPD(index, ipd);
  646. }
  647. //-----------------------------------------------------------------------------
  648. DefineEngineFunction(getOVRSensorCount, S32, (),,
  649. "@brief Get the number of sensor devices that are currently connected.\n\n"
  650. "@return The number of Oculus VR sensor devices that are currently connected.\n"
  651. "@ingroup Game")
  652. {
  653. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  654. {
  655. return 0;
  656. }
  657. return OCULUSVRDEV->getSensorCount();
  658. }
  659. DefineEngineFunction(getOVRSensorEulerRotation, Point3F, (S32 index),,
  660. "@brief Get the Euler rotation values for the given sensor index.\n\n"
  661. "@param index The sensor index.\n"
  662. "@return The Euler rotation values of the Oculus VR sensor, in degrees.\n"
  663. "@ingroup Game")
  664. {
  665. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  666. {
  667. return Point3F::Zero;
  668. }
  669. EulerF rot = OCULUSVRDEV->getSensorEulerRotation(index);
  670. return Point3F(mRadToDeg(rot.x), mRadToDeg(rot.y), mRadToDeg(rot.z));
  671. }
  672. DefineEngineFunction(getOVRSensorAcceleration, Point3F, (S32 index),,
  673. "@brief Get the acceleration values for the given sensor index.\n\n"
  674. "@param index The sensor index.\n"
  675. "@return The acceleration values of the Oculus VR sensor, in m/s^2.\n"
  676. "@ingroup Game")
  677. {
  678. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  679. {
  680. return Point3F::Zero;
  681. }
  682. return OCULUSVRDEV->getSensorAcceleration(index);
  683. }
  684. DefineEngineFunction(getOVRSensorAngVelocity, Point3F, (S32 index),,
  685. "@brief Get the angular velocity values for the given sensor index.\n\n"
  686. "@param index The sensor index.\n"
  687. "@return The angular velocity values of the Oculus VR sensor, in degrees/s.\n"
  688. "@ingroup Game")
  689. {
  690. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  691. {
  692. return Point3F::Zero;
  693. }
  694. EulerF rot = OCULUSVRDEV->getSensorAngularVelocity(index);
  695. return Point3F(mRadToDeg(rot.x), mRadToDeg(rot.y), mRadToDeg(rot.z));
  696. }
  697. DefineEngineFunction(getOVRSensorYawCorrection, bool, (S32 index),,
  698. "@brief Get the yaw correction state for the given sensor index.\n\n"
  699. "@param index The sensor index.\n"
  700. "@return True if yaw correction (using magnetometer calibration data) is active.\n"
  701. "@ingroup Game")
  702. {
  703. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  704. {
  705. return false;
  706. }
  707. return OCULUSVRDEV->getSensorYawCorrection(index);
  708. }
  709. DefineEngineFunction(setOVRSensorYawCorrection, void, (S32 index, bool state),,
  710. "@brief Set the yaw correction state for the given sensor index.\n\n"
  711. "@param index The sensor index.\n"
  712. "@param state The yaw correction state to change to.\n"
  713. "@note Yaw correction cannot be enabled if the user has disabled it through "
  714. "the Oculus VR control panel.\n"
  715. "@ingroup Game")
  716. {
  717. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  718. {
  719. return;
  720. }
  721. OCULUSVRDEV->setSensorYawCorrection(index, state);
  722. }
  723. DefineEngineFunction(getOVRSensorMagnetometerCalibrated, bool, (S32 index),,
  724. "@brief Get the magnetometer calibrated data state for the given sensor index.\n\n"
  725. "@param index The sensor index.\n"
  726. "@return True if magnetometer calibration data is available.\n"
  727. "@ingroup Game")
  728. {
  729. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  730. {
  731. return false;
  732. }
  733. return OCULUSVRDEV->getSensorMagnetometerCalibrated(index);
  734. }
  735. DefineEngineFunction(ovrResetAllSensors, void, (),,
  736. "@brief Resets all Oculus VR sensors.\n\n"
  737. "This resets all sensor orientations such that their 'normal' rotation "
  738. "is defined when this function is called. This defines an HMD's forwards "
  739. "and up direction, for example."
  740. "@ingroup Game")
  741. {
  742. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  743. {
  744. return;
  745. }
  746. OCULUSVRDEV->resetAllSensors();
  747. }
  748. DefineEngineFunction(ovrIsDisplayingWarning, bool, (),,
  749. "@brief returns is warning is being displayed.\n\n"
  750. "@ingroup Game")
  751. {
  752. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  753. {
  754. return false;
  755. }
  756. return OCULUSVRDEV->isDiplayingWarning();
  757. }
  758. DefineEngineFunction(ovrDismissWarnings, void, (),,
  759. "@brief dismisses warnings.\n\n"
  760. "@ingroup Game")
  761. {
  762. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  763. {
  764. return;
  765. }
  766. OCULUSVRDEV->dismissWarning();
  767. }
  768. DefineEngineFunction(ovrDumpMetrics, String, (S32 idx),(0),
  769. "@brief dumps sensor metrics.\n\n"
  770. "@ingroup Game")
  771. {
  772. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  773. {
  774. return "";
  775. }
  776. return OCULUSVRDEV->dumpMetrics(idx);
  777. }
  778. bool OculusVRDevice::_handleDeviceEvent( GFXDevice::GFXDeviceEventType evt )
  779. {
  780. if(!ManagedSingleton<OculusVRDevice>::instanceOrNull())
  781. {
  782. return true;
  783. }
  784. switch( evt )
  785. {
  786. case GFXDevice::deStartOfFrame:
  787. for (U32 i=0; i<OCULUSVRDEV->mHMDDevices.size(); i++)
  788. {
  789. OCULUSVRDEV->mHMDDevices[i]->onStartFrame();
  790. }
  791. // Fall through
  792. break;
  793. case GFXDevice::dePostFrame:
  794. for (U32 i=0; i<OCULUSVRDEV->mHMDDevices.size(); i++)
  795. {
  796. OCULUSVRDEV->mHMDDevices[i]->onEndFrame();
  797. }
  798. break;
  799. case GFXDevice::deDestroy:
  800. for (U32 i=0; i<OCULUSVRDEV->mHMDDevices.size(); i++)
  801. {
  802. OCULUSVRDEV->mHMDDevices[i]->onDeviceDestroy();
  803. }
  804. default:
  805. break;
  806. }
  807. return true;
  808. }