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

125 lines
3.4 KiB

3 years ago
3 years ago
  1. /*
  2. * Copyright (C) 2021 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/camera-follow-component.hpp"
  21. #include "game/components/transform-component.hpp"
  22. #include "math/math.hpp"
  23. #include <algorithm>
  24. #include <cmath>
  25. using namespace ecs;
  26. camera_system::camera_system(entt::registry& registry):
  27. entity_system(registry),
  28. camera(nullptr),
  29. viewport{0, 0, 0, 0},
  30. mouse_position{0, 0}
  31. {
  32. orbit_cam.set_elevation_limits({math::radians(5.0f), math::radians(89.0f)});
  33. //orbit_cam.set_elevation_limits({math::radians(-89.0f), math::radians(89.0f)});
  34. orbit_cam.set_focal_distance_limits({2.0f, 200.0f});
  35. orbit_cam.set_fov_limits({math::radians(80.0f), math::radians(35.0f)});
  36. orbit_cam.set_clip_near_limits({0.1f, 5.0f});
  37. orbit_cam.set_clip_far_limits({100.0f, 5000.0f});
  38. orbit_cam.set_target_focal_point({0.0f, 0.0f, 0.0f});
  39. orbit_cam.set_target_azimuth(0.0f);
  40. orbit_cam.set_target_elevation(math::radians(45.0f));
  41. orbit_cam.set_target_zoom(0.0f);
  42. orbit_cam.set_focal_point_oscillation(hz_to_rads(8.0f));
  43. orbit_cam.set_azimuth_oscillation(hz_to_rads(2.0f));
  44. orbit_cam.set_elevation_oscillation(hz_to_rads(2.0f));
  45. orbit_cam.set_zoom_oscillation(hz_to_rads(5.0f));
  46. orbit_cam.reset_springs();
  47. }
  48. void camera_system::update(double t, double dt)
  49. {
  50. if (!camera)
  51. return;
  52. // Determine target focal point
  53. int subject_count = 0;
  54. float3 target_focal_point = {0, 0, 0};
  55. registry.view<camera_follow_component, transform_component>().each(
  56. [&](auto entity, auto& follow, auto& transform)
  57. {
  58. target_focal_point += transform.local.translation;
  59. ++subject_count;
  60. });
  61. if (subject_count > 1)
  62. target_focal_point /= static_cast<float>(subject_count);
  63. // Focus at ant's head height off the ground.
  64. target_focal_point.y += 0.2f;
  65. // Check for collision with environment
  66. //...
  67. orbit_cam.set_target_focal_point(target_focal_point);
  68. orbit_cam.update(static_cast<float>(dt));
  69. }
  70. void camera_system::pan(float angle)
  71. {
  72. orbit_cam.pan(angle);
  73. }
  74. void camera_system::tilt(float angle)
  75. {
  76. orbit_cam.tilt(angle);
  77. }
  78. void camera_system::zoom(float factor)
  79. {
  80. orbit_cam.zoom(factor);
  81. }
  82. void camera_system::set_camera(scene::camera* camera)
  83. {
  84. this->camera = camera;
  85. if (camera)
  86. {
  87. orbit_cam.attach(camera);
  88. }
  89. else
  90. {
  91. orbit_cam.detach();
  92. }
  93. }
  94. void camera_system::set_viewport(const float4& viewport)
  95. {
  96. this->viewport = viewport;
  97. orbit_cam.set_aspect_ratio(viewport[2] / viewport[3]);
  98. }
  99. void camera_system::handle_event(const mouse_moved_event& event)
  100. {
  101. mouse_position[0] = event.x;
  102. mouse_position[1] = event.y;
  103. }
  104. void camera_system::handle_event(const window_resized_event& event)
  105. {
  106. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  107. }