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

188 lines
5.9 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/camera-subject-component.hpp"
  21. #include "game/components/transform-component.hpp"
  22. #include "animation/spring.hpp"
  23. #include "scene/camera.hpp"
  24. #include "math/math.hpp"
  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. void camera_system::update(double t, double dt)
  35. {
  36. if (!camera)
  37. return;
  38. // Determine focal point
  39. int subject_count = 0;
  40. float3 focal_point = {0, 0, 0};
  41. registry.view<camera_subject_component, transform_component>().each(
  42. [&](auto entity, auto& subject, auto& transform)
  43. {
  44. focal_point += transform.transform.translation;
  45. ++subject_count;
  46. });
  47. if (subject_count > 1)
  48. focal_point /= static_cast<float>(subject_count);
  49. // Determine focal distance
  50. float focal_distance = math::log_lerp<float>(focal_distance_far, focal_distance_near, zoom_factor);
  51. // Determine view point
  52. quaternion_type rotation = math::normalize(azimuth_rotation * elevation_rotation);
  53. float3 view_point = focal_point + rotation * float3{0.0f, 0.0f, focal_distance};
  54. // Update camera transform
  55. transform_type source_transform = camera->get_transform();
  56. transform_type target_transform = math::identity_transform<float>;
  57. target_transform.translation = view_point;
  58. target_transform.rotation = rotation;
  59. float2 xz_direction = math::normalize(math::swizzle<0, 2>(focal_point) - math::swizzle<0, 2>(source_transform.translation));
  60. float source_azimuth = math::wrap_radians(std::atan2(-xz_direction.y, xz_direction.x) - math::half_pi<float>);
  61. float source_elevation = elevation;
  62. float smooth_factor = 0.1f;
  63. float smooth_azimuth = math::lerp_angle(source_azimuth, azimuth, smooth_factor);
  64. float smooth_elevation = math::lerp_angle(source_elevation, elevation, smooth_factor);
  65. smooth_azimuth = source_azimuth;
  66. float shortest_angle = math::wrap_radians(azimuth - source_azimuth);
  67. static float velocity = 0.0f;
  68. spring<float, float>(smooth_azimuth, velocity, smooth_azimuth + shortest_angle, 1.0f, 2.0f * math::two_pi<float>, dt);
  69. quaternion_type smooth_azimuth_rotation = math::angle_axis(smooth_azimuth, float3{0.0f, 1.0f, 0.0f});
  70. quaternion_type smooth_elevation_rotation = math::angle_axis(smooth_elevation, float3{-1.0f, 0.0f, 0.0f});
  71. quaternion_type smooth_rotation = math::normalize(smooth_azimuth_rotation * smooth_elevation_rotation);
  72. float3 smooth_view_point = focal_point + smooth_rotation * float3{0.0f, 0.0f, focal_distance};
  73. transform_type smooth_transform;
  74. smooth_transform.translation = smooth_view_point;
  75. //smooth_transform.translation = math::lerp(source_transform.translation, target_transform.translation, smooth_factor);
  76. //smooth_transform.rotation = math::slerp(source_transform.rotation, target_transform.rotation, smooth_factor);
  77. smooth_transform.rotation = smooth_rotation;
  78. smooth_transform.scale = math::lerp(source_transform.scale, target_transform.scale, smooth_factor);
  79. camera->set_transform(smooth_transform);
  80. // Determine FOV
  81. float fov = math::log_lerp<float>(fov_far, fov_near, zoom_factor);
  82. // Determine aspect ratio
  83. float aspect_ratio = viewport[2] / viewport[3];
  84. // Determine clipping planes
  85. float clip_near = math::log_lerp<float>(near_clip_far, near_clip_near, zoom_factor);
  86. float clip_far = math::log_lerp<float>(far_clip_far, far_clip_near, zoom_factor);
  87. // Update camera projection
  88. camera->set_perspective(fov, aspect_ratio, clip_near, clip_far);
  89. }
  90. void camera_system::rotate(float angle)
  91. {
  92. set_azimuth(azimuth + angle);
  93. }
  94. void camera_system::tilt(float angle)
  95. {
  96. set_elevation(elevation + angle);
  97. }
  98. void camera_system::zoom(float factor)
  99. {
  100. set_zoom(std::max<float>(0.0f, std::min<float>(1.0f, zoom_factor + factor)));
  101. }
  102. void camera_system::set_camera(::camera* camera)
  103. {
  104. this->camera = camera;
  105. }
  106. void camera_system::set_viewport(const float4& viewport)
  107. {
  108. this->viewport = viewport;
  109. }
  110. void camera_system::set_azimuth(float angle)
  111. {
  112. azimuth = math::wrap_radians(angle);
  113. azimuth_rotation = math::angle_axis(azimuth, float3{0.0f, 1.0f, 0.0f});
  114. }
  115. void camera_system::set_elevation(float angle)
  116. {
  117. elevation = math::wrap_radians(angle);
  118. elevation_rotation = math::angle_axis(elevation, float3{-1.0f, 0.0f, 0.0f});
  119. }
  120. void camera_system::set_zoom(float factor)
  121. {
  122. this->zoom_factor = factor;
  123. }
  124. void camera_system::set_focal_distance(float distance_near, float distance_far)
  125. {
  126. focal_distance_near = distance_near;
  127. focal_distance_far = distance_far;
  128. }
  129. void camera_system::set_fov(float angle_near, float angle_far)
  130. {
  131. fov_near = angle_near;
  132. fov_far = angle_far;
  133. }
  134. void camera_system::set_clip_near(float distance_near, float distance_far)
  135. {
  136. near_clip_near = distance_near;
  137. near_clip_far = distance_far;
  138. }
  139. void camera_system::set_clip_far(float distance_near, float distance_far)
  140. {
  141. far_clip_near = distance_near;
  142. far_clip_far = distance_far;
  143. }
  144. void camera_system::handle_event(const mouse_moved_event& event)
  145. {
  146. mouse_position[0] = event.x;
  147. mouse_position[1] = event.y;
  148. }
  149. void camera_system::handle_event(const window_resized_event& event)
  150. {
  151. set_viewport({0.0f, 0.0f, static_cast<float>(event.w), static_cast<float>(event.h)});
  152. }