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

144 lines
4.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 "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 "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. {}
  37. void tool_system::update(double t, double dt)
  38. {
  39. if (!camera)
  40. return;
  41. float3 pick_near = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 0.0f}, viewport);
  42. float3 pick_far = camera->unproject({mouse_position[0], viewport[3] - mouse_position[1], 1.0f}, viewport);
  43. float3 pick_origin = pick_near;
  44. float3 pick_direction = math::normalize(pick_far - pick_near);
  45. ray<float> picking_ray = {pick_near, pick_direction};
  46. float a = std::numeric_limits<float>::infinity();
  47. bool intersection = false;
  48. float3 pick;
  49. // Cast ray from cursor to collision components to find closest intersection
  50. registry.view<transform_component, collision_component>().each(
  51. [&](auto entity, auto& transform, auto& collision)
  52. {
  53. math::transform<float> inverse_transform = math::inverse(transform.transform);
  54. float3 origin = inverse_transform * pick_origin;
  55. float3 direction = math::normalize(math::conjugate(transform.transform.rotation) * pick_direction);
  56. ray<float> transformed_ray = {origin, direction};
  57. // Broad phase AABB test
  58. auto aabb_result = ray_aabb_intersection(transformed_ray, collision.bounds);
  59. if (!std::get<0>(aabb_result))
  60. {
  61. return;
  62. }
  63. // Narrow phase mesh test
  64. auto mesh_result = collision.mesh_accelerator.query_nearest(transformed_ray);
  65. if (mesh_result)
  66. {
  67. intersection = true;
  68. if (mesh_result->t < a)
  69. {
  70. a = mesh_result->t;
  71. pick = picking_ray.extrapolate(a);
  72. }
  73. }
  74. });
  75. const float3& camera_position = camera->get_translation();
  76. float3 pick_planar_position = float3{pick.x, 0, pick.z};
  77. float3 camera_planar_position = float3{camera_position.x, 0, camera_position.z};
  78. float pick_angle = 0.0f;
  79. float3 pick_planar_direction = math::normalize(pick_planar_position - camera_planar_position);
  80. float3 camera_planar_focal_point = float3{orbit_cam->get_focal_point().x, 0, orbit_cam->get_focal_point().z};
  81. float3 camera_planar_direction = math::normalize(camera_planar_focal_point - camera_planar_position);
  82. if (std::fabs(math::length_squared(camera_planar_direction - pick_planar_direction) > 0.0001f))
  83. {
  84. pick_angle = std::acos(math::dot(camera_planar_direction, pick_planar_direction));
  85. if (math::dot(math::cross(camera_planar_direction, pick_planar_direction), float3{0, 1, 0}) < 0.0f)
  86. pick_angle = -pick_angle;
  87. }
  88. // Move active tools to intersection location
  89. registry.view<tool_component, transform_component>().each(
  90. [&](auto entity, auto& tool, auto& transform)
  91. {
  92. if (!tool.active)
  93. return;
  94. if (intersection)
  95. {
  96. transform.transform.translation = pick;
  97. }
  98. math::quaternion<float> rotation = math::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0});
  99. transform.transform.rotation = rotation;
  100. });
  101. }
  102. void tool_system::set_camera(const ::camera* camera)
  103. {
  104. this->camera = camera;
  105. }
  106. void tool_system::set_orbit_cam(const ::orbit_cam* orbit_cam)
  107. {
  108. this->orbit_cam = orbit_cam;
  109. }
  110. void tool_system::set_viewport(const float4& viewport)
  111. {
  112. this->viewport = viewport;
  113. }
  114. void tool_system::set_pick(bool enabled)
  115. {
  116. pick_enabled = enabled;
  117. }
  118. void tool_system::handle_event(const mouse_moved_event& event)
  119. {
  120. if (pick_enabled)
  121. {
  122. mouse_position[0] = event.x;
  123. mouse_position[1] = event.y;
  124. }
  125. }