arvr_interface_gdnative.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*************************************************************************/
  2. /* arvr_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 "arvr_interface_gdnative.h"
  31. #include "core/input/input_filter.h"
  32. #include "servers/arvr/arvr_positional_tracker.h"
  33. #include "servers/rendering/rendering_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 = NULL;
  42. interface = NULL;
  43. }
  44. ARVRInterfaceGDNative::~ARVRInterfaceGDNative() {
  45. print_verbose("Destruct gdnative interface\n");
  46. if (interface != NULL && is_initialized()) {
  47. uninitialize();
  48. };
  49. // cleanup after ourselves
  50. cleanup();
  51. }
  52. void ARVRInterfaceGDNative::cleanup() {
  53. if (interface != NULL) {
  54. interface->destructor(data);
  55. data = NULL;
  56. interface = NULL;
  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 == NULL, 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 == NULL, 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 == NULL, 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 == NULL);
  88. interface->set_anchor_detection_is_enabled(data, p_enable);
  89. }
  90. int ARVRInterfaceGDNative::get_camera_feed_id() {
  91. ERR_FAIL_COND_V(interface == NULL, 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 == NULL, false);
  101. stereo = interface->is_stereo(data);
  102. return stereo;
  103. }
  104. bool ARVRInterfaceGDNative::is_initialized() const {
  105. ERR_FAIL_COND_V(interface == NULL, false);
  106. return interface->is_initialized(data);
  107. }
  108. bool ARVRInterfaceGDNative::initialize() {
  109. ERR_FAIL_COND_V(interface == NULL, 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 != NULL) && (arvr_server->get_primary_interface() == NULL)) {
  115. arvr_server->set_primary_interface(this);
  116. };
  117. };
  118. return initialized;
  119. }
  120. void ARVRInterfaceGDNative::uninitialize() {
  121. ERR_FAIL_COND(interface == NULL);
  122. ARVRServer *arvr_server = ARVRServer::get_singleton();
  123. if (arvr_server != NULL) {
  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 == NULL, 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 == NULL, 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 == NULL, 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 == NULL, 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. void ARVRInterfaceGDNative::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
  157. ERR_FAIL_COND(interface == NULL);
  158. interface->commit_for_eye(data, (godot_int)p_eye, (godot_rid *)&p_render_target, (godot_rect2 *)&p_screen_rect);
  159. }
  160. void ARVRInterfaceGDNative::process() {
  161. ERR_FAIL_COND(interface == NULL);
  162. interface->process(data);
  163. }
  164. void ARVRInterfaceGDNative::notification(int p_what) {
  165. ERR_FAIL_COND(interface == NULL);
  166. // this is only available in interfaces that implement 1.1 or later
  167. if ((interface->version.major > 1) || ((interface->version.major == 1) && (interface->version.minor > 0))) {
  168. interface->notification(data, p_what);
  169. }
  170. }
  171. /////////////////////////////////////////////////////////////////////////////////////
  172. // some helper callbacks
  173. extern "C" {
  174. void GDAPI godot_arvr_register_interface(const godot_arvr_interface_gdnative *p_interface) {
  175. // If our major version is 0 or bigger then 10, we're likely looking at our constructor pointer from an older plugin
  176. 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.");
  177. Ref<ARVRInterfaceGDNative> new_interface;
  178. new_interface.instance();
  179. new_interface->set_interface((const godot_arvr_interface_gdnative *)p_interface);
  180. ARVRServer::get_singleton()->add_interface(new_interface);
  181. }
  182. godot_real GDAPI godot_arvr_get_worldscale() {
  183. ARVRServer *arvr_server = ARVRServer::get_singleton();
  184. ERR_FAIL_NULL_V(arvr_server, 1.0);
  185. return arvr_server->get_world_scale();
  186. }
  187. godot_transform GDAPI godot_arvr_get_reference_frame() {
  188. godot_transform reference_frame;
  189. Transform *reference_frame_ptr = (Transform *)&reference_frame;
  190. ARVRServer *arvr_server = ARVRServer::get_singleton();
  191. if (arvr_server != NULL) {
  192. *reference_frame_ptr = arvr_server->get_reference_frame();
  193. } else {
  194. godot_transform_new_identity(&reference_frame);
  195. }
  196. return reference_frame;
  197. }
  198. void GDAPI godot_arvr_blit(godot_int p_eye, godot_rid *p_render_target, godot_rect2 *p_rect) {
  199. // 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
  200. ARVRInterface::Eyes eye = (ARVRInterface::Eyes)p_eye;
  201. #if 0
  202. RID *render_target = (RID *)p_render_target;
  203. #endif
  204. Rect2 screen_rect = *(Rect2 *)p_rect;
  205. if (eye == ARVRInterface::EYE_LEFT) {
  206. screen_rect.size.x /= 2.0;
  207. } else if (p_eye == ARVRInterface::EYE_RIGHT) {
  208. screen_rect.size.x /= 2.0;
  209. screen_rect.position.x += screen_rect.size.x;
  210. }
  211. #ifndef _MSC_VER
  212. #warning this needs to be redone
  213. #endif
  214. #if 0
  215. RSG::rasterizer->blit_render_target_to_screen(*render_target, screen_rect, 0);
  216. #endif
  217. }
  218. godot_int GDAPI godot_arvr_get_texid(godot_rid *p_render_target) {
  219. // In order to send off our textures to display on our hardware we need the opengl texture ID instead of the render target RID
  220. // This is a handy function to expose that.
  221. #if 0
  222. RID *render_target = (RID *)p_render_target;
  223. RID eye_texture = RSG::storage->render_target_get_texture(*render_target);
  224. #endif
  225. #ifndef _MSC_VER
  226. #warning need to obtain this ID again
  227. #endif
  228. uint32_t texid = 0; //RS::get_singleton()->texture_get_texid(eye_texture);
  229. return texid;
  230. }
  231. 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) {
  232. ARVRServer *arvr_server = ARVRServer::get_singleton();
  233. ERR_FAIL_NULL_V(arvr_server, 0);
  234. InputFilter *input = InputFilter::get_singleton();
  235. ERR_FAIL_NULL_V(input, 0);
  236. ARVRPositionalTracker *new_tracker = memnew(ARVRPositionalTracker);
  237. new_tracker->set_name(p_device_name);
  238. new_tracker->set_type(ARVRServer::TRACKER_CONTROLLER);
  239. if (p_hand == 1) {
  240. new_tracker->set_hand(ARVRPositionalTracker::TRACKER_LEFT_HAND);
  241. } else if (p_hand == 2) {
  242. new_tracker->set_hand(ARVRPositionalTracker::TRACKER_RIGHT_HAND);
  243. }
  244. // also register as joystick...
  245. int joyid = input->get_unused_joy_id();
  246. if (joyid != -1) {
  247. new_tracker->set_joy_id(joyid);
  248. input->joy_connection_changed(joyid, true, p_device_name, "");
  249. }
  250. if (p_tracks_orientation) {
  251. Basis orientation;
  252. new_tracker->set_orientation(orientation);
  253. }
  254. if (p_tracks_position) {
  255. Vector3 position;
  256. new_tracker->set_position(position);
  257. }
  258. // add our tracker to our server and remember its pointer
  259. arvr_server->add_tracker(new_tracker);
  260. // note, this ID is only unique within controllers!
  261. return new_tracker->get_tracker_id();
  262. }
  263. void GDAPI godot_arvr_remove_controller(godot_int p_controller_id) {
  264. ARVRServer *arvr_server = ARVRServer::get_singleton();
  265. ERR_FAIL_NULL(arvr_server);
  266. InputFilter *input = InputFilter::get_singleton();
  267. ERR_FAIL_NULL(input);
  268. ARVRPositionalTracker *remove_tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  269. if (remove_tracker != NULL) {
  270. // unset our joystick if applicable
  271. int joyid = remove_tracker->get_joy_id();
  272. if (joyid != -1) {
  273. input->joy_connection_changed(joyid, false, "", "");
  274. remove_tracker->set_joy_id(-1);
  275. }
  276. // remove our tracker from our server
  277. arvr_server->remove_tracker(remove_tracker);
  278. memdelete(remove_tracker);
  279. }
  280. }
  281. 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) {
  282. ARVRServer *arvr_server = ARVRServer::get_singleton();
  283. ERR_FAIL_NULL(arvr_server);
  284. ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  285. if (tracker != NULL) {
  286. Transform *transform = (Transform *)p_transform;
  287. if (p_tracks_orientation) {
  288. tracker->set_orientation(transform->basis);
  289. }
  290. if (p_tracks_position) {
  291. tracker->set_rw_position(transform->origin);
  292. }
  293. }
  294. }
  295. void GDAPI godot_arvr_set_controller_button(godot_int p_controller_id, godot_int p_button, godot_bool p_is_pressed) {
  296. ARVRServer *arvr_server = ARVRServer::get_singleton();
  297. ERR_FAIL_NULL(arvr_server);
  298. InputFilter *input = InputFilter::get_singleton();
  299. ERR_FAIL_NULL(input);
  300. ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  301. if (tracker != NULL) {
  302. int joyid = tracker->get_joy_id();
  303. if (joyid != -1) {
  304. input->joy_button(joyid, p_button, p_is_pressed);
  305. }
  306. }
  307. }
  308. 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) {
  309. ARVRServer *arvr_server = ARVRServer::get_singleton();
  310. ERR_FAIL_NULL(arvr_server);
  311. InputFilter *input = InputFilter::get_singleton();
  312. ERR_FAIL_NULL(input);
  313. ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  314. if (tracker != NULL) {
  315. int joyid = tracker->get_joy_id();
  316. if (joyid != -1) {
  317. InputFilter::JoyAxis jx;
  318. jx.min = p_can_be_negative ? -1 : 0;
  319. jx.value = p_value;
  320. input->joy_axis(joyid, p_axis, jx);
  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. ARVRPositionalTracker *tracker = arvr_server->find_by_type_and_id(ARVRServer::TRACKER_CONTROLLER, p_controller_id);
  328. if (tracker != NULL) {
  329. return tracker->get_rumble();
  330. }
  331. return 0.0;
  332. }
  333. }