oculusVRDevice.cpp 26 KB

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