💿🐜 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.

330 lines
9.4 KiB

3 years ago
3 years ago
  1. /*
  2. * Copyright (C) 2021 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 "tool.hpp"
  20. #include "entity/components/collision.hpp"
  21. #include "entity/components/tool.hpp"
  22. #include "entity/components/transform.hpp"
  23. #include "event/event-dispatcher.hpp"
  24. #include "game/events/tool-events.hpp"
  25. #include "animation/ease.hpp"
  26. #include "geom/mesh.hpp"
  27. #include "geom/intersection.hpp"
  28. #include "math/math.hpp"
  29. #include "entity/commands.hpp"
  30. namespace entity {
  31. namespace system {
  32. tool::tool(entity::registry& registry, ::event_dispatcher* event_dispatcher):
  33. updatable(registry),
  34. event_dispatcher(event_dispatcher),
  35. camera(nullptr),
  36. viewport{0, 0, 0, 0},
  37. mouse_position{0, 0},
  38. pick_enabled(true),
  39. was_pick_enabled(pick_enabled),
  40. active_tool(entt::null)
  41. {
  42. hand_angle_spring.z = 1.0f;
  43. hand_angle_spring.w = hz_to_rads(8.0f);
  44. hand_angle_spring.x1 = math::pi<float>;
  45. hand_angle_spring.x0 = hand_angle_spring.x1;
  46. hand_angle_spring.v = 0.0f;
  47. pick_spring.z = 1.0f;
  48. pick_spring.w = hz_to_rads(30.0f);
  49. pick_spring.x1 = {0.0f, 0.0f, 0.0f};
  50. pick_spring.x0 = pick_spring.x1;
  51. pick_spring.v = {0.0f, 0.0f, 0.0f};
  52. // Create descend animation
  53. animation_channel<float>* channel = descend_animation.add_channel(0);
  54. descend_animation.set_interpolator(ease<float, double>::out_cubic);
  55. descend_animation.set_frame_callback
  56. (
  57. [this](int channel, const float& t)
  58. {
  59. this->active_tool_distance = t;
  60. }
  61. );
  62. // Create descend animation
  63. channel = ascend_animation.add_channel(0);
  64. ascend_animation.set_interpolator(ease<float, double>::out_cubic);
  65. ascend_animation.set_frame_callback
  66. (
  67. [this](int channel, const float& t)
  68. {
  69. this->active_tool_distance = t;
  70. }
  71. );
  72. active_tool = entt::null;
  73. active_tool_distance = 0.0f;
  74. warp = true;
  75. tool_active = false;
  76. event_dispatcher->subscribe<mouse_moved_event>(this);
  77. event_dispatcher->subscribe<window_resized_event>(this);
  78. }
  79. tool::~tool()
  80. {
  81. event_dispatcher->unsubscribe<mouse_moved_event>(this);
  82. event_dispatcher->unsubscribe<window_resized_event>(this);
  83. }
  84. void tool::update(double t, double dt)
  85. {
  86. /*
  87. if (active_tool == entt::null)
  88. return;
  89. // Advance animations
  90. ascend_animation.advance(dt);
  91. descend_animation.advance(dt);
  92. if (!camera)
  93. return;
  94. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  95. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  96. float3 pick_origin = pick_near;
  97. float3 pick_direction = math::normalize(pick_far - pick_near);
  98. geom::ray<float> picking_ray = {pick_near, pick_direction};
  99. float a = std::numeric_limits<float>::infinity();
  100. bool intersection = false;
  101. float3 pick;
  102. // Cast ray from cursor to collision components to find closest intersection
  103. registry.view<component::transform, component::collision>().each(
  104. [&](entity::id entity_id, auto& transform, auto& collision)
  105. {
  106. math::transform<float> inverse_transform = math::inverse(transform.local);
  107. float3 origin = inverse_transform * pick_origin;
  108. float3 direction = math::normalize(math::conjugate(transform.local.rotation) * pick_direction);
  109. geom::ray<float> transformed_ray = {origin, direction};
  110. // Broad phase AABB test
  111. auto aabb_result = geom::ray_aabb_intersection(transformed_ray, collision.bounds);
  112. if (!std::get<0>(aabb_result))
  113. {
  114. return;
  115. }
  116. // Narrow phase mesh test
  117. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  118. if (mesh_result)
  119. {
  120. intersection = true;
  121. if (mesh_result->t < a)
  122. {
  123. a = mesh_result->t;
  124. pick = picking_ray.extrapolate(a);
  125. pick_spring.x1 = pick;
  126. }
  127. }
  128. });
  129. const float3& camera_position = camera->get_translation();
  130. float3 pick_planar_position = float3{pick.x, 0, pick.z};
  131. float3 camera_planar_position = float3{camera_position.x, 0, camera_position.z};
  132. float pick_angle = 0.0f;
  133. float3 pick_planar_direction = math::normalize(pick_planar_position - camera_planar_position);
  134. float3 camera_planar_focal_point = float3{orbit_cam->get_focal_point().x, 0, orbit_cam->get_focal_point().z};
  135. float3 camera_planar_direction = math::normalize(camera_planar_focal_point - camera_planar_position);
  136. if (std::fabs(math::length_squared(camera_planar_direction - pick_planar_direction) > 0.0001f))
  137. {
  138. pick_angle = std::acos(math::dot(camera_planar_direction, pick_planar_direction));
  139. if (math::dot(math::cross(camera_planar_direction, pick_planar_direction), float3{0, 1, 0}) < 0.0f)
  140. pick_angle = -pick_angle;
  141. }
  142. // Determine target hand angle
  143. hand_angle_spring.x1 = -std::min<float>(0.5f, std::max<float>(-0.5f, ((mouse_position[0] / viewport[2]) - 0.5f) * 1.0f)) * (math::pi<float>);
  144. // Solve springs
  145. solve_numeric_spring<float, float>(hand_angle_spring, dt);
  146. solve_numeric_spring<float3, float>(pick_spring, dt);
  147. // Don't use spring for picking
  148. pick_spring.x0 = pick_spring.x1;
  149. // Move active tools to intersection location
  150. registry.view<component::tool, component::transform>().each(
  151. [&](entity::id entity_id, auto& tool, auto& transform)
  152. {
  153. if (!tool.active)
  154. return;
  155. active_tool = entity_id;
  156. float tool_distance = active_tool_distance;//(tool_active) ? tool.active_distance : tool.idle_distance;
  157. // Interpolate between left and right hand
  158. math::quaternion<float> hand_rotation = math::angle_axis(orbit_cam->get_azimuth() + hand_angle_spring.x0, float3{0, 1, 0});
  159. math::quaternion<float> tilt_rotation = math::angle_axis(orbit_cam->get_elevation(), float3{-1.0f, 0.0f, 0.0f});
  160. if (tool.heliotropic)
  161. {
  162. math::quaternion<float> solar_rotation = math::rotation(float3{0, -1, 0}, sun_direction);
  163. transform.local.translation = pick_spring.x0 + solar_rotation * float3{0, tool_distance, 0};
  164. transform.local.rotation = solar_rotation * hand_rotation;
  165. }
  166. else
  167. {
  168. math::quaternion<float> rotation = hand_rotation * tilt_rotation;
  169. transform.local.translation = pick_spring.x0 + rotation * float3{0, tool_distance, 0};
  170. transform.local.rotation = rotation;
  171. }
  172. if (warp)
  173. {
  174. transform.warp = true;
  175. command::assign_render_layers(registry, active_tool, 1);
  176. warp = false;
  177. }
  178. // Update tool's cursor position
  179. tool.cursor = pick_spring.x0;
  180. //math::quaternion<float> rotation = math::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0});
  181. //transform.transform.rotation = rotation;
  182. });
  183. was_pick_enabled = pick_enabled;
  184. */
  185. }
  186. void tool::set_camera(const scene::camera* camera)
  187. {
  188. this->camera = camera;
  189. }
  190. void tool::set_viewport(const float4& viewport)
  191. {
  192. this->viewport = viewport;
  193. mouse_position.x = viewport[2] * 0.5f;
  194. mouse_position.y = viewport[3] * 0.5f;
  195. }
  196. void tool::set_pick(bool enabled)
  197. {
  198. pick_enabled = enabled;
  199. }
  200. void tool::set_sun_direction(const float3& direction)
  201. {
  202. sun_direction = direction;
  203. }
  204. void tool::set_active_tool(entity::id entity_id)
  205. {
  206. if (active_tool == entity_id)
  207. return;
  208. const float descent_time = 0.1f;
  209. const float ascent_time = 0.1f;
  210. if (active_tool != entt::null)
  211. {
  212. auto& tool = registry.get<component::tool>(active_tool);
  213. tool.active = false;
  214. command::assign_render_layers(registry, active_tool, 0);
  215. }
  216. active_tool = entity_id;
  217. if (active_tool != entt::null)
  218. {
  219. auto& tool = registry.get<component::tool>(active_tool);
  220. tool.active = true;
  221. active_tool_distance = tool.idle_distance;
  222. command::warp_to(registry, active_tool, pick_spring.x0 + float3{0.0f, tool.idle_distance, 0.0f});
  223. // Adjust descend and ascend animations
  224. animation_channel<float>* channel = descend_animation.get_channel(0);
  225. channel->remove_keyframes();
  226. channel->insert_keyframe({0.0, tool.idle_distance});
  227. channel->insert_keyframe({descent_time, tool.active_distance});
  228. channel = ascend_animation.get_channel(0);
  229. channel->remove_keyframes();
  230. channel->insert_keyframe({0.0, tool.active_distance});
  231. channel->insert_keyframe({ascent_time, tool.idle_distance});
  232. }
  233. warp = true;
  234. }
  235. void tool::set_tool_active(bool active)
  236. {
  237. if (active_tool == entt::null)
  238. return;
  239. tool_active = active;
  240. if (active)
  241. {
  242. descend_animation.rewind();
  243. descend_animation.play();
  244. // Queue tool pressed event
  245. tool_pressed_event event;
  246. event.entity_id = active_tool;
  247. event.position = pick_spring.x0;
  248. event_dispatcher->queue(event);
  249. }
  250. else
  251. {
  252. ascend_animation.rewind();
  253. ascend_animation.play();
  254. // Queue tool pressed event
  255. tool_released_event event;
  256. event.entity_id = active_tool;
  257. event.position = pick_spring.x0;
  258. event_dispatcher->queue(event);
  259. }
  260. }
  261. void tool::handle_event(const mouse_moved_event& event)
  262. {
  263. if (pick_enabled && was_pick_enabled)
  264. {
  265. mouse_position[0] = event.x;
  266. mouse_position[1] = event.y;
  267. }
  268. }
  269. void tool::handle_event(const window_resized_event& event)
  270. {
  271. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  272. }
  273. } // namespace system
  274. } // namespace entity