arvr_server.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* arvr_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifndef ARVR_SERVER_H
  31. #define ARVR_SERVER_H
  32. #include "core/os/os.h"
  33. #include "core/os/thread_safe.h"
  34. #include "core/reference.h"
  35. #include "core/rid.h"
  36. #include "core/variant.h"
  37. class ARVRInterface;
  38. class ARVRPositionalTracker;
  39. /**
  40. @author Bastiaan Olij <mux213@gmail.com>
  41. The ARVR server is a singleton object that gives access to the various
  42. objects and SDKs that are available on the system.
  43. Because there can be multiple SDKs active this is exposed as an array
  44. and our ARVR server object acts as a pass through
  45. Also each positioning tracker is accessible from here.
  46. I've added some additional info into this header file that should move
  47. into the documentation, I will do so when we're close to accepting this PR
  48. or as a separate PR once this has been merged into the master branch.
  49. **/
  50. class ARVRServer : public Object {
  51. GDCLASS(ARVRServer, Object);
  52. _THREAD_SAFE_CLASS_
  53. public:
  54. enum TrackerType {
  55. TRACKER_CONTROLLER = 0x01, /* tracks a controller */
  56. TRACKER_BASESTATION = 0x02, /* tracks location of a base station */
  57. TRACKER_ANCHOR = 0x04, /* tracks an anchor point, used in AR to track a real live location */
  58. TRACKER_UNKNOWN = 0x80, /* unknown tracker */
  59. TRACKER_ANY_KNOWN = 0x7f, /* all except unknown */
  60. TRACKER_ANY = 0xff /* used by get_connected_trackers to return all types */
  61. };
  62. enum RotationMode {
  63. RESET_FULL_ROTATION = 0, /* we reset the full rotation, regardless of how the HMD is oriented, we're looking dead ahead */
  64. RESET_BUT_KEEP_TILT = 1, /* reset rotation but keep tilt. */
  65. DONT_RESET_ROTATION = 2, /* don't reset the rotation, we will only center on position */
  66. };
  67. private:
  68. Vector<Ref<ARVRInterface> > interfaces;
  69. Vector<ARVRPositionalTracker *> trackers;
  70. Ref<ARVRInterface> primary_interface; /* we'll identify one interface as primary, this will be used by our viewports */
  71. real_t world_scale; /* scale by which we multiply our tracker positions */
  72. Transform world_origin; /* our world origin point, maps a location in our virtual world to the origin point in our real world tracking volume */
  73. Transform reference_frame; /* our reference frame */
  74. uint64_t last_process_usec; /* for frame timing, usec when we did our processing */
  75. uint64_t last_commit_usec; /* for frame timing, usec when we finished committing both eyes */
  76. uint64_t last_frame_usec; /* time it took between process and committing, we should probably average this over the last x frames */
  77. protected:
  78. static ARVRServer *singleton;
  79. static void _bind_methods();
  80. public:
  81. static ARVRServer *get_singleton();
  82. /*
  83. World scale allows you to specify a scale factor that is applied to all positioning vectors in our VR world in essence scaling up, or scaling down the world.
  84. For stereoscopic rendering specifically this is very important to give an accurate sense of scale.
  85. Add controllers into the mix and an accurate mapping of real world movement to perceived virtual movement becomes very important.
  86. Most VR platforms, and our assumption, is that 1 unit in our virtual world equates to 1 meter in the real mode.
  87. This scale basically effects the unit size relationship to real world size.
  88. I may remove access to this property in GDScript in favour of exposing it on the ARVROrigin node
  89. */
  90. real_t get_world_scale() const;
  91. void set_world_scale(real_t p_world_scale);
  92. /*
  93. The world maps the 0,0,0 coordinate of our real world coordinate system for our tracking volume to a location in our
  94. virtual world. It is this origin point that should be moved when the player is moved through the world by controller
  95. actions be it straffing, teleporting, etc. Movement of the player by moving through the physical space is always tracked
  96. in relation to this point.
  97. Note that the ARVROrigin spatial node in your scene automatically updates this property and it should be used instead of
  98. direct access to this property and it therefore is not available in GDScript
  99. Note: this should not be used in AR and should be ignored by an AR based interface as it would throw what you're looking at in the real world
  100. and in the virtual world out of sync
  101. */
  102. Transform get_world_origin() const;
  103. void set_world_origin(const Transform &p_world_origin);
  104. /*
  105. center_on_hmd calculates a new reference frame. This ensures the HMD is positioned to 0,0,0 facing 0,0,-1 (need to verify this direction)
  106. in the virtual world.
  107. You can ignore the tilt of the device ensuring you're looking straight forward even if the player is looking down or sideways.
  108. You can chose to keep the height the tracking provides which is important for room scale capable tracking.
  109. Note: this should not be used in AR and should be ignored by an AR based interface as it would throw what you're looking at in the real world
  110. and in the virtual world out of sync
  111. */
  112. Transform get_reference_frame() const;
  113. void center_on_hmd(RotationMode p_rotation_mode, bool p_keep_height);
  114. /*
  115. get_hmd_transform gets our hmd transform (centered between eyes) with most up to date tracking, relative to the origin
  116. */
  117. Transform get_hmd_transform();
  118. /*
  119. Interfaces are objects that 'glue' Godot to an AR or VR SDK such as the Oculus SDK, OpenVR, OpenHMD, etc.
  120. */
  121. void add_interface(const Ref<ARVRInterface> &p_interface);
  122. void remove_interface(const Ref<ARVRInterface> &p_interface);
  123. int get_interface_count() const;
  124. Ref<ARVRInterface> get_interface(int p_index) const;
  125. Ref<ARVRInterface> find_interface(const String &p_name) const;
  126. Array get_interfaces() const;
  127. /*
  128. note, more then one interface can technically be active, especially on mobile, but only one interface is used for
  129. rendering. This interface identifies itself by calling set_primary_interface when it is initialized
  130. */
  131. Ref<ARVRInterface> get_primary_interface() const;
  132. void set_primary_interface(const Ref<ARVRInterface> &p_primary_interface);
  133. void clear_primary_interface_if(const Ref<ARVRInterface> &p_primary_interface); /* this is automatically called if an interface destructs */
  134. /*
  135. Our trackers are objects that expose the orientation and position of physical devices such as controller, anchor points, etc.
  136. They are created and managed by our active AR/VR interfaces.
  137. */
  138. bool is_tracker_id_in_use_for_type(TrackerType p_tracker_type, int p_tracker_id) const;
  139. int get_free_tracker_id_for_type(TrackerType p_tracker_type);
  140. void add_tracker(ARVRPositionalTracker *p_tracker);
  141. void remove_tracker(ARVRPositionalTracker *p_tracker);
  142. int get_tracker_count() const;
  143. ARVRPositionalTracker *get_tracker(int p_index) const;
  144. ARVRPositionalTracker *find_by_type_and_id(TrackerType p_tracker_type, int p_tracker_id) const;
  145. uint64_t get_last_process_usec();
  146. uint64_t get_last_commit_usec();
  147. uint64_t get_last_frame_usec();
  148. void _process();
  149. void _mark_commit();
  150. ARVRServer();
  151. ~ARVRServer();
  152. };
  153. #define ARVR ARVRServer
  154. VARIANT_ENUM_CAST(ARVRServer::TrackerType);
  155. VARIANT_ENUM_CAST(ARVRServer::RotationMode);
  156. #endif