💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
9.5 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "control-system.hpp"
  20. #include "input/control.hpp"
  21. #include "orbit-cam.hpp"
  22. #include "scene/camera.hpp"
  23. #include "geometry/intersection.hpp"
  24. #include "animation/ease.hpp"
  25. #include "nest.hpp"
  26. #include "math/math.hpp"
  27. control_system::control_system():
  28. timestep(0.0f),
  29. zoom(0.0f),
  30. tool(nullptr),
  31. flashlight(nullptr),
  32. flashlight_light_cone(nullptr),
  33. underworld_camera(nullptr)
  34. {
  35. control_set.add_control(&move_forward_control);
  36. control_set.add_control(&move_back_control);
  37. control_set.add_control(&move_right_control);
  38. control_set.add_control(&move_left_control);
  39. control_set.add_control(&rotate_ccw_control);
  40. control_set.add_control(&rotate_cw_control);
  41. control_set.add_control(&tilt_up_control);
  42. control_set.add_control(&tilt_down_control);
  43. control_set.add_control(&zoom_in_control);
  44. control_set.add_control(&zoom_out_control);
  45. control_set.add_control(&adjust_camera_control);
  46. control_set.add_control(&ascend_control);
  47. control_set.add_control(&descend_control);
  48. control_set.add_control(&toggle_view_control);
  49. control_set.add_control(&tool_menu_control);
  50. // Set deadzone at 15% for all controls
  51. const std::list<control*>* controls = control_set.get_controls();
  52. for (control* control: *controls)
  53. {
  54. control->set_deadzone(0.15f);
  55. }
  56. zoom_speed = 4.0f; //1
  57. min_elevation = math::radians(-85.0f);
  58. max_elevation = math::radians(85.0f);
  59. near_focal_distance = 2.0f;
  60. far_focal_distance = 200.0f;
  61. near_movement_speed = 10.0f;
  62. far_movement_speed = 80.0f;
  63. near_fov = math::radians(80.0f);
  64. far_fov = math::radians(35.0f);
  65. near_clip_near = 0.1f;
  66. far_clip_near = 5.0f;
  67. near_clip_far = 100.0f;
  68. far_clip_far = 2000.0f;
  69. nest = nullptr;
  70. orbit_cam = nullptr;
  71. mouse_angle = 0.0f;
  72. old_mouse_angle = mouse_angle;
  73. flashlight_turns = 0.0f;
  74. flashlight_turns_i = 0;
  75. flashlight_turns_f = 0.0f;
  76. }
  77. void control_system::update(float dt)
  78. {
  79. this->timestep = dt;
  80. if (adjust_camera_control.is_active() && !adjust_camera_control.was_active())
  81. {
  82. }
  83. // Determine zoom
  84. if (zoom_in_control.is_active())
  85. zoom += zoom_speed * dt * zoom_in_control.get_current_value();
  86. if (zoom_out_control.is_active())
  87. zoom -= zoom_speed * dt * zoom_out_control.get_current_value();
  88. zoom = std::max<float>(0.0f, std::min<float>(1.0f, zoom));
  89. float focal_distance = math::log_lerp<float>(near_focal_distance, far_focal_distance, 1.0f - zoom);
  90. float fov = math::log_lerp<float>(near_fov, far_fov, 1.0f - zoom);
  91. //float elevation_factor = (orbit_cam->get_target_elevation() - min_elevation) / max_elevation;
  92. //fov = math::log_lerp<float>(near_fov, far_fov, elevation_factor);
  93. float clip_near = math::log_lerp<float>(near_clip_near, far_clip_near, 1.0f - zoom);
  94. float clip_far = math::log_lerp<float>(near_clip_far, far_clip_far, 1.0f - zoom);
  95. float movement_speed = math::log_lerp<float>(near_movement_speed * dt, far_movement_speed * dt, 1.0f - zoom);
  96. orbit_cam->set_target_focal_distance(focal_distance);
  97. orbit_cam->get_camera()->set_perspective(fov, orbit_cam->get_camera()->get_aspect_ratio(), clip_near, clip_far);
  98. const float rotation_speed = 2.0f * dt;
  99. float rotation = 0.0f;
  100. if (rotate_ccw_control.is_active())
  101. rotation += rotate_ccw_control.get_current_value() * rotation_speed;
  102. if (rotate_cw_control.is_active())
  103. rotation -= rotate_cw_control.get_current_value() * rotation_speed;
  104. if (rotation)
  105. {
  106. orbit_cam->rotate(rotation);
  107. }
  108. const float tilt_speed = 2.0f * dt;
  109. float tilt = 0.0f;
  110. if (tilt_up_control.is_active())
  111. tilt -= tilt_up_control.get_current_value() * tilt_speed;
  112. if (tilt_down_control.is_active())
  113. tilt += tilt_down_control.get_current_value() * tilt_speed;
  114. if (tilt)
  115. {
  116. orbit_cam->tilt(tilt);
  117. float elevation = orbit_cam->get_target_elevation();
  118. elevation = std::min<float>(max_elevation, std::max<float>(min_elevation, elevation));
  119. orbit_cam->set_target_elevation(elevation);
  120. }
  121. float2 movement{0.0f, 0.0f};
  122. if (move_right_control.is_active())
  123. movement[0] += move_right_control.get_current_value();
  124. if (move_left_control.is_active())
  125. movement[0] -= move_left_control.get_current_value();
  126. if (move_forward_control.is_active())
  127. movement[1] -= move_forward_control.get_current_value();
  128. if (move_back_control.is_active())
  129. movement[1] += move_back_control.get_current_value();
  130. const float deadzone = 0.01f;
  131. float magnitude_squared = math::length_squared(movement);
  132. if (magnitude_squared > deadzone)
  133. {
  134. if (magnitude_squared > 1.0f)
  135. {
  136. movement = math::normalize(movement);
  137. }
  138. orbit_cam->move(movement * movement_speed);
  139. }
  140. float ascention = 0.0f;
  141. if (ascend_control.is_active())
  142. ascention += ascend_control.get_current_value();
  143. if (descend_control.is_active())
  144. ascention -= descend_control.get_current_value();
  145. if (ascention)
  146. {
  147. float old_depth = -orbit_cam->get_target_focal_point()[1];
  148. orbit_cam->set_target_focal_point(orbit_cam->get_target_focal_point() + float3{0.0f, ascention * movement_speed, 0.0f});
  149. if (nest)
  150. {
  151. float3 focal_point = orbit_cam->get_target_focal_point();
  152. float depth = -focal_point[1];
  153. float delta_shaft_angle = nest->get_shaft_angle(*nest->get_central_shaft(), depth) - nest->get_shaft_angle(*nest->get_central_shaft(), old_depth);
  154. //orbit_cam->set_target_azimuth(orbit_cam->get_target_azimuth() - delta_shaft_angle);
  155. orbit_cam->set_target_focal_point(nest->get_shaft_position(*nest->get_central_shaft(), depth));
  156. }
  157. }
  158. orbit_cam->update(dt);
  159. camera* camera = orbit_cam->get_camera();
  160. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  161. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  162. float3 pick_direction = math::normalize(pick_far - pick_near);
  163. ray<float> picking_ray = {pick_near, pick_direction};
  164. plane<float> ground_plane = {float3{0, 1, 0}, 0.0f};
  165. auto intersection_result = ray_plane_intersection(picking_ray, ground_plane);
  166. if (std::get<0>(intersection_result))
  167. {
  168. float3 pick = picking_ray.extrapolate(std::get<1>(intersection_result));
  169. if (tool)
  170. tool->set_translation(pick);
  171. }
  172. if (toggle_view_control.is_active() && !toggle_view_control.was_active())
  173. {
  174. }
  175. // Turn flashlight
  176. float2 viewport_center = {(viewport[0] + viewport[2]) * 0.5f, (viewport[1] + viewport[3]) * 0.5f};
  177. float2 mouse_direction = math::normalize(mouse_position - viewport_center);
  178. old_mouse_angle = mouse_angle;
  179. mouse_angle = std::atan2(-mouse_direction.y, mouse_direction.x);
  180. if (mouse_angle - old_mouse_angle != 0.0f)
  181. {
  182. if (mouse_angle - old_mouse_angle <= -math::pi<float>)
  183. flashlight_turns_i -= 1;
  184. else if (mouse_angle - old_mouse_angle >= math::pi<float>)
  185. flashlight_turns_i += 1;
  186. flashlight_turns_f = (mouse_angle) / math::two_pi<float>;
  187. flashlight_turns = flashlight_turns_i - flashlight_turns_f;
  188. if (flashlight && nest)
  189. {
  190. math::transform<float> flashlight_transform = math::identity_transform<float>;
  191. float flashlight_depth = nest->get_shaft_depth(*nest->get_central_shaft(), flashlight_turns);
  192. flashlight_transform.translation = {0.0f, -flashlight_depth, 0.0f};
  193. flashlight_transform.rotation = math::angle_axis(-flashlight_turns * math::two_pi<float> + math::half_pi<float>, {0, 1, 0});
  194. flashlight->set_transform(flashlight_transform);
  195. flashlight_light_cone->set_transform(flashlight_transform);
  196. if (underworld_camera)
  197. {
  198. underworld_camera->look_at({0, -flashlight_depth + 50.0f, 0}, {0, -flashlight_depth, 0}, {0, 0, -1});
  199. }
  200. }
  201. }
  202. }
  203. void control_system::set_orbit_cam(::orbit_cam* orbit_cam)
  204. {
  205. this->orbit_cam = orbit_cam;
  206. }
  207. void control_system::set_nest(::nest* nest)
  208. {
  209. this->nest = nest;
  210. }
  211. void control_system::set_tool(model_instance* tool)
  212. {
  213. this->tool = tool;
  214. }
  215. void control_system::set_flashlight(model_instance* flashlight, model_instance* light_cone)
  216. {
  217. this->flashlight = flashlight;
  218. this->flashlight_light_cone = light_cone;
  219. }
  220. void control_system::set_viewport(const float4& viewport)
  221. {
  222. this->viewport = viewport;
  223. }
  224. void control_system::set_underworld_camera(::camera* camera)
  225. {
  226. this->underworld_camera = camera;
  227. }
  228. void control_system::handle_event(const mouse_moved_event& event)
  229. {
  230. if (adjust_camera_control.is_active())
  231. {
  232. bool invert_x = true;
  233. bool invert_y = false;
  234. float rotation_factor = event.dx;
  235. float elevation_factor = event.dy;
  236. if (invert_x)
  237. {
  238. rotation_factor *= -1.0f;
  239. }
  240. if (invert_y)
  241. {
  242. elevation_factor *= -1.0f;
  243. }
  244. float rotation = math::radians(22.5f) * rotation_factor * timestep;
  245. float elevation = orbit_cam->get_target_elevation() + elevation_factor * 0.25f * timestep;
  246. elevation = std::min<float>(max_elevation, std::max<float>(min_elevation, elevation));
  247. orbit_cam->rotate(rotation);
  248. orbit_cam->set_target_elevation(elevation);
  249. }
  250. else if (!adjust_camera_control.was_active())
  251. {
  252. mouse_position[0] = event.x;
  253. mouse_position[1] = event.y;
  254. }
  255. }