mobile_interface.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*************************************************************************/
  2. /* mobile_interface.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 "mobile_interface.h"
  31. #include "core/os/input.h"
  32. #include "core/os/os.h"
  33. #include "servers/visual/visual_server_global.h"
  34. StringName MobileVRInterface::get_name() const {
  35. return "Native mobile";
  36. };
  37. int MobileVRInterface::get_capabilities() const {
  38. return ARVRInterface::ARVR_STEREO;
  39. };
  40. Vector3 MobileVRInterface::scale_magneto(const Vector3 &p_magnetometer) {
  41. // Our magnetometer doesn't give us nice clean data.
  42. // Well it may on Mac OS X because we're getting a calibrated value in the current implementation but Android we're getting raw data.
  43. // This is a fairly simple adjustment we can do to correct for the magnetometer data being elliptical
  44. Vector3 mag_raw = p_magnetometer;
  45. Vector3 mag_scaled = p_magnetometer;
  46. // update our variables every x frames
  47. if (mag_count > 20) {
  48. mag_current_min = mag_next_min;
  49. mag_current_max = mag_next_max;
  50. mag_count = 0;
  51. } else {
  52. mag_count++;
  53. };
  54. // adjust our min and max
  55. if (mag_raw.x > mag_next_max.x) mag_next_max.x = mag_raw.x;
  56. if (mag_raw.y > mag_next_max.y) mag_next_max.y = mag_raw.y;
  57. if (mag_raw.z > mag_next_max.z) mag_next_max.z = mag_raw.z;
  58. if (mag_raw.x < mag_next_min.x) mag_next_min.x = mag_raw.x;
  59. if (mag_raw.y < mag_next_min.y) mag_next_min.y = mag_raw.y;
  60. if (mag_raw.z < mag_next_min.z) mag_next_min.z = mag_raw.z;
  61. // scale our x, y and z
  62. if (!(mag_current_max.x - mag_current_min.x)) {
  63. mag_raw.x -= (mag_current_min.x + mag_current_max.x) / 2.0;
  64. mag_scaled.x = (mag_raw.x - mag_current_min.x) / ((mag_current_max.x - mag_current_min.x) * 2.0 - 1.0);
  65. };
  66. if (!(mag_current_max.y - mag_current_min.y)) {
  67. mag_raw.y -= (mag_current_min.y + mag_current_max.y) / 2.0;
  68. mag_scaled.y = (mag_raw.y - mag_current_min.y) / ((mag_current_max.y - mag_current_min.y) * 2.0 - 1.0);
  69. };
  70. if (!(mag_current_max.z - mag_current_min.z)) {
  71. mag_raw.z -= (mag_current_min.z + mag_current_max.z) / 2.0;
  72. mag_scaled.z = (mag_raw.z - mag_current_min.z) / ((mag_current_max.z - mag_current_min.z) * 2.0 - 1.0);
  73. };
  74. return mag_scaled;
  75. };
  76. Basis MobileVRInterface::combine_acc_mag(const Vector3 &p_grav, const Vector3 &p_magneto) {
  77. // yup, stock standard cross product solution...
  78. Vector3 up = -p_grav.normalized();
  79. Vector3 magneto_east = up.cross(p_magneto.normalized()); // or is this west?, but should be horizon aligned now
  80. magneto_east.normalize();
  81. Vector3 magneto = up.cross(magneto_east); // and now we have a horizon aligned north
  82. magneto.normalize();
  83. // We use our gravity and magnetometer vectors to construct our matrix
  84. Basis acc_mag_m3;
  85. acc_mag_m3.elements[0] = -magneto_east;
  86. acc_mag_m3.elements[1] = up;
  87. acc_mag_m3.elements[2] = magneto;
  88. return acc_mag_m3;
  89. };
  90. void MobileVRInterface::set_position_from_sensors() {
  91. _THREAD_SAFE_METHOD_
  92. // this is a helper function that attempts to adjust our transform using our 9dof sensors
  93. // 9dof is a misleading marketing term coming from 3 accelerometer axis + 3 gyro axis + 3 magnetometer axis = 9 axis
  94. // but in reality this only offers 3 dof (yaw, pitch, roll) orientation
  95. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  96. uint64_t ticks_elapsed = ticks - last_ticks;
  97. float delta_time = (double)ticks_elapsed / 1000000.0;
  98. // few things we need
  99. Input *input = Input::get_singleton();
  100. Vector3 down(0.0, -1.0, 0.0); // Down is Y negative
  101. Vector3 north(0.0, 0.0, 1.0); // North is Z positive
  102. // make copies of our inputs
  103. bool has_grav = false;
  104. Vector3 acc = input->get_accelerometer();
  105. Vector3 gyro = input->get_gyroscope();
  106. Vector3 grav = input->get_gravity();
  107. Vector3 magneto = scale_magneto(input->get_magnetometer()); // this may be overkill on iOS because we're already getting a calibrated magnetometer reading
  108. if (sensor_first) {
  109. sensor_first = false;
  110. } else {
  111. acc = scrub(acc, last_accerometer_data, 2, 0.2);
  112. magneto = scrub(magneto, last_magnetometer_data, 3, 0.3);
  113. };
  114. last_accerometer_data = acc;
  115. last_magnetometer_data = magneto;
  116. if (grav.length() < 0.1) {
  117. // not ideal but use our accelerometer, this will contain shakey shakey user behaviour
  118. // maybe look into some math but I'm guessing that if this isn't available, its because we lack the gyro sensor to actually work out
  119. // what a stable gravity vector is
  120. grav = acc;
  121. if (grav.length() > 0.1) {
  122. has_grav = true;
  123. };
  124. } else {
  125. has_grav = true;
  126. };
  127. bool has_magneto = magneto.length() > 0.1;
  128. if (gyro.length() > 0.1) {
  129. /* this can return to 0.0 if the user doesn't move the phone, so once on, it's on */
  130. has_gyro = true;
  131. };
  132. if (has_gyro) {
  133. // start with applying our gyro (do NOT smooth our gyro!)
  134. Basis rotate;
  135. rotate.rotate(orientation.get_axis(0), gyro.x * delta_time);
  136. rotate.rotate(orientation.get_axis(1), gyro.y * delta_time);
  137. rotate.rotate(orientation.get_axis(2), gyro.z * delta_time);
  138. orientation = rotate * orientation;
  139. tracking_state = ARVRInterface::ARVR_NORMAL_TRACKING;
  140. };
  141. ///@TODO improve this, the magnetometer is very fidgity sometimes flipping the axis for no apparent reason (probably a bug on my part)
  142. // if you have a gyro + accelerometer that combo tends to be better then combining all three but without a gyro you need the magnetometer..
  143. if (has_magneto && has_grav && !has_gyro) {
  144. // convert to quaternions, easier to smooth those out
  145. Quat transform_quat(orientation);
  146. Quat acc_mag_quat(combine_acc_mag(grav, magneto));
  147. transform_quat = transform_quat.slerp(acc_mag_quat, 0.1);
  148. orientation = Basis(transform_quat);
  149. tracking_state = ARVRInterface::ARVR_NORMAL_TRACKING;
  150. } else if (has_grav) {
  151. // use gravity vector to make sure down is down...
  152. // transform gravity into our world space
  153. grav.normalize();
  154. Vector3 grav_adj = orientation.xform(grav);
  155. float dot = grav_adj.dot(down);
  156. if ((dot > -1.0) && (dot < 1.0)) {
  157. // axis around which we have this rotation
  158. Vector3 axis = grav_adj.cross(down);
  159. axis.normalize();
  160. Basis drift_compensation(axis, acos(dot) * delta_time * 10);
  161. orientation = drift_compensation * orientation;
  162. };
  163. };
  164. // JIC
  165. orientation.orthonormalize();
  166. last_ticks = ticks;
  167. };
  168. void MobileVRInterface::_bind_methods() {
  169. ClassDB::bind_method(D_METHOD("set_iod", "iod"), &MobileVRInterface::set_iod);
  170. ClassDB::bind_method(D_METHOD("get_iod"), &MobileVRInterface::get_iod);
  171. ClassDB::bind_method(D_METHOD("set_display_width", "display_width"), &MobileVRInterface::set_display_width);
  172. ClassDB::bind_method(D_METHOD("get_display_width"), &MobileVRInterface::get_display_width);
  173. ClassDB::bind_method(D_METHOD("set_display_to_lens", "display_to_lens"), &MobileVRInterface::set_display_to_lens);
  174. ClassDB::bind_method(D_METHOD("get_display_to_lens"), &MobileVRInterface::get_display_to_lens);
  175. ClassDB::bind_method(D_METHOD("set_oversample", "oversample"), &MobileVRInterface::set_oversample);
  176. ClassDB::bind_method(D_METHOD("get_oversample"), &MobileVRInterface::get_oversample);
  177. ClassDB::bind_method(D_METHOD("set_k1", "k"), &MobileVRInterface::set_k1);
  178. ClassDB::bind_method(D_METHOD("get_k1"), &MobileVRInterface::get_k1);
  179. ClassDB::bind_method(D_METHOD("set_k2", "k"), &MobileVRInterface::set_k2);
  180. ClassDB::bind_method(D_METHOD("get_k2"), &MobileVRInterface::get_k2);
  181. ADD_PROPERTY(PropertyInfo(Variant::REAL, "iod", PROPERTY_HINT_RANGE, "4.0,10.0,0.1"), "set_iod", "get_iod");
  182. ADD_PROPERTY(PropertyInfo(Variant::REAL, "display_width", PROPERTY_HINT_RANGE, "5.0,25.0,0.1"), "set_display_width", "get_display_width");
  183. ADD_PROPERTY(PropertyInfo(Variant::REAL, "display_to_lens", PROPERTY_HINT_RANGE, "5.0,25.0,0.1"), "set_display_to_lens", "get_display_to_lens");
  184. ADD_PROPERTY(PropertyInfo(Variant::REAL, "oversample", PROPERTY_HINT_RANGE, "1.0,2.0,0.1"), "set_oversample", "get_oversample");
  185. ADD_PROPERTY(PropertyInfo(Variant::REAL, "k1", PROPERTY_HINT_RANGE, "0.1,10.0,0.0001"), "set_k1", "get_k1");
  186. ADD_PROPERTY(PropertyInfo(Variant::REAL, "k2", PROPERTY_HINT_RANGE, "0.1,10.0,0.0001"), "set_k2", "get_k2");
  187. }
  188. void MobileVRInterface::set_iod(const real_t p_iod) {
  189. intraocular_dist = p_iod;
  190. };
  191. real_t MobileVRInterface::get_iod() const {
  192. return intraocular_dist;
  193. };
  194. void MobileVRInterface::set_display_width(const real_t p_display_width) {
  195. display_width = p_display_width;
  196. };
  197. real_t MobileVRInterface::get_display_width() const {
  198. return display_width;
  199. };
  200. void MobileVRInterface::set_display_to_lens(const real_t p_display_to_lens) {
  201. display_to_lens = p_display_to_lens;
  202. };
  203. real_t MobileVRInterface::get_display_to_lens() const {
  204. return display_to_lens;
  205. };
  206. void MobileVRInterface::set_oversample(const real_t p_oversample) {
  207. oversample = p_oversample;
  208. };
  209. real_t MobileVRInterface::get_oversample() const {
  210. return oversample;
  211. };
  212. void MobileVRInterface::set_k1(const real_t p_k1) {
  213. k1 = p_k1;
  214. };
  215. real_t MobileVRInterface::get_k1() const {
  216. return k1;
  217. };
  218. void MobileVRInterface::set_k2(const real_t p_k2) {
  219. k2 = p_k2;
  220. };
  221. real_t MobileVRInterface::get_k2() const {
  222. return k2;
  223. };
  224. bool MobileVRInterface::is_stereo() {
  225. // needs stereo...
  226. return true;
  227. };
  228. bool MobileVRInterface::is_initialized() {
  229. return (initialized);
  230. };
  231. bool MobileVRInterface::initialize() {
  232. ARVRServer *arvr_server = ARVRServer::get_singleton();
  233. ERR_FAIL_NULL_V(arvr_server, false);
  234. if (!initialized) {
  235. // reset our sensor data and orientation
  236. mag_count = 0;
  237. has_gyro = false;
  238. sensor_first = true;
  239. mag_next_min = Vector3(10000, 10000, 10000);
  240. mag_next_max = Vector3(-10000, -10000, -10000);
  241. mag_current_min = Vector3(0, 0, 0);
  242. mag_current_max = Vector3(0, 0, 0);
  243. // reset our orientation
  244. orientation = Basis();
  245. // make this our primary interface
  246. arvr_server->set_primary_interface(this);
  247. last_ticks = OS::get_singleton()->get_ticks_usec();
  248. ;
  249. initialized = true;
  250. };
  251. return true;
  252. };
  253. void MobileVRInterface::uninitialize() {
  254. if (initialized) {
  255. ARVRServer *arvr_server = ARVRServer::get_singleton();
  256. if (arvr_server != NULL) {
  257. // no longer our primary interface
  258. arvr_server->clear_primary_interface_if(this);
  259. }
  260. initialized = false;
  261. };
  262. };
  263. Size2 MobileVRInterface::get_render_targetsize() {
  264. _THREAD_SAFE_METHOD_
  265. // we use half our window size
  266. Size2 target_size = OS::get_singleton()->get_window_size();
  267. target_size.x *= 0.5 * oversample;
  268. target_size.y *= oversample;
  269. return target_size;
  270. };
  271. Transform MobileVRInterface::get_transform_for_eye(ARVRInterface::Eyes p_eye, const Transform &p_cam_transform) {
  272. _THREAD_SAFE_METHOD_
  273. Transform transform_for_eye;
  274. ARVRServer *arvr_server = ARVRServer::get_singleton();
  275. ERR_FAIL_NULL_V(arvr_server, transform_for_eye);
  276. if (initialized) {
  277. float world_scale = arvr_server->get_world_scale();
  278. // we don't need to check for the existence of our HMD, doesn't effect our values...
  279. // note * 0.01 to convert cm to m and * 0.5 as we're moving half in each direction...
  280. if (p_eye == ARVRInterface::EYE_LEFT) {
  281. transform_for_eye.origin.x = -(intraocular_dist * 0.01 * 0.5 * world_scale);
  282. } else if (p_eye == ARVRInterface::EYE_RIGHT) {
  283. transform_for_eye.origin.x = intraocular_dist * 0.01 * 0.5 * world_scale;
  284. } else {
  285. // for mono we don't reposition, we want our center position.
  286. };
  287. // just scale our origin point of our transform
  288. Transform hmd_transform;
  289. hmd_transform.basis = orientation;
  290. hmd_transform.origin = Vector3(0.0, eye_height * world_scale, 0.0);
  291. transform_for_eye = p_cam_transform * (arvr_server->get_reference_frame()) * hmd_transform * transform_for_eye;
  292. } else {
  293. // huh? well just return what we got....
  294. transform_for_eye = p_cam_transform;
  295. };
  296. return transform_for_eye;
  297. };
  298. CameraMatrix MobileVRInterface::get_projection_for_eye(ARVRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far) {
  299. _THREAD_SAFE_METHOD_
  300. CameraMatrix eye;
  301. if (p_eye == ARVRInterface::EYE_MONO) {
  302. ///@TODO for now hardcode some of this, what is really needed here is that this needs to be in sync with the real cameras properties
  303. // which probably means implementing a specific class for iOS and Android. For now this is purely here as an example.
  304. // Note also that if you use a normal viewport with AR/VR turned off you can still use the tracker output of this interface
  305. // to position a stock standard Godot camera and have control over this.
  306. // This will make more sense when we implement ARkit on iOS (probably a separate interface).
  307. eye.set_perspective(60.0, p_aspect, p_z_near, p_z_far, false);
  308. } else {
  309. eye.set_for_hmd(p_eye == ARVRInterface::EYE_LEFT ? 1 : 2, p_aspect, intraocular_dist, display_width, display_to_lens, oversample, p_z_near, p_z_far);
  310. };
  311. return eye;
  312. };
  313. void MobileVRInterface::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
  314. _THREAD_SAFE_METHOD_
  315. // We must have a valid render target
  316. ERR_FAIL_COND(!p_render_target.is_valid());
  317. // Because we are rendering to our device we must use our main viewport!
  318. ERR_FAIL_COND(p_screen_rect == Rect2());
  319. float offset_x = 0.0;
  320. float aspect_ratio = 0.5 * p_screen_rect.size.x / p_screen_rect.size.y;
  321. Vector2 eye_center;
  322. if (p_eye == ARVRInterface::EYE_LEFT) {
  323. offset_x = -1.0;
  324. eye_center.x = ((-intraocular_dist / 2.0) + (display_width / 4.0)) / (display_width / 2.0);
  325. } else if (p_eye == ARVRInterface::EYE_RIGHT) {
  326. eye_center.x = ((intraocular_dist / 2.0) - (display_width / 4.0)) / (display_width / 2.0);
  327. }
  328. // unset our render target so we are outputting to our main screen by making RasterizerStorageGLES3::system_fbo our current FBO
  329. VSG::rasterizer->set_current_render_target(RID());
  330. // now output to screen
  331. // VSG::rasterizer->blit_render_target_to_screen(p_render_target, screen_rect, 0);
  332. // get our render target
  333. RID eye_texture = VSG::storage->render_target_get_texture(p_render_target);
  334. uint32_t texid = VS::get_singleton()->texture_get_texid(eye_texture);
  335. glActiveTexture(GL_TEXTURE0);
  336. glBindTexture(GL_TEXTURE_2D, texid);
  337. lens_shader.bind();
  338. lens_shader.set_uniform(LensDistortedShaderGLES3::OFFSET_X, offset_x);
  339. lens_shader.set_uniform(LensDistortedShaderGLES3::K1, k1);
  340. lens_shader.set_uniform(LensDistortedShaderGLES3::K2, k2);
  341. lens_shader.set_uniform(LensDistortedShaderGLES3::EYE_CENTER, eye_center);
  342. lens_shader.set_uniform(LensDistortedShaderGLES3::UPSCALE, oversample);
  343. lens_shader.set_uniform(LensDistortedShaderGLES3::ASPECT_RATIO, aspect_ratio);
  344. glBindVertexArray(half_screen_array);
  345. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  346. glBindVertexArray(0);
  347. };
  348. void MobileVRInterface::process() {
  349. _THREAD_SAFE_METHOD_
  350. if (initialized) {
  351. set_position_from_sensors();
  352. };
  353. };
  354. MobileVRInterface::MobileVRInterface() {
  355. initialized = false;
  356. // Just set some defaults for these. At some point we need to look at adding a lookup table for common device + headset combos and/or support reading cardboard QR codes
  357. eye_height = 1.85;
  358. intraocular_dist = 6.0;
  359. display_width = 14.5;
  360. display_to_lens = 4.0;
  361. oversample = 1.5;
  362. k1 = 0.215;
  363. k2 = 0.215;
  364. last_ticks = 0;
  365. // create our shader stuff
  366. lens_shader.init();
  367. {
  368. glGenBuffers(1, &half_screen_quad);
  369. glBindBuffer(GL_ARRAY_BUFFER, half_screen_quad);
  370. {
  371. /* clang-format off */
  372. const float qv[16] = {
  373. 0, -1,
  374. -1, -1,
  375. 0, 1,
  376. -1, 1,
  377. 1, 1,
  378. 1, 1,
  379. 1, -1,
  380. 1, -1,
  381. };
  382. /* clang-format on */
  383. glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 16, qv, GL_STATIC_DRAW);
  384. }
  385. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  386. glGenVertexArrays(1, &half_screen_array);
  387. glBindVertexArray(half_screen_array);
  388. glBindBuffer(GL_ARRAY_BUFFER, half_screen_quad);
  389. glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0);
  390. glEnableVertexAttribArray(0);
  391. glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, ((uint8_t *)NULL) + 8);
  392. glEnableVertexAttribArray(4);
  393. glBindVertexArray(0);
  394. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  395. }
  396. };
  397. MobileVRInterface::~MobileVRInterface() {
  398. // and make sure we cleanup if we haven't already
  399. if (is_initialized()) {
  400. uninitialize();
  401. };
  402. };