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

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