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

128 lines
3.4 KiB

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