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

146 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 "entity/components/collision-component.hpp"
  21. #include "entity/components/tool-component.hpp"
  22. #include "entity/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 <vmq/vmq.hpp>
  28. using namespace vmq::operators;
  29. using namespace ecs;
  30. tool_system::tool_system(entt::registry& registry):
  31. entity_system(registry),
  32. camera(nullptr),
  33. orbit_cam(orbit_cam),
  34. viewport{0, 0, 0, 0},
  35. mouse_position{0, 0},
  36. pick_enabled(true)
  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 = vmq::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. vmq::transform<float> inverse_transform = vmq::inverse(transform.transform);
  55. float3 origin = inverse_transform * pick_origin;
  56. float3 direction = vmq::normalize(vmq::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 = vmq::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 = vmq::normalize(camera_planar_focal_point - camera_planar_position);
  83. if (std::fabs(vmq::length_squared(camera_planar_direction - pick_planar_direction) > 0.0001f))
  84. {
  85. pick_angle = std::acos(vmq::dot(camera_planar_direction, pick_planar_direction));
  86. if (vmq::dot(vmq::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;
  98. }
  99. vmq::quaternion<float> rotation = vmq::angle_axis(orbit_cam->get_azimuth() + pick_angle, float3{0, 1, 0});
  100. transform.transform.rotation = rotation;
  101. });
  102. }
  103. void tool_system::set_camera(const ::camera* camera)
  104. {
  105. this->camera = camera;
  106. }
  107. void tool_system::set_orbit_cam(const ::orbit_cam* orbit_cam)
  108. {
  109. this->orbit_cam = orbit_cam;
  110. }
  111. void tool_system::set_viewport(const float4& viewport)
  112. {
  113. this->viewport = viewport;
  114. }
  115. void tool_system::set_pick(bool enabled)
  116. {
  117. pick_enabled = enabled;
  118. }
  119. void tool_system::handle_event(const mouse_moved_event& event)
  120. {
  121. if (pick_enabled)
  122. {
  123. mouse_position[0] = event.x;
  124. mouse_position[1] = event.y;
  125. }
  126. }