arvr_interface_gdnative.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**************************************************************************/
  2. /* arvr_interface_gdnative.cpp */
  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. #include "arvr_interface_gdnative.h"
  31. #include "main/input_default.h"
  32. #include "servers/arvr/arvr_positional_tracker.h"
  33. #include "servers/visual/visual_server_globals.h"
  34. void ARVRInterfaceGDNative::_bind_methods() {
  35. ADD_PROPERTY_DEFAULT("interface_is_initialized", false);
  36. ADD_PROPERTY_DEFAULT("ar_is_anchor_detection_enabled", false);
  37. }
  38. ARVRInterfaceGDNative::ARVRInterfaceGDNative() {
  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. ARVRInterfaceGDNative::~ARVRInterfaceGDNative() {
  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 ARVRInterfaceGDNative::cleanup() {
  53. if (interface != nullptr) {
  54. interface->destructor(data);
  55. data = nullptr;
  56. interface = nullptr;
  57. }
  58. }
  59. void ARVRInterfaceGDNative::set_interface(const godot_arvr_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 ARVRInterfaceGDNative::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 ARVRInterfaceGDNative::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 ARVRInterfaceGDNative::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 ARVRInterfaceGDNative::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 ARVRInterfaceGDNative::get_camera_feed_id() {
  91. ERR_FAIL_COND_V(interface == nullptr, 0);
  92. if ((interface->version.major > 1) || ((interface->version.major) == 1 && (interface->version.minor >= 1))) {
  93. return (unsigned int)interface->get_camera_feed_id(data);
  94. } else {
  95. return 0;
  96. }
  97. }
  98. bool ARVRInterfaceGDNative::is_stereo() {
  99. bool stereo;
  100. ERR_FAIL_COND_V(interface == nullptr, false);
  101. stereo = interface->is_stereo(data);
  102. return stereo;
  103. }
  104. bool ARVRInterfaceGDNative::is_initialized() const {
  105. ERR_FAIL_COND_V(interface == nullptr, false);
  106. return interface->is_initialized(data);
  107. }
  108. bool ARVRInterfaceGDNative::initialize() {
  109. ERR_FAIL_COND_V(interface == nullptr, false);
  110. bool initialized = interface->initialize(data);
  111. if (initialized) {
  112. // if we successfully initialize our interface and we don't have a primary interface yet, this becomes our primary interface
  113. ARVRServer *arvr_server = ARVRServer::get_singleton();
  114. if ((arvr_server != nullptr) && (arvr_server->get_primary_interface() == nullptr)) {
  115. arvr_server->set_primary_interface(this);
  116. };
  117. };
  118. return initialized;
  119. }
  120. void ARVRInterfaceGDNative::uninitialize() {
  121. ERR_FAIL_COND(interface == nullptr);
  122. ARVRServer *arvr_server = ARVRServer::get_singleton();
  123. if (arvr_server != nullptr) {
  124. // Whatever happens, make sure this is no longer our primary interface
  125. arvr_server->clear_primary_interface_if(this);
  126. }
  127. interface->uninitialize(data);
  128. }
  129. Size2 ARVRInterfaceGDNative::get_render_targetsize() {
  130. ERR_FAIL_COND_V(interface == nullptr, Size2());
  131. godot_vector2 result = interface->get_render_targetsize(data);
  132. Vector2 *vec = (Vector2 *)&result;
  133. return *vec;
  134. }
  135. Transform ARVRInterfaceGDNative::get_transform_for_eye(ARVRInterface::Eyes p_eye, const Transform &p_cam_transform) {
  136. Transform *ret;
  137. ERR_FAIL_COND_V(interface == nullptr, Transform());
  138. godot_transform t = interface->get_transform_for_eye(data, (int)p_eye, (godot_transform *)&p_cam_transform);
  139. ret = (Transform *)&t;
  140. return *ret;
  141. }
  142. CameraMatrix ARVRInterfaceGDNative::get_projection_for_eye(ARVRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
  143. CameraMatrix cm;
  144. ERR_FAIL_COND_V(interface == nullptr, CameraMatrix());
  145. interface->fill_projection_for_eye(data, (godot_real *)cm.matrix, (godot_int)p_eye, p_aspect, p_z_near, p_z_far);
  146. return cm;
  147. }
  148. unsigned int ARVRInterfaceGDNative::get_external_texture_for_eye(ARVRInterface::Eyes p_eye) {
  149. ERR_FAIL_COND_V(interface == nullptr, 0);
  150. if ((interface->version.major > 1) || ((interface->version.major) == 1 && (interface->version.minor >= 1))) {
  151. return (unsigned int)interface->get_external_texture_for_eye(data, (godot_int)p_eye);
  152. } else {
  153. return 0;
  154. }
  155. }
  156. unsigned int ARVRInterfaceGDNative::get_external_depth_for_eye(ARVRInterface::Eyes p_eye) {
  157. ERR_FAIL_COND_V(interface == nullptr, 0);
  158. if ((interface->version.major > 1) || ((interface->version.major) == 1 && (interface->version.minor >= 2))) {
  159. return (unsigned int)interface->get_external_depth_for_eye(data, (godot_int)p_eye);
  160. } else {
  161. return 0;
  162. }
  163. }
  164. void ARVRInterfaceGDNative::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
  165. ERR_FAIL_COND(interface == nullptr);
  166. interface->commit_for_eye(data, (godot_int)p_eye, (godot_rid *)&p_render_target, (godot_rect2 *)&p_screen_rect);
  167. }
  168. void ARVRInterfaceGDNative::process() {
  169. ERR_FAIL_COND(interface == nullptr);
  170. interface->process(data);
  171. }
  172. void ARVRInterfaceGDNative::notification(int p_what) {
  173. ERR_FAIL_COND(interface == nullptr);
  174. // this is only available in interfaces that implement 1.1 or later
  175. if ((interface->version.major > 1) || ((interface->version.major == 1) && (interface->version.minor > 0))) {
  176. interface->notification(data, p_what);
  177. }
  178. }
  179. /////////////////////////////////////////////////////////////////////////////////////
  180. // some helper callbacks
  181. extern "C" {
  182. void GDAPI godot_arvr_register_interface(const godot_arvr_interface_gdnative *p_interface) {
  183. // If our major version is 0 or bigger then 10, we're likely looking at our constructor pointer from an older plugin
  184. ERR_FAIL_COND_MSG((p_interface->version.major == 0) || (p_interface->version.major > 10), "GDNative ARVR interfaces build for Godot 3.0 are not supported.");
  185. Ref<ARVRInterfaceGDNative> new_interface;
  186. new_interface.instance();
  187. new_interface->set_interface((const godot_arvr_interface_gdnative *)p_interface);
  188. ARVRServer::get_singleton()->add_interface(new_interface);
  189. }
  190. godot_real GDAPI godot_arvr_get_worldscale() {
  191. ARVRServer *arvr_server = ARVRServer::get_singleton();
  192. ERR_FAIL_NULL_V(arvr_server, 1.0);
  193. return arvr_server->get_world_scale();
  194. }
  195. godot_transform GDAPI godot_arvr_get_reference_frame() {
  196. godot_transform reference_frame;
  197. Transform *reference_frame_ptr = (Transform *)&reference_frame;
  198. ARVRServer *arvr_server = ARVRServer::get_singleton();
  199. if (arvr_server != nullptr) {
  200. *reference_frame_ptr = arvr_server->get_reference_frame();
  201. } else {
  202. godot_transform_new_identity(&reference_frame);
  203. }
  204. return reference_frame;
  205. }
  206. void GDAPI godot_arvr_blit(godot_int p_eye, godot_rid *p_render_target, godot_rect2 *p_rect) {
  207. // 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
  208. ARVRInterface::Eyes eye = (ARVRInterface::Eyes)p_eye;
  209. RID *render_target = (RID *)p_render_target;
  210. Rect2 screen_rect = *(Rect2 *)p_rect;
  211. if (eye == ARVRInterface::EYE_LEFT) {
  212. screen_rect.size.x /= 2.0;
  213. } else if (p_eye == ARVRInterface::EYE_RIGHT) {
  214. screen_rect.size.x /= 2.0;
  215. screen_rect.position.x += screen_rect.size.x;
  216. }
  217. VSG::rasterizer->set_current_render_target(RID());
  218. VSG::rasterizer->blit_render_target_to_screen(*render_target, screen_rect, 0);
  219. }
  220. godot_int GDAPI godot_arvr_get_texid(godot_rid *p_render_target) {
  221. // In order to send off our textures to display on our hardware we need the opengl texture ID instead of the render target RID
  222. // This is a handy function to expose that.
  223. RID *render_target = (RID *)p_render_target;
  224. RID eye_texture = VSG::storage->render_target_get_texture(*render_target);
  225. uint32_t texid = VS::get_singleton()->texture_get_texid(eye_texture);
  226. return texid;
  227. }
  228. godot_int GDAPI godot_arvr_add_controller(char *p_device_name, godot_int p_hand, godot_bool p_tracks_orientation, godot_bool p_tracks_position) {
  229. ARVRServer *arvr_server = ARVRServer::get_singleton();
  230. ERR_FAIL_NULL_V(arvr_server, 0);
  231. InputDefault *input = (InputDefault *)Input::get_singleton();
  232. ERR_FAIL_NULL_V(input, 0);
  233. Ref<ARVRPositionalTracker> new_tracker;
  234. new_tracker.instance();
  235. new_tracker->set_name(p_device_name);
  236. new_tracker->set_type(ARVRServer::TRACKER_CONTROLLER);
  237. if (p_hand == 1) {
  238. new_tracker->set_hand(ARVRPositionalTracker::TRACKER_LEFT_HAND);
  239. } else if (p_hand == 2) {
  240. new_tracker->set_hand(ARVRPositionalTracker::TRACKER_RIGHT_HAND);
  241. }
  242. // also register as joystick...
  243. int joyid = input->get_unused_joy_id();
  244. if (joyid != -1) {
  245. new_tracker->set_joy_id(joyid);
  246. input->joy_connection_changed(joyid, true, p_device_name, "");
  247. }
  248. if (p_tracks_orientation) {
  249. Basis orientation;
  250. new_tracker->set_orientation(orientation);
  251. }
  252. if (p_tracks_position) {
  253. Vector3 position;
  254. new_tracker->set_position(position);
  255. }
  256. // add our tracker to our server and remember its pointer
  257. arvr_server->add_tracker(new_tracker);
  258. // note, this ID is only unique within controllers!
  259. return new_tracker->get_tracker_id();
  260. }
  261. void GDAPI godot_arvr_remove_controller(godot_int p_controller_id) {
  262. ARVRServer *arvr_server = ARVRServer::get_singleton();
  263. ERR_FAIL_NULL(arvr_server);
  264. InputDefault *input = (InputDefault *)Input::get_singleton();
  265. ERR_FAIL_NULL(input);
  266. Ref<ARVRPositionalTracker> remove_tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  267. if (remove_tracker.is_valid()) {
  268. // unset our joystick if applicable
  269. int joyid = remove_tracker->get_joy_id();
  270. if (joyid != -1) {
  271. input->joy_connection_changed(joyid, false, "", "");
  272. remove_tracker->set_joy_id(-1);
  273. }
  274. // remove our tracker from our server
  275. arvr_server->remove_tracker(remove_tracker);
  276. remove_tracker.unref();
  277. }
  278. }
  279. void GDAPI godot_arvr_set_controller_transform(godot_int p_controller_id, godot_transform *p_transform, godot_bool p_tracks_orientation, godot_bool p_tracks_position) {
  280. ARVRServer *arvr_server = ARVRServer::get_singleton();
  281. ERR_FAIL_NULL(arvr_server);
  282. Ref<ARVRPositionalTracker> tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  283. if (tracker.is_valid()) {
  284. Transform *transform = (Transform *)p_transform;
  285. if (p_tracks_orientation) {
  286. tracker->set_orientation(transform->basis);
  287. }
  288. if (p_tracks_position) {
  289. tracker->set_rw_position(transform->origin);
  290. }
  291. }
  292. }
  293. void GDAPI godot_arvr_set_controller_button(godot_int p_controller_id, godot_int p_button, godot_bool p_is_pressed) {
  294. ARVRServer *arvr_server = ARVRServer::get_singleton();
  295. ERR_FAIL_NULL(arvr_server);
  296. InputDefault *input = (InputDefault *)Input::get_singleton();
  297. ERR_FAIL_NULL(input);
  298. Ref<ARVRPositionalTracker> tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  299. if (tracker.is_valid()) {
  300. int joyid = tracker->get_joy_id();
  301. if (joyid != -1) {
  302. input->joy_button(joyid, p_button, p_is_pressed);
  303. }
  304. }
  305. }
  306. void GDAPI godot_arvr_set_controller_axis(godot_int p_controller_id, godot_int p_axis, godot_real p_value, godot_bool p_can_be_negative) {
  307. ARVRServer *arvr_server = ARVRServer::get_singleton();
  308. ERR_FAIL_NULL(arvr_server);
  309. InputDefault *input = (InputDefault *)Input::get_singleton();
  310. ERR_FAIL_NULL(input);
  311. Ref<ARVRPositionalTracker> tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  312. if (tracker.is_valid()) {
  313. int joyid = tracker->get_joy_id();
  314. if (joyid != -1) {
  315. float value = p_value;
  316. if (!p_can_be_negative) {
  317. // Convert to a value between -1.0f and 1.0f.
  318. value = p_value * 2.0f - 1.0f;
  319. }
  320. input->joy_axis(joyid, p_axis, value);
  321. }
  322. }
  323. }
  324. godot_real GDAPI godot_arvr_get_controller_rumble(godot_int p_controller_id) {
  325. ARVRServer *arvr_server = ARVRServer::get_singleton();
  326. ERR_FAIL_NULL_V(arvr_server, 0.0);
  327. Ref<ARVRPositionalTracker> tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  328. if (tracker.is_valid()) {
  329. return tracker->get_rumble();
  330. }
  331. return 0.0;
  332. }
  333. void GDAPI godot_arvr_set_interface(godot_object *p_arvr_interface, const godot_arvr_interface_gdnative *p_gdn_interface) {
  334. // If our major version is 0 or bigger then 10, we're likely looking at our constructor pointer from an older plugin
  335. ERR_FAIL_COND_MSG((p_gdn_interface->version.major == 0) || (p_gdn_interface->version.major > 10), "GDNative ARVR interfaces build for Godot 3.0 are not supported.");
  336. ARVRInterfaceGDNative *interface = (ARVRInterfaceGDNative *)p_arvr_interface;
  337. interface->set_interface((const godot_arvr_interface_gdnative *)p_gdn_interface);
  338. }
  339. godot_int GDAPI godot_arvr_get_depthid(godot_rid *p_render_target) {
  340. // We also need to access our depth texture for reprojection.
  341. RID *render_target = (RID *)p_render_target;
  342. uint32_t texid = VSG::storage->render_target_get_depth_texture_id(*render_target);
  343. return texid;
  344. }
  345. }