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

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