xr_interface_gdnative.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*************************************************************************/
  2. /* xr_interface_gdnative.cpp */
  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. #include "xr_interface_gdnative.h"
  31. #include "core/input/input.h"
  32. #include "servers/rendering/rendering_server_globals.h"
  33. #include "servers/xr/xr_positional_tracker.h"
  34. void XRInterfaceGDNative::_bind_methods() {
  35. ADD_PROPERTY_DEFAULT("interface_is_initialized", false);
  36. ADD_PROPERTY_DEFAULT("ar_is_anchor_detection_enabled", false);
  37. }
  38. XRInterfaceGDNative::XRInterfaceGDNative() {
  39. print_verbose("Construct gdnative interface\n");
  40. // we won't have our data pointer until our library gets set
  41. data = nullptr;
  42. interface = nullptr;
  43. }
  44. XRInterfaceGDNative::~XRInterfaceGDNative() {
  45. print_verbose("Destruct gdnative interface\n");
  46. if (interface != nullptr && is_initialized()) {
  47. uninitialize();
  48. };
  49. // cleanup after ourselves
  50. cleanup();
  51. }
  52. void XRInterfaceGDNative::cleanup() {
  53. if (interface != nullptr) {
  54. interface->destructor(data);
  55. data = nullptr;
  56. interface = nullptr;
  57. }
  58. }
  59. void XRInterfaceGDNative::set_interface(const godot_xr_interface_gdnative *p_interface) {
  60. // this should only be called once, just being paranoid..
  61. if (interface) {
  62. cleanup();
  63. }
  64. // bind to our interface
  65. interface = p_interface;
  66. // Now we do our constructing...
  67. data = interface->constructor((godot_object *)this);
  68. }
  69. StringName XRInterfaceGDNative::get_name() const {
  70. ERR_FAIL_COND_V(interface == nullptr, StringName());
  71. godot_string result = interface->get_name(data);
  72. StringName name = *(String *)&result;
  73. godot_string_destroy(&result);
  74. return name;
  75. }
  76. int XRInterfaceGDNative::get_capabilities() const {
  77. int capabilities;
  78. ERR_FAIL_COND_V(interface == nullptr, 0); // 0 = None
  79. capabilities = interface->get_capabilities(data);
  80. return capabilities;
  81. }
  82. bool XRInterfaceGDNative::get_anchor_detection_is_enabled() const {
  83. ERR_FAIL_COND_V(interface == nullptr, false);
  84. return interface->get_anchor_detection_is_enabled(data);
  85. }
  86. void XRInterfaceGDNative::set_anchor_detection_is_enabled(bool p_enable) {
  87. ERR_FAIL_COND(interface == nullptr);
  88. interface->set_anchor_detection_is_enabled(data, p_enable);
  89. }
  90. int XRInterfaceGDNative::get_camera_feed_id() {
  91. ERR_FAIL_COND_V(interface == nullptr, 0);
  92. return (unsigned int)interface->get_camera_feed_id(data);
  93. }
  94. bool XRInterfaceGDNative::is_stereo() {
  95. bool stereo;
  96. ERR_FAIL_COND_V(interface == nullptr, false);
  97. stereo = interface->is_stereo(data);
  98. return stereo;
  99. }
  100. bool XRInterfaceGDNative::is_initialized() const {
  101. ERR_FAIL_COND_V(interface == nullptr, false);
  102. return interface->is_initialized(data);
  103. }
  104. bool XRInterfaceGDNative::initialize() {
  105. ERR_FAIL_COND_V(interface == nullptr, false);
  106. bool initialized = interface->initialize(data);
  107. if (initialized) {
  108. // if we successfully initialize our interface and we don't have a primary interface yet, this becomes our primary interface
  109. XRServer *xr_server = XRServer::get_singleton();
  110. if ((xr_server != nullptr) && (xr_server->get_primary_interface() == nullptr)) {
  111. xr_server->set_primary_interface(this);
  112. };
  113. };
  114. return initialized;
  115. }
  116. void XRInterfaceGDNative::uninitialize() {
  117. ERR_FAIL_COND(interface == nullptr);
  118. XRServer *xr_server = XRServer::get_singleton();
  119. if (xr_server != nullptr) {
  120. // Whatever happens, make sure this is no longer our primary interface
  121. xr_server->clear_primary_interface_if(this);
  122. }
  123. interface->uninitialize(data);
  124. }
  125. Size2 XRInterfaceGDNative::get_render_targetsize() {
  126. ERR_FAIL_COND_V(interface == nullptr, Size2());
  127. godot_vector2 result = interface->get_render_targetsize(data);
  128. Vector2 *vec = (Vector2 *)&result;
  129. return *vec;
  130. }
  131. Transform XRInterfaceGDNative::get_transform_for_eye(XRInterface::Eyes p_eye, const Transform &p_cam_transform) {
  132. Transform *ret;
  133. ERR_FAIL_COND_V(interface == nullptr, Transform());
  134. godot_transform t = interface->get_transform_for_eye(data, (int)p_eye, (godot_transform *)&p_cam_transform);
  135. ret = (Transform *)&t;
  136. return *ret;
  137. }
  138. CameraMatrix XRInterfaceGDNative::get_projection_for_eye(XRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
  139. CameraMatrix cm;
  140. ERR_FAIL_COND_V(interface == nullptr, CameraMatrix());
  141. interface->fill_projection_for_eye(data, (godot_real *)cm.matrix, (godot_int)p_eye, p_aspect, p_z_near, p_z_far);
  142. return cm;
  143. }
  144. unsigned int XRInterfaceGDNative::get_external_texture_for_eye(XRInterface::Eyes p_eye) {
  145. ERR_FAIL_COND_V(interface == nullptr, 0);
  146. return (unsigned int)interface->get_external_texture_for_eye(data, (godot_int)p_eye);
  147. }
  148. void XRInterfaceGDNative::commit_for_eye(XRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
  149. ERR_FAIL_COND(interface == nullptr);
  150. interface->commit_for_eye(data, (godot_int)p_eye, (godot_rid *)&p_render_target, (godot_rect2 *)&p_screen_rect);
  151. }
  152. void XRInterfaceGDNative::process() {
  153. ERR_FAIL_COND(interface == nullptr);
  154. interface->process(data);
  155. }
  156. void XRInterfaceGDNative::notification(int p_what) {
  157. ERR_FAIL_COND(interface == nullptr);
  158. interface->notification(data, p_what);
  159. }
  160. /////////////////////////////////////////////////////////////////////////////////////
  161. // some helper callbacks
  162. extern "C" {
  163. void GDAPI godot_xr_register_interface(const godot_xr_interface_gdnative *p_interface) {
  164. // Must be on a version 4 plugin
  165. ERR_FAIL_COND_MSG(p_interface->version.major < 4, "GDNative XR interfaces build for Godot 3.x are not supported.");
  166. Ref<XRInterfaceGDNative> new_interface;
  167. new_interface.instance();
  168. new_interface->set_interface((const godot_xr_interface_gdnative *)p_interface);
  169. XRServer::get_singleton()->add_interface(new_interface);
  170. }
  171. godot_real GDAPI godot_xr_get_worldscale() {
  172. XRServer *xr_server = XRServer::get_singleton();
  173. ERR_FAIL_NULL_V(xr_server, 1.0);
  174. return xr_server->get_world_scale();
  175. }
  176. godot_transform GDAPI godot_xr_get_reference_frame() {
  177. godot_transform reference_frame;
  178. Transform *reference_frame_ptr = (Transform *)&reference_frame;
  179. XRServer *xr_server = XRServer::get_singleton();
  180. if (xr_server != nullptr) {
  181. *reference_frame_ptr = xr_server->get_reference_frame();
  182. } else {
  183. godot_transform_new_identity(&reference_frame);
  184. }
  185. return reference_frame;
  186. }
  187. void GDAPI godot_xr_blit(godot_int p_eye, godot_rid *p_render_target, godot_rect2 *p_rect) {
  188. // blits out our texture as is, handy for preview display of one of the eyes that is already rendered with lens distortion on an external HMD
  189. XRInterface::Eyes eye = (XRInterface::Eyes)p_eye;
  190. #if 0
  191. RID *render_target = (RID *)p_render_target;
  192. #endif
  193. Rect2 screen_rect = *(Rect2 *)p_rect;
  194. if (eye == XRInterface::EYE_LEFT) {
  195. screen_rect.size.x /= 2.0;
  196. } else if (p_eye == XRInterface::EYE_RIGHT) {
  197. screen_rect.size.x /= 2.0;
  198. screen_rect.position.x += screen_rect.size.x;
  199. }
  200. #ifndef _MSC_VER
  201. #warning this needs to be redone
  202. #endif
  203. #if 0
  204. RSG::rasterizer->blit_render_target_to_screen(*render_target, screen_rect, 0);
  205. #endif
  206. }
  207. godot_int GDAPI godot_xr_get_texid(godot_rid *p_render_target) {
  208. // In order to send off our textures to display on our hardware we need the opengl texture ID instead of the render target RID
  209. // This is a handy function to expose that.
  210. #if 0
  211. RID *render_target = (RID *)p_render_target;
  212. RID eye_texture = RSG::storage->render_target_get_texture(*render_target);
  213. #endif
  214. #ifndef _MSC_VER
  215. #warning need to obtain this ID again
  216. #endif
  217. uint32_t texid = 0; //RS::get_singleton()->texture_get_texid(eye_texture);
  218. return texid;
  219. }
  220. godot_int GDAPI godot_xr_add_controller(char *p_device_name, godot_int p_hand, godot_bool p_tracks_orientation, godot_bool p_tracks_position) {
  221. XRServer *xr_server = XRServer::get_singleton();
  222. ERR_FAIL_NULL_V(xr_server, 0);
  223. Input *input = Input::get_singleton();
  224. ERR_FAIL_NULL_V(input, 0);
  225. XRPositionalTracker *new_tracker = memnew(XRPositionalTracker);
  226. new_tracker->set_name(p_device_name);
  227. new_tracker->set_type(XRServer::TRACKER_CONTROLLER);
  228. if (p_hand == 1) {
  229. new_tracker->set_hand(XRPositionalTracker::TRACKER_LEFT_HAND);
  230. } else if (p_hand == 2) {
  231. new_tracker->set_hand(XRPositionalTracker::TRACKER_RIGHT_HAND);
  232. }
  233. // also register as joystick...
  234. int joyid = input->get_unused_joy_id();
  235. if (joyid != -1) {
  236. new_tracker->set_joy_id(joyid);
  237. input->joy_connection_changed(joyid, true, p_device_name, "");
  238. }
  239. if (p_tracks_orientation) {
  240. Basis orientation;
  241. new_tracker->set_orientation(orientation);
  242. }
  243. if (p_tracks_position) {
  244. Vector3 position;
  245. new_tracker->set_position(position);
  246. }
  247. // add our tracker to our server and remember its pointer
  248. xr_server->add_tracker(new_tracker);
  249. // note, this ID is only unique within controllers!
  250. return new_tracker->get_tracker_id();
  251. }
  252. void GDAPI godot_xr_remove_controller(godot_int p_controller_id) {
  253. XRServer *xr_server = XRServer::get_singleton();
  254. ERR_FAIL_NULL(xr_server);
  255. Input *input = Input::get_singleton();
  256. ERR_FAIL_NULL(input);
  257. XRPositionalTracker *remove_tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, p_controller_id);
  258. if (remove_tracker != nullptr) {
  259. // unset our joystick if applicable
  260. int joyid = remove_tracker->get_joy_id();
  261. if (joyid != -1) {
  262. input->joy_connection_changed(joyid, false, "", "");
  263. remove_tracker->set_joy_id(-1);
  264. }
  265. // remove our tracker from our server
  266. xr_server->remove_tracker(remove_tracker);
  267. memdelete(remove_tracker);
  268. }
  269. }
  270. void GDAPI godot_xr_set_controller_transform(godot_int p_controller_id, godot_transform *p_transform, godot_bool p_tracks_orientation, godot_bool p_tracks_position) {
  271. XRServer *xr_server = XRServer::get_singleton();
  272. ERR_FAIL_NULL(xr_server);
  273. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, p_controller_id);
  274. if (tracker != nullptr) {
  275. Transform *transform = (Transform *)p_transform;
  276. if (p_tracks_orientation) {
  277. tracker->set_orientation(transform->basis);
  278. }
  279. if (p_tracks_position) {
  280. tracker->set_rw_position(transform->origin);
  281. }
  282. }
  283. }
  284. void GDAPI godot_xr_set_controller_button(godot_int p_controller_id, godot_int p_button, godot_bool p_is_pressed) {
  285. XRServer *xr_server = XRServer::get_singleton();
  286. ERR_FAIL_NULL(xr_server);
  287. Input *input = Input::get_singleton();
  288. ERR_FAIL_NULL(input);
  289. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, p_controller_id);
  290. if (tracker != nullptr) {
  291. int joyid = tracker->get_joy_id();
  292. if (joyid != -1) {
  293. input->joy_button(joyid, p_button, p_is_pressed);
  294. }
  295. }
  296. }
  297. void GDAPI godot_xr_set_controller_axis(godot_int p_controller_id, godot_int p_axis, godot_real p_value, godot_bool p_can_be_negative) {
  298. XRServer *xr_server = XRServer::get_singleton();
  299. ERR_FAIL_NULL(xr_server);
  300. Input *input = Input::get_singleton();
  301. ERR_FAIL_NULL(input);
  302. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, p_controller_id);
  303. if (tracker != nullptr) {
  304. int joyid = tracker->get_joy_id();
  305. if (joyid != -1) {
  306. Input::JoyAxis jx;
  307. jx.min = p_can_be_negative ? -1 : 0;
  308. jx.value = p_value;
  309. input->joy_axis(joyid, p_axis, jx);
  310. }
  311. }
  312. }
  313. godot_real GDAPI godot_xr_get_controller_rumble(godot_int p_controller_id) {
  314. XRServer *xr_server = XRServer::get_singleton();
  315. ERR_FAIL_NULL_V(xr_server, 0.0);
  316. XRPositionalTracker *tracker = xr_server->find_by_type_and_id(XRServer::TRACKER_CONTROLLER, p_controller_id);
  317. if (tracker != nullptr) {
  318. return tracker->get_rumble();
  319. }
  320. return 0.0;
  321. }
  322. }