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

195 lines
6.1 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 "tool-system.hpp"
  20. #include "game/components/collision-component.hpp"
  21. #include "game/components/tool-component.hpp"
  22. #include "game/components/transform-component.hpp"
  23. #include "scene/camera.hpp"
  24. #include "animation/orbit-cam.hpp"
  25. #include "geometry/mesh.hpp"
  26. #include "geometry/intersection.hpp"
  27. #include "math/math.hpp"
  28. using namespace ecs;
  29. tool_system::tool_system(entt::registry& registry):
  30. entity_system(registry),
  31. camera(nullptr),
  32. orbit_cam(orbit_cam),
  33. viewport{0, 0, 0, 0},
  34. mouse_position{0, 0},
  35. pick_enabled(true),
  36. was_pick_enabled(pick_enabled)
  37. {
  38. hand_angle_spring.z = 1.0f;
  39. hand_angle_spring.w = hz_to_rads(8.0f);
  40. hand_angle_spring.x1 = math::pi<float>;
  41. hand_angle_spring.x0 = hand_angle_spring.x1;
  42. hand_angle_spring.v = 0.0f;
  43. pick_spring.z = 1.0f;
  44. pick_spring.w = hz_to_rads(30.0f);
  45. pick_spring.x1 = {0.0f, 0.0f, 0.0f};
  46. pick_spring.x0 = pick_spring.x1;
  47. pick_spring.v = {0.0f, 0.0f, 0.0f};
  48. }
  49. void tool_system::update(double t, double dt)
  50. {
  51. if (!camera)
  52. return;
  53. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  54. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  55. float3 pick_origin = pick_near;
  56. float3 pick_direction = math::normalize(pick_far - pick_near);
  57. ray<float> picking_ray = {pick_near, pick_direction};
  58. float a = std::numeric_limits<float>::infinity();
  59. bool intersection = false;
  60. float3 pick;
  61. // Cast ray from cursor to collision components to find closest intersection
  62. registry.view<transform_component, collision_component>().each(
  63. [&](auto entity, auto& transform, auto& collision)
  64. {
  65. math::transform<float> inverse_transform = math::inverse(transform.transform);
  66. float3 origin = inverse_transform * pick_origin;
  67. float3 direction = math::normalize(math::conjugate(transform.transform.rotation) * pick_direction);
  68. ray<float> transformed_ray = {origin, direction};
  69. // Broad phase AABB test
  70. auto aabb_result = ray_aabb_intersection(transformed_ray, collision.bounds);
  71. if (!std::get<0>(aabb_result))
  72. {
  73. return;
  74. }
  75. // Narrow phase mesh test
  76. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  77. if (mesh_result)
  78. {
  79. intersection = true;
  80. if (mesh_result->t < a)
  81. {
  82. a = mesh_result->t;
  83. pick = picking_ray.extrapolate(a);
  84. pick_spring.x1 = pick;
  85. }
  86. }
  87. });
  88. const float3& camera_position = camera->get_translation();
  89. float3 pick_planar_position = float3{pick.x, 0, pick.z};
  90. float3 camera_planar_position = float3{camera_position.x, 0, camera_position.z};
  91. float pick_angle = 0.0f;
  92. float3 pick_planar_direction = math::normalize(pick_planar_position - camera_planar_position);
  93. float3 camera_planar_focal_point = float3{orbit_cam->get_focal_point().x, 0, orbit_cam->get_focal_point().z};
  94. float3 camera_planar_direction = math::normalize(camera_planar_focal_point - camera_planar_position);
  95. if (std::fabs(math::length_squared(camera_planar_direction - pick_planar_direction) > 0.0001f))
  96. {
  97. pick_angle = std::acos(math::dot(camera_planar_direction, pick_planar_direction));
  98. if (math::dot(math::cross(camera_planar_direction, pick_planar_direction), float3{0, 1, 0}) < 0.0f)
  99. pick_angle = -pick_angle;
  100. }
  101. // Determine target hand angle
  102. hand_angle_spring.x1 = math::pi<float> - std::min<float>(0.5f, std::max<float>(-0.5f, ((mouse_position[0] / viewport[2]) - 0.5f) * 3.0f)) * math::pi<float>;
  103. // Solve springs
  104. solve_numeric_spring<float, float>(hand_angle_spring, dt);
  105. solve_numeric_spring<float3, float>(pick_spring, dt);
  106. // Move active tools to intersection location
  107. registry.view<tool_component, transform_component>().each(
  108. [&](auto entity, auto& tool, auto& transform)
  109. {
  110. /*
  111. if (registry.has<model_component>(entity))
  112. {
  113. }*/
  114. if (!tool.active)
  115. return;
  116. if (intersection)
  117. {
  118. transform.transform.translation = pick_spring.x0 + float3{0, tool.hover_distance, 0};
  119. }
  120. if (tool.heliotropic)
  121. {
  122. math::quaternion<float> solar_rotation = math::rotation(float3{0, -1, 0}, sun_direction);
  123. transform.transform.translation = pick_spring.x0 + solar_rotation * float3{0, tool.hover_distance, 0};
  124. // Interpolate between left and right hand
  125. math::quaternion<float> hand_rotation = math::angle_axis(orbit_cam->get_azimuth() + hand_angle_spring.x0, float3{0, 1, 0});
  126. transform.transform.rotation = solar_rotation * hand_rotation;
  127. }
  128. //math::quaternion<float> rotation = math::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0});
  129. //transform.transform.rotation = rotation;
  130. });
  131. was_pick_enabled = pick_enabled;
  132. }
  133. void tool_system::set_camera(const ::camera* camera)
  134. {
  135. this->camera = camera;
  136. }
  137. void tool_system::set_orbit_cam(const ::orbit_cam* orbit_cam)
  138. {
  139. this->orbit_cam = orbit_cam;
  140. }
  141. void tool_system::set_viewport(const float4& viewport)
  142. {
  143. this->viewport = viewport;
  144. }
  145. void tool_system::set_pick(bool enabled)
  146. {
  147. pick_enabled = enabled;
  148. }
  149. void tool_system::set_sun_direction(const float3& direction)
  150. {
  151. sun_direction = direction;
  152. }
  153. void tool_system::handle_event(const mouse_moved_event& event)
  154. {
  155. if (pick_enabled && was_pick_enabled)
  156. {
  157. mouse_position[0] = event.x;
  158. mouse_position[1] = event.y;
  159. }
  160. }
  161. void tool_system::handle_event(const window_resized_event& event)
  162. {
  163. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  164. }