arvr_interface_gdnative.cpp 13 KB

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