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

123 lines
3.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 "camera-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. camera_system::camera_system(entt::registry& registry):
  30. entity_system(registry),
  31. orbit_cam(nullptr),
  32. viewport{0, 0, 0, 0},
  33. mouse_position{0, 0}
  34. {}
  35. void camera_system::update(double t, double dt)
  36. {
  37. if (!orbit_cam)
  38. return;
  39. const camera* camera = orbit_cam->get_camera();
  40. if (!camera)
  41. return;
  42. /*
  43. registry.view<transform_component, tool_component>().each(
  44. [&](auto entity, auto& transform, auto& tool)
  45. {
  46. if (!tool.active)
  47. return;
  48. float3 screen_position = camera->project(transform.transform.translation, viewport);
  49. std::cout << screen_position << std::endl;
  50. // Project tool position onto viewport
  51. });
  52. */
  53. // Cast a ray straight down from infinity at the focal point's lateral coordinates.
  54. // std::numeric_limits<float>::infinity() actually doesn't work here
  55. float3 focal_point = orbit_cam->get_target_focal_point();
  56. float3 pick_origin = float3{focal_point.x, focal_point.y + 500.0f, focal_point.z};
  57. float3 pick_direction = float3{0, -1, 0};
  58. ray<float> picking_ray = {pick_origin, pick_direction};
  59. float a = std::numeric_limits<float>::infinity();
  60. bool intersection = false;
  61. float3 pick;
  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 = ray_mesh_intersection(transformed_ray, *collision.mesh);
  77. if (std::get<0>(mesh_result))
  78. {
  79. intersection = true;
  80. if (std::get<1>(mesh_result) < a)
  81. {
  82. a = std::get<1>(mesh_result);
  83. pick = picking_ray.extrapolate(a);
  84. }
  85. }
  86. });
  87. if (intersection)
  88. {
  89. //orbit_cam->set_target_focal_point(pick);
  90. }
  91. }
  92. void camera_system::set_orbit_cam(::orbit_cam* orbit_cam)
  93. {
  94. this->orbit_cam = orbit_cam;
  95. }
  96. void camera_system::set_viewport(const float4& viewport)
  97. {
  98. this->viewport = viewport;
  99. }
  100. void camera_system::handle_event(const mouse_moved_event& event)
  101. {
  102. mouse_position[0] = event.x;
  103. mouse_position[1] = event.y;
  104. }