xr_interface_extension.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*************************************************************************/
  2. /* xr_interface_extension.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_extension.h"
  31. #include "servers/rendering/renderer_storage.h"
  32. #include "servers/rendering/rendering_server_globals.h"
  33. void XRInterfaceExtension::_bind_methods() {
  34. GDVIRTUAL_BIND(_get_name);
  35. GDVIRTUAL_BIND(_get_capabilities);
  36. GDVIRTUAL_BIND(_is_initialized);
  37. GDVIRTUAL_BIND(_initialize);
  38. GDVIRTUAL_BIND(_uninitialize);
  39. GDVIRTUAL_BIND(_get_tracking_status);
  40. GDVIRTUAL_BIND(_get_render_target_size);
  41. GDVIRTUAL_BIND(_get_view_count);
  42. GDVIRTUAL_BIND(_get_camera_transform);
  43. GDVIRTUAL_BIND(_get_transform_for_view, "view", "cam_transform");
  44. GDVIRTUAL_BIND(_get_projection_for_view, "view", "aspect", "z_near", "z_far");
  45. GDVIRTUAL_BIND(_commit_views, "render_target", "screen_rect");
  46. GDVIRTUAL_BIND(_process);
  47. GDVIRTUAL_BIND(_notification, "what");
  48. // we don't have any properties specific to VR yet....
  49. // but we do have properties specific to AR....
  50. GDVIRTUAL_BIND(_get_anchor_detection_is_enabled);
  51. GDVIRTUAL_BIND(_set_anchor_detection_is_enabled, "enabled");
  52. GDVIRTUAL_BIND(_get_camera_feed_id);
  53. // helper methods
  54. ClassDB::bind_method(D_METHOD("add_blit", "render_target", "src_rect", "dst_rect", "use_layer", "layer", "apply_lens_distortion", "eye_center", "k1", "k2", "upscale", "aspect_ratio"), &XRInterfaceExtension::add_blit);
  55. ClassDB::bind_method(D_METHOD("get_render_target_texture", "render_target"), &XRInterfaceExtension::get_render_target_texture);
  56. // ClassDB::bind_method(D_METHOD("get_render_target_depth", "render_target"), &XRInterfaceExtension::get_render_target_depth);
  57. }
  58. StringName XRInterfaceExtension::get_name() const {
  59. StringName name;
  60. if (GDVIRTUAL_CALL(_get_name, name)) {
  61. return name;
  62. }
  63. return "Unknown";
  64. }
  65. uint32_t XRInterfaceExtension::get_capabilities() const {
  66. uint32_t capabilities;
  67. if (GDVIRTUAL_CALL(_get_capabilities, capabilities)) {
  68. return capabilities;
  69. }
  70. return 0;
  71. }
  72. bool XRInterfaceExtension::is_initialized() const {
  73. bool initialised = false;
  74. if (GDVIRTUAL_CALL(_is_initialized, initialised)) {
  75. return initialised;
  76. }
  77. return false;
  78. }
  79. bool XRInterfaceExtension::initialize() {
  80. bool initialised = false;
  81. if (GDVIRTUAL_CALL(_initialize, initialised)) {
  82. return initialised;
  83. }
  84. return false;
  85. }
  86. void XRInterfaceExtension::uninitialize() {
  87. GDVIRTUAL_CALL(_uninitialize);
  88. }
  89. XRInterface::TrackingStatus XRInterfaceExtension::get_tracking_status() const {
  90. uint32_t status;
  91. if (GDVIRTUAL_CALL(_get_tracking_status, status)) {
  92. return TrackingStatus(status);
  93. }
  94. return XR_UNKNOWN_TRACKING;
  95. }
  96. /** these will only be implemented on AR interfaces, so we want dummies for VR **/
  97. bool XRInterfaceExtension::get_anchor_detection_is_enabled() const {
  98. bool enabled;
  99. if (GDVIRTUAL_CALL(_get_anchor_detection_is_enabled, enabled)) {
  100. return enabled;
  101. }
  102. return false;
  103. }
  104. void XRInterfaceExtension::set_anchor_detection_is_enabled(bool p_enable) {
  105. // don't do anything here, this needs to be implemented on AR interface to enable/disable things like plane detection etc.
  106. GDVIRTUAL_CALL(_set_anchor_detection_is_enabled, p_enable);
  107. }
  108. int XRInterfaceExtension::get_camera_feed_id() {
  109. int feed_id;
  110. if (GDVIRTUAL_CALL(_get_camera_feed_id, feed_id)) {
  111. return feed_id;
  112. }
  113. return 0;
  114. }
  115. Size2 XRInterfaceExtension::get_render_target_size() {
  116. Size2 size;
  117. if (GDVIRTUAL_CALL(_get_render_target_size, size)) {
  118. return size;
  119. }
  120. return Size2(0, 0);
  121. }
  122. uint32_t XRInterfaceExtension::get_view_count() {
  123. uint32_t view_count;
  124. if (GDVIRTUAL_CALL(_get_view_count, view_count)) {
  125. return view_count;
  126. }
  127. return 1;
  128. }
  129. Transform3D XRInterfaceExtension::get_camera_transform() {
  130. Transform3D transform;
  131. if (GDVIRTUAL_CALL(_get_camera_transform, transform)) {
  132. return transform;
  133. }
  134. return Transform3D();
  135. }
  136. Transform3D XRInterfaceExtension::get_transform_for_view(uint32_t p_view, const Transform3D &p_cam_transform) {
  137. Transform3D transform;
  138. if (GDVIRTUAL_CALL(_get_transform_for_view, p_view, p_cam_transform, transform)) {
  139. return transform;
  140. }
  141. return Transform3D();
  142. }
  143. CameraMatrix XRInterfaceExtension::get_projection_for_view(uint32_t p_view, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
  144. CameraMatrix cm;
  145. PackedFloat64Array arr;
  146. if (GDVIRTUAL_CALL(_get_projection_for_view, p_view, p_aspect, p_z_near, p_z_far, arr)) {
  147. ERR_FAIL_COND_V_MSG(arr.size() != 16, CameraMatrix(), "Projection matrix must contain 16 floats");
  148. real_t *m = (real_t *)cm.matrix;
  149. for (int i = 0; i < 16; i++) {
  150. m[i] = arr[i];
  151. }
  152. return cm;
  153. }
  154. return CameraMatrix();
  155. }
  156. void XRInterfaceExtension::add_blit(RID p_render_target, Rect2 p_src_rect, Rect2i p_dst_rect, bool p_use_layer, uint32_t p_layer, bool p_apply_lens_distortion, Vector2 p_eye_center, float p_k1, float p_k2, float p_upscale, float p_aspect_ratio) {
  157. BlitToScreen blit;
  158. ERR_FAIL_COND_MSG(!can_add_blits, "add_blit can only be called from an XR plugin from within _commit_views!");
  159. blit.render_target = p_render_target;
  160. blit.src_rect = p_src_rect;
  161. blit.dst_rect = p_dst_rect;
  162. blit.multi_view.use_layer = p_use_layer;
  163. blit.multi_view.layer = p_layer;
  164. blit.lens_distortion.apply = p_apply_lens_distortion;
  165. blit.lens_distortion.eye_center = p_eye_center;
  166. blit.lens_distortion.k1 = p_k1;
  167. blit.lens_distortion.k2 = p_k2;
  168. blit.lens_distortion.upscale = p_upscale;
  169. blit.lens_distortion.aspect_ratio = p_aspect_ratio;
  170. blits.push_back(blit);
  171. }
  172. Vector<BlitToScreen> XRInterfaceExtension::commit_views(RID p_render_target, const Rect2 &p_screen_rect) {
  173. // This is just so our XR plugin can add blits...
  174. blits.clear();
  175. can_add_blits = true;
  176. if (GDVIRTUAL_CALL(_commit_views, p_render_target, p_screen_rect)) {
  177. return blits;
  178. }
  179. can_add_blits = false;
  180. return blits;
  181. }
  182. void XRInterfaceExtension::process() {
  183. GDVIRTUAL_CALL(_process);
  184. }
  185. void XRInterfaceExtension::notification(int p_what) {
  186. GDVIRTUAL_CALL(_notification, p_what);
  187. }
  188. RID XRInterfaceExtension::get_render_target_texture(RID p_render_target) {
  189. RendererStorage *storage = RSG::storage;
  190. ERR_FAIL_NULL_V_MSG(storage, RID(), "Renderer storage not setup");
  191. return storage->render_target_get_texture(p_render_target);
  192. }
  193. /*
  194. RID XRInterfaceExtension::get_render_target_depth(RID p_render_target) {
  195. RendererStorage *storage = RSG::storage;
  196. ERR_FAIL_NULL_V_MSG(storage, RID(), "Renderer storage not setup");
  197. return storage->render_target_get_depth(p_render_target);
  198. }
  199. */