openxr_interface.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**************************************************************************/
  2. /* openxr_interface.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef OPENXR_INTERFACE_H
  31. #define OPENXR_INTERFACE_H
  32. #include "action_map/openxr_action_map.h"
  33. #include "extensions/openxr_fb_passthrough_extension_wrapper.h"
  34. #include "openxr_api.h"
  35. #include "servers/xr/xr_interface.h"
  36. #include "servers/xr/xr_positional_tracker.h"
  37. // declare some default strings
  38. #define INTERACTION_PROFILE_NONE "/interaction_profiles/none"
  39. class OpenXRInterface : public XRInterface {
  40. GDCLASS(OpenXRInterface, XRInterface);
  41. private:
  42. OpenXRAPI *openxr_api = nullptr;
  43. bool initialized = false;
  44. XRInterface::TrackingStatus tracking_state;
  45. OpenXRFbPassthroughExtensionWrapper *passthrough_wrapper = nullptr;
  46. // At a minimum we need a tracker for our head
  47. Ref<XRPositionalTracker> head;
  48. Transform3D head_transform;
  49. Vector3 head_linear_velocity;
  50. Vector3 head_angular_velocity;
  51. Transform3D transform_for_view[2]; // We currently assume 2, but could be 4 for VARJO which we do not support yet
  52. void _load_action_map();
  53. struct Action { // An action we've registered with OpenXR
  54. String action_name; // Name of our action as presented to Godot (can be altered from the action map)
  55. OpenXRAction::ActionType action_type; // The action type of this action
  56. RID action_rid; // RID of the action registered with our OpenXR API
  57. };
  58. struct ActionSet { // An action set we've registered with OpenXR
  59. String action_set_name; // Name of our action set
  60. bool is_active; // If true this action set is active and we will sync it
  61. Vector<Action *> actions; // List of actions in this action set
  62. RID action_set_rid; // RID of the action registered with our OpenXR API
  63. };
  64. struct Tracker { // A tracker we've registered with OpenXR
  65. String tracker_name; // Name of our tracker (can be altered from the action map)
  66. Vector<Action *> actions; // Actions related to this tracker
  67. Ref<XRPositionalTracker> positional_tracker; // Our positional tracker object that holds our tracker state
  68. RID tracker_rid; // RID of the tracker registered with our OpenXR API
  69. RID interaction_profile; // RID of the interaction profile bound to this tracker (can be null)
  70. };
  71. Vector<ActionSet *> action_sets;
  72. Vector<RID> interaction_profiles;
  73. Vector<Tracker *> trackers;
  74. ActionSet *create_action_set(const String &p_action_set_name, const String &p_localized_name, const int p_priority);
  75. void free_action_sets();
  76. Action *create_action(ActionSet *p_action_set, const String &p_action_name, const String &p_localized_name, OpenXRAction::ActionType p_action_type, const Vector<Tracker *> p_trackers);
  77. Action *find_action(const String &p_action_name);
  78. void free_actions(ActionSet *p_action_set);
  79. Tracker *find_tracker(const String &p_tracker_name, bool p_create = false);
  80. void handle_tracker(Tracker *p_tracker);
  81. void free_trackers();
  82. void free_interaction_profiles();
  83. void _set_default_pos(Transform3D &p_transform, double p_world_scale, uint64_t p_eye);
  84. protected:
  85. static void _bind_methods();
  86. public:
  87. virtual StringName get_name() const override;
  88. virtual uint32_t get_capabilities() const override;
  89. virtual PackedStringArray get_suggested_tracker_names() const override;
  90. virtual TrackingStatus get_tracking_status() const override;
  91. bool initialize_on_startup() const;
  92. virtual bool is_initialized() const override;
  93. virtual bool initialize() override;
  94. virtual void uninitialize() override;
  95. virtual Dictionary get_system_info() override;
  96. virtual void 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 = 0) override;
  97. virtual bool supports_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  98. virtual XRInterface::PlayAreaMode get_play_area_mode() const override;
  99. virtual bool set_play_area_mode(XRInterface::PlayAreaMode p_mode) override;
  100. float get_display_refresh_rate() const;
  101. void set_display_refresh_rate(float p_refresh_rate);
  102. Array get_available_display_refresh_rates() const;
  103. bool is_action_set_active(const String &p_action_set) const;
  104. void set_action_set_active(const String &p_action_set, bool p_active);
  105. Array get_action_sets() const;
  106. double get_render_target_size_multiplier() const;
  107. void set_render_target_size_multiplier(double multiplier);
  108. virtual Size2 get_render_target_size() override;
  109. virtual uint32_t get_view_count() override;
  110. virtual Transform3D get_camera_transform() override;
  111. virtual Transform3D get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) override;
  112. virtual Projection get_projection_for_view(uint32_t p_view, double p_aspect, double p_z_near, double p_z_far) override;
  113. virtual RID get_color_texture() override;
  114. virtual RID get_depth_texture() override;
  115. virtual void process() override;
  116. virtual void pre_render() override;
  117. bool pre_draw_viewport(RID p_render_target) override;
  118. virtual Vector<BlitToScreen> post_draw_viewport(RID p_render_target, const Rect2 &p_screen_rect) override;
  119. virtual void end_frame() override;
  120. virtual bool is_passthrough_supported() override;
  121. virtual bool is_passthrough_enabled() override;
  122. virtual bool start_passthrough() override;
  123. virtual void stop_passthrough() override;
  124. /** environment blend mode. */
  125. virtual Array get_supported_environment_blend_modes() override;
  126. virtual bool set_environment_blend_mode(XRInterface::EnvironmentBlendMode mode) override;
  127. void on_state_ready();
  128. void on_state_visible();
  129. void on_state_focused();
  130. void on_state_stopping();
  131. void on_pose_recentered();
  132. void tracker_profile_changed(RID p_tracker, RID p_interaction_profile);
  133. OpenXRInterface();
  134. ~OpenXRInterface();
  135. };
  136. #endif // OPENXR_INTERFACE_H