openxr_interface.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*************************************************************************/
  2. /* openxr_interface.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "openxr_interface.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/io/resource_saver.h"
  33. #include "servers/rendering/rendering_server_globals.h"
  34. void OpenXRInterface::_bind_methods() {
  35. // todo
  36. }
  37. StringName OpenXRInterface::get_name() const {
  38. return StringName("OpenXR");
  39. };
  40. uint32_t OpenXRInterface::get_capabilities() const {
  41. return XRInterface::XR_VR + XRInterface::XR_STEREO;
  42. };
  43. XRInterface::TrackingStatus OpenXRInterface::get_tracking_status() const {
  44. return tracking_state;
  45. }
  46. void OpenXRInterface::_load_action_map() {
  47. ERR_FAIL_NULL(openxr_api);
  48. // This may seem a bit duplicitous to a little bit of background info here.
  49. // OpenXRActionMap (with all its sub resource classes) is a class that allows us to configure and store an action map in.
  50. // This gives the user the ability to edit the action map in a UI and customise the actions.
  51. // OpenXR however requires us to submit an action map and it takes over from that point and we can no longer change it.
  52. // This system does that push and we store the info needed to then work with this action map going forward.
  53. // Within our openxr device we maintain a number of classes that wrap the relevant OpenXR objects for this.
  54. // Within OpenXRInterface we have a few internal classes that keep track of what we've created.
  55. // This allow us to process the relevant actions each frame.
  56. // just in case clean up
  57. free_action_sets();
  58. free_trackers();
  59. Ref<OpenXRActionMap> action_map;
  60. if (Engine::get_singleton()->is_editor_hint()) {
  61. #ifdef TOOLS_ENABLED
  62. action_map.instantiate();
  63. action_map->create_editor_action_sets();
  64. #endif
  65. } else {
  66. String default_tres_name = openxr_api->get_default_action_map_resource_name();
  67. // Check if we can load our default
  68. if (ResourceLoader::exists(default_tres_name)) {
  69. action_map = ResourceLoader::load(default_tres_name);
  70. }
  71. // Check if we need to create default action set
  72. if (action_map.is_null()) {
  73. action_map.instantiate();
  74. action_map->create_default_action_sets();
  75. #ifdef TOOLS_ENABLED
  76. // Save our action sets so our user can
  77. action_map->set_path(default_tres_name, true);
  78. ResourceSaver::save(default_tres_name, action_map);
  79. #endif
  80. }
  81. }
  82. // process our action map
  83. if (action_map.is_valid()) {
  84. Map<Ref<OpenXRAction>, RID> action_rids;
  85. Array action_sets = action_map->get_action_sets();
  86. for (int i = 0; i < action_sets.size(); i++) {
  87. // Create our action set
  88. Ref<OpenXRActionSet> xr_action_set = action_sets[i];
  89. ActionSet *action_set = create_action_set(xr_action_set->get_name(), xr_action_set->get_localized_name(), xr_action_set->get_priority());
  90. if (!action_set) {
  91. continue;
  92. }
  93. // Now create our actions for these
  94. Array actions = xr_action_set->get_actions();
  95. for (int j = 0; j < actions.size(); j++) {
  96. Ref<OpenXRAction> xr_action = actions[j];
  97. PackedStringArray toplevel_paths = xr_action->get_toplevel_paths();
  98. Vector<RID> toplevel_rids;
  99. Vector<Tracker *> trackers;
  100. for (int k = 0; k < toplevel_paths.size(); k++) {
  101. Tracker *tracker = get_tracker(toplevel_paths[k]);
  102. if (tracker) {
  103. toplevel_rids.push_back(tracker->path_rid);
  104. trackers.push_back(tracker);
  105. }
  106. }
  107. Action *action = create_action(action_set, xr_action->get_name(), xr_action->get_localized_name(), xr_action->get_action_type(), toplevel_rids);
  108. if (action) {
  109. // we link our actions back to our trackers so we know which actions to check when we're processing our trackers
  110. for (int t = 0; t < trackers.size(); t++) {
  111. link_action_to_tracker(trackers[t], action);
  112. }
  113. // add this to our map for creating our interaction profiles
  114. action_rids[xr_action] = action->action_rid;
  115. }
  116. }
  117. }
  118. // now do our suggestions
  119. Array interaction_profiles = action_map->get_interaction_profiles();
  120. for (int i = 0; i < interaction_profiles.size(); i++) {
  121. Vector<OpenXRAPI::Binding> bindings;
  122. Ref<OpenXRInteractionProfile> xr_interaction_profile = interaction_profiles[i];
  123. Array xr_bindings = xr_interaction_profile->get_bindings();
  124. for (int j = 0; j < xr_bindings.size(); j++) {
  125. Ref<OpenXRIPBinding> xr_binding = xr_bindings[j];
  126. Ref<OpenXRAction> xr_action = xr_binding->get_action();
  127. OpenXRAPI::Binding binding;
  128. if (action_rids.has(xr_action)) {
  129. binding.action = action_rids[xr_action];
  130. } else {
  131. print_line("Action ", xr_action->get_name(), " isn't part of an action set!");
  132. continue;
  133. }
  134. PackedStringArray xr_paths = xr_binding->get_paths();
  135. for (int k = 0; k < xr_paths.size(); k++) {
  136. binding.path = xr_paths[k];
  137. bindings.push_back(binding);
  138. }
  139. }
  140. openxr_api->suggest_bindings(xr_interaction_profile->get_interaction_profile_path(), bindings);
  141. }
  142. }
  143. }
  144. OpenXRInterface::ActionSet *OpenXRInterface::create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority) {
  145. ERR_FAIL_NULL_V(openxr_api, nullptr);
  146. // find if it already exists
  147. for (int i = 0; i < action_sets.size(); i++) {
  148. if (action_sets[i]->action_set_name == p_action_set_name) {
  149. // already exists in this set
  150. return nullptr;
  151. }
  152. }
  153. ActionSet *action_set = memnew(ActionSet);
  154. action_set->action_set_name = p_action_set_name;
  155. action_set->is_active = true;
  156. action_set->action_set_rid = openxr_api->action_set_create(p_action_set_name, p_localized_name, p_priority);
  157. action_sets.push_back(action_set);
  158. return action_set;
  159. }
  160. void OpenXRInterface::free_action_sets() {
  161. ERR_FAIL_NULL(openxr_api);
  162. for (int i = 0; i < action_sets.size(); i++) {
  163. ActionSet *action_set = action_sets[i];
  164. openxr_api->path_free(action_set->action_set_rid);
  165. free_actions(action_set);
  166. memfree(action_set);
  167. }
  168. action_sets.clear();
  169. }
  170. OpenXRInterface::Action *OpenXRInterface::create_action(ActionSet *p_action_set, const String &p_action_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<RID> p_toplevel_paths) {
  171. ERR_FAIL_NULL_V(openxr_api, nullptr);
  172. for (int i = 0; i < p_action_set->actions.size(); i++) {
  173. if (p_action_set->actions[i]->action_name == p_action_name) {
  174. // already exists in this set
  175. return nullptr;
  176. }
  177. }
  178. Action *action = memnew(Action);
  179. action->action_name = p_action_name;
  180. action->action_type = p_action_type;
  181. action->action_rid = openxr_api->action_create(p_action_set->action_set_rid, p_action_name, p_localized_name, p_action_type, p_toplevel_paths);
  182. p_action_set->actions.push_back(action);
  183. return action;
  184. }
  185. OpenXRInterface::Action *OpenXRInterface::find_action(const String &p_action_name) {
  186. // We just find the first action by this name
  187. for (int i = 0; i < action_sets.size(); i++) {
  188. for (int j = 0; j < action_sets[i]->actions.size(); j++) {
  189. if (action_sets[i]->actions[j]->action_name == p_action_name) {
  190. return action_sets[i]->actions[j];
  191. }
  192. }
  193. }
  194. // not found
  195. return nullptr;
  196. }
  197. void OpenXRInterface::free_actions(ActionSet *p_action_set) {
  198. ERR_FAIL_NULL(openxr_api);
  199. for (int i = 0; i < p_action_set->actions.size(); i++) {
  200. Action *action = p_action_set->actions[i];
  201. openxr_api->action_free(action->action_rid);
  202. memdelete(action);
  203. }
  204. p_action_set->actions.clear();
  205. }
  206. OpenXRInterface::Tracker *OpenXRInterface::get_tracker(const String &p_path_name) {
  207. XRServer *xr_server = XRServer::get_singleton();
  208. ERR_FAIL_NULL_V(xr_server, nullptr);
  209. ERR_FAIL_NULL_V(openxr_api, nullptr);
  210. Tracker *tracker = nullptr;
  211. for (int i = 0; i < trackers.size(); i++) {
  212. tracker = trackers[i];
  213. if (tracker->path_name == p_path_name) {
  214. return tracker;
  215. }
  216. }
  217. // create our positional tracker
  218. Ref<XRPositionalTracker> positional_tracker;
  219. positional_tracker.instantiate();
  220. // We have standardised some names to make things nicer to the user so lets recognise the toplevel paths related to these.
  221. if (p_path_name == "/user/hand/left") {
  222. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  223. positional_tracker->set_tracker_name("left_hand");
  224. positional_tracker->set_tracker_desc("Left hand controller");
  225. positional_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_LEFT);
  226. } else if (p_path_name == "/user/hand/right") {
  227. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  228. positional_tracker->set_tracker_name("right_hand");
  229. positional_tracker->set_tracker_desc("Right hand controller");
  230. positional_tracker->set_tracker_hand(XRPositionalTracker::TRACKER_HAND_RIGHT);
  231. } else {
  232. positional_tracker->set_tracker_type(XRServer::TRACKER_CONTROLLER);
  233. positional_tracker->set_tracker_name(p_path_name);
  234. positional_tracker->set_tracker_desc(p_path_name);
  235. }
  236. xr_server->add_tracker(positional_tracker);
  237. // create a new entry
  238. tracker = memnew(Tracker);
  239. tracker->path_name = p_path_name;
  240. tracker->path_rid = openxr_api->path_create(p_path_name);
  241. tracker->positional_tracker = positional_tracker;
  242. trackers.push_back(tracker);
  243. return tracker;
  244. }
  245. OpenXRInterface::Tracker *OpenXRInterface::find_tracker(const String &p_positional_tracker_name) {
  246. for (int i = 0; i < trackers.size(); i++) {
  247. Tracker *tracker = trackers[i];
  248. if (tracker->positional_tracker.is_valid() && tracker->positional_tracker->get_tracker_name() == p_positional_tracker_name) {
  249. return tracker;
  250. }
  251. }
  252. return nullptr;
  253. }
  254. void OpenXRInterface::link_action_to_tracker(Tracker *p_tracker, Action *p_action) {
  255. if (p_tracker->actions.find(p_action) == -1) {
  256. p_tracker->actions.push_back(p_action);
  257. }
  258. }
  259. void OpenXRInterface::handle_tracker(Tracker *p_tracker) {
  260. ERR_FAIL_NULL(openxr_api);
  261. ERR_FAIL_COND(p_tracker->positional_tracker.is_null());
  262. // handle all the actions
  263. for (int i = 0; i < p_tracker->actions.size(); i++) {
  264. Action *action = p_tracker->actions[i];
  265. switch (action->action_type) {
  266. case OpenXRAction::OPENXR_ACTION_BOOL: {
  267. bool pressed = openxr_api->get_action_bool(action->action_rid, p_tracker->path_rid);
  268. p_tracker->positional_tracker->set_input(action->action_name, Variant(pressed));
  269. } break;
  270. case OpenXRAction::OPENXR_ACTION_FLOAT: {
  271. real_t value = openxr_api->get_action_float(action->action_rid, p_tracker->path_rid);
  272. p_tracker->positional_tracker->set_input(action->action_name, Variant(value));
  273. } break;
  274. case OpenXRAction::OPENXR_ACTION_VECTOR2: {
  275. Vector2 value = openxr_api->get_action_vector2(action->action_rid, p_tracker->path_rid);
  276. p_tracker->positional_tracker->set_input(action->action_name, Variant(value));
  277. } break;
  278. case OpenXRAction::OPENXR_ACTION_POSE: {
  279. Transform3D transform;
  280. Vector3 linear, angular;
  281. XRPose::TrackingConfidence confidence = openxr_api->get_action_pose(action->action_rid, p_tracker->path_rid, transform, linear, angular);
  282. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  283. String name;
  284. // We can't have dual action names in OpenXR hence we added _pose, but default, aim and grip and default pose action names in Godot so rename them on the tracker.
  285. // NOTE need to decide on whether we should keep the naming convention or rename it on Godots side
  286. if (action->action_name == "default_pose") {
  287. name = "default";
  288. } else if (action->action_name == "aim_pose") {
  289. name = "aim";
  290. } else if (action->action_name == "grip_pose") {
  291. name = "grip";
  292. } else {
  293. name = action->action_name;
  294. }
  295. p_tracker->positional_tracker->set_pose(name, transform, linear, angular, confidence);
  296. }
  297. } break;
  298. default: {
  299. // not yet supported
  300. } break;
  301. }
  302. }
  303. }
  304. void OpenXRInterface::trigger_haptic_pulse(const String &p_action_name, const StringName &p_tracker_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec) {
  305. ERR_FAIL_NULL(openxr_api);
  306. Action *action = find_action(p_action_name);
  307. ERR_FAIL_NULL(action);
  308. Tracker *tracker = find_tracker(p_tracker_name);
  309. ERR_FAIL_NULL(tracker);
  310. // TODO OpenXR does not support delay, so we may need to add support for that somehow...
  311. XrDuration duration = XrDuration(p_duration_sec * 1000000000.0); // seconds -> nanoseconds
  312. openxr_api->trigger_haptic_pulse(action->action_rid, tracker->path_rid, p_frequency, p_amplitude, duration);
  313. }
  314. void OpenXRInterface::free_trackers() {
  315. XRServer *xr_server = XRServer::get_singleton();
  316. ERR_FAIL_NULL(xr_server);
  317. ERR_FAIL_NULL(openxr_api);
  318. for (int i = 0; i < trackers.size(); i++) {
  319. Tracker *tracker = trackers[i];
  320. openxr_api->path_free(tracker->path_rid);
  321. xr_server->remove_tracker(tracker->positional_tracker);
  322. tracker->positional_tracker.unref();
  323. memdelete(tracker);
  324. }
  325. trackers.clear();
  326. }
  327. bool OpenXRInterface::initialise_on_startup() const {
  328. if (openxr_api == nullptr) {
  329. return false;
  330. } else if (!openxr_api->is_initialized()) {
  331. return false;
  332. } else {
  333. return true;
  334. }
  335. }
  336. bool OpenXRInterface::is_initialized() const {
  337. return initialized;
  338. };
  339. bool OpenXRInterface::initialize() {
  340. XRServer *xr_server = XRServer::get_singleton();
  341. ERR_FAIL_NULL_V(xr_server, false);
  342. if (openxr_api == nullptr) {
  343. return false;
  344. } else if (!openxr_api->is_initialized()) {
  345. return false;
  346. } else if (initialized) {
  347. return true;
  348. }
  349. // load up our action sets before setting up our session, note that our profiles are suggestions, OpenXR takes ownership of (re)binding
  350. _load_action_map();
  351. if (!openxr_api->initialise_session()) {
  352. return false;
  353. }
  354. // we must create a tracker for our head
  355. head.instantiate();
  356. head->set_tracker_type(XRServer::TRACKER_HEAD);
  357. head->set_tracker_name("head");
  358. head->set_tracker_desc("Players head");
  359. xr_server->add_tracker(head);
  360. // attach action sets
  361. for (int i = 0; i < action_sets.size(); i++) {
  362. openxr_api->action_set_attach(action_sets[i]->action_set_rid);
  363. }
  364. // make this our primary interface
  365. xr_server->set_primary_interface(this);
  366. initialized = true;
  367. return initialized;
  368. }
  369. void OpenXRInterface::uninitialize() {
  370. // Our OpenXR driver will clean itself up properly when Godot exits, so we just do some basic stuff here
  371. // end the session if we need to?
  372. // cleanup stuff
  373. free_action_sets();
  374. free_trackers();
  375. XRServer *xr_server = XRServer::get_singleton();
  376. if (xr_server) {
  377. if (head.is_valid()) {
  378. xr_server->remove_tracker(head);
  379. head.unref();
  380. }
  381. }
  382. initialized = false;
  383. }
  384. bool OpenXRInterface::supports_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  385. return false;
  386. }
  387. XRInterface::PlayAreaMode OpenXRInterface::get_play_area_mode() const {
  388. return XRInterface::XR_PLAY_AREA_UNKNOWN;
  389. }
  390. bool OpenXRInterface::set_play_area_mode(XRInterface::PlayAreaMode p_mode) {
  391. return false;
  392. }
  393. Size2 OpenXRInterface::get_render_target_size() {
  394. if (openxr_api == nullptr) {
  395. return Size2();
  396. } else {
  397. return openxr_api->get_recommended_target_size();
  398. }
  399. }
  400. uint32_t OpenXRInterface::get_view_count() {
  401. // TODO set this based on our configuration
  402. return 2;
  403. }
  404. void OpenXRInterface::_set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye) {
  405. p_transform = Transform3D();
  406. // if we're not tracking, don't put our head on the floor...
  407. p_transform.origin.y = 1.5 * p_world_scale;
  408. // overkill but..
  409. if (p_eye == 1) {
  410. p_transform.origin.x = 0.03 * p_world_scale;
  411. } else if (p_eye == 2) {
  412. p_transform.origin.x = -0.03 * p_world_scale;
  413. }
  414. }
  415. Transform3D OpenXRInterface::get_camera_transform() {
  416. XRServer *xr_server = XRServer::get_singleton();
  417. ERR_FAIL_NULL_V(xr_server, Transform3D());
  418. Transform3D hmd_transform;
  419. double world_scale = xr_server->get_world_scale();
  420. // head_transform should be updated in process
  421. hmd_transform.basis = head_transform.basis;
  422. hmd_transform.origin = head_transform.origin * world_scale;
  423. return hmd_transform;
  424. }
  425. Transform3D OpenXRInterface::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  426. XRServer *xr_server = XRServer::get_singleton();
  427. ERR_FAIL_NULL_V(xr_server, Transform3D());
  428. Transform3D t;
  429. if (openxr_api && openxr_api->get_view_transform(p_view, t)) {
  430. // update our cached value if we have a valid transform
  431. transform_for_view[p_view] = t;
  432. } else {
  433. // reuse cached value
  434. t = transform_for_view[p_view];
  435. }
  436. // Apply our world scale
  437. double world_scale = xr_server->get_world_scale();
  438. t.origin *= world_scale;
  439. return p_cam_transform * xr_server->get_reference_frame() * t;
  440. }
  441. CameraMatrix OpenXRInterface::get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) {
  442. CameraMatrix cm;
  443. if (openxr_api) {
  444. if (openxr_api->get_view_projection(p_view, p_z_near, p_z_far, cm)) {
  445. return cm;
  446. }
  447. }
  448. // Failed to get from our OpenXR device? Default to some sort of sensible camera matrix..
  449. cm.set_for_hmd(p_view + 1, 1.0, 6.0, 14.5, 4.0, 1.5, p_z_near, p_z_far);
  450. return cm;
  451. }
  452. void OpenXRInterface::process() {
  453. if (openxr_api) {
  454. // do our normal process
  455. if (openxr_api->process()) {
  456. Transform3D t;
  457. Vector3 linear_velocity;
  458. Vector3 angular_velocity;
  459. XRPose::TrackingConfidence confidence = openxr_api->get_head_center(t, linear_velocity, angular_velocity);
  460. if (confidence != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  461. // Only update our transform if we have one to update it with
  462. // note that poses are stored without world scale and reference frame applied!
  463. head_transform = t;
  464. head_linear_velocity = linear_velocity;
  465. head_angular_velocity = angular_velocity;
  466. }
  467. }
  468. // handle our action sets....
  469. Vector<RID> active_sets;
  470. for (int i = 0; i < action_sets.size(); i++) {
  471. if (action_sets[i]->is_active) {
  472. active_sets.push_back(action_sets[i]->action_set_rid);
  473. }
  474. }
  475. if (openxr_api->sync_action_sets(active_sets)) {
  476. for (int i = 0; i < trackers.size(); i++) {
  477. handle_tracker(trackers[i]);
  478. }
  479. }
  480. }
  481. if (head.is_valid()) {
  482. // TODO figure out how to get our velocities
  483. head->set_pose("default", head_transform, head_linear_velocity, head_angular_velocity);
  484. // TODO set confidence on pose once we support tracking this..
  485. }
  486. }
  487. void OpenXRInterface::pre_render() {
  488. if (openxr_api) {
  489. openxr_api->pre_render();
  490. }
  491. }
  492. bool OpenXRInterface::pre_draw_viewport(RID p_render_target) {
  493. if (openxr_api) {
  494. return openxr_api->pre_draw_viewport(p_render_target);
  495. } else {
  496. // don't render
  497. return false;
  498. }
  499. }
  500. Vector<BlitToScreen> OpenXRInterface::post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) {
  501. Vector<BlitToScreen> blit_to_screen;
  502. // If separate HMD we should output one eye to screen
  503. if (p_screen_rect != Rect2()) {
  504. BlitToScreen blit;
  505. blit.render_target = p_render_target;
  506. blit.multi_view.use_layer = true;
  507. blit.multi_view.layer = 0;
  508. blit.lens_distortion.apply = false;
  509. Size2 render_size = get_render_target_size();
  510. Rect2 dst_rect = p_screen_rect;
  511. float new_height = dst_rect.size.x * (render_size.y / render_size.x);
  512. if (new_height > dst_rect.size.y) {
  513. dst_rect.position.y = (0.5 * dst_rect.size.y) - (0.5 * new_height);
  514. dst_rect.size.y = new_height;
  515. } else {
  516. float new_width = dst_rect.size.y * (render_size.x / render_size.y);
  517. dst_rect.position.x = (0.5 * dst_rect.size.x) - (0.5 * new_width);
  518. dst_rect.size.x = new_width;
  519. }
  520. blit.dst_rect = dst_rect;
  521. blit_to_screen.push_back(blit);
  522. }
  523. if (openxr_api) {
  524. openxr_api->post_draw_viewport(p_render_target);
  525. }
  526. return blit_to_screen;
  527. }
  528. void OpenXRInterface::end_frame() {
  529. if (openxr_api) {
  530. openxr_api->end_frame();
  531. }
  532. }
  533. OpenXRInterface::OpenXRInterface() {
  534. openxr_api = OpenXRAPI::get_singleton();
  535. // while we don't have head tracking, don't put the headset on the floor...
  536. _set_default_pos(head_transform, 1.0, 0);
  537. _set_default_pos(transform_for_view[0], 1.0, 1);
  538. _set_default_pos(transform_for_view[1], 1.0, 2);
  539. }
  540. OpenXRInterface::~OpenXRInterface() {
  541. openxr_api = nullptr;
  542. }