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

192 lines
5.9 KiB

  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 "ecs/systems/astronomy-system.hpp"
  20. #include "coordinates/coordinates.hpp"
  21. #include "astro/apparent-size.hpp"
  22. #include "ecs/components/celestial-body-component.hpp"
  23. #include "ecs/components/transform-component.hpp"
  24. #include "renderer/passes/sky-pass.hpp"
  25. #include "color/color.hpp"
  26. #include <iostream>
  27. namespace ecs {
  28. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  29. astronomy_system::astronomy_system(ecs::registry& registry):
  30. entity_system(registry),
  31. universal_time(0.0),
  32. days_per_timestep(1.0 / seconds_per_day),
  33. observer_location{0.0, 0.0, 0.0},
  34. lst(0.0),
  35. obliquity(0.0),
  36. axial_rotation(0.0),
  37. axial_rotation_at_epoch(0.0),
  38. axial_rotation_speed(0.0),
  39. sky_pass(nullptr),
  40. sun_light(nullptr)
  41. {}
  42. void astronomy_system::update(double t, double dt)
  43. {
  44. // Add scaled timestep to current time
  45. set_universal_time(universal_time + dt * days_per_timestep);
  46. // Update horizontal (topocentric) positions of intrasolar celestial bodies
  47. registry.view<celestial_body_component, transform_component>().each(
  48. [&](ecs::entity entity, auto& body, auto& transform)
  49. {
  50. // Transform orbital position from ecliptic space to horizontal space
  51. double3 horizontal = ecliptic_to_horizontal * body.orbital_state.r;
  52. // Subtract observer's radial distance (planet radius + observer's altitude)
  53. horizontal.z -= observer_location[0];
  54. // Convert rectangular horizontal coordinates to spherical
  55. double3 spherical = coordinates::rectangular::to_spherical(horizontal);
  56. // Find angular radius
  57. double angular_radius = astro::find_angular_radius(body.radius, spherical[0]);
  58. // Transform into local coordinates
  59. const double3x3 horizontal_to_local =
  60. {
  61. 0.0, 0.0, -1.0,
  62. 1.0, 0.0, 0.0,
  63. 0.0, 1.0, 0.0
  64. };
  65. double3 translation = horizontal_to_local * horizontal;
  66. double3x3 rotation = horizontal_to_local * ecliptic_to_horizontal;
  67. // Set local transform of transform component
  68. transform.local.translation = math::type_cast<float>(translation);
  69. transform.local.rotation = math::type_cast<float>(math::quaternion_cast(rotation));
  70. transform.local.scale = math::type_cast<float>(double3{body.radius, body.radius, body.radius});
  71. if (sun_light != nullptr)
  72. {
  73. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(static_cast<float>(spherical.z), float3{0, 1, 0});
  74. math::quaternion<float> sun_elevation_rotation = math::angle_axis(static_cast<float>(spherical.y), float3{-1, 0, 0});
  75. math::quaternion<float> sun_az_el_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  76. //sun_az_el_rotation = math::angle_axis((float)universal_time * math::two_pi<float>, float3{1, 0, 0});
  77. //
  78. //sun_light->look_at({0, 0, 0}, {0, -1, 0}, {0, 0, 1});
  79. // Set sun color
  80. float cct = 3000.0f + std::sin(spherical.y) * 5000.0f;
  81. float3 color_xyz = color::cct::to_xyz(cct);
  82. float3 color_acescg = color::xyz::to_acescg(color_xyz);
  83. sun_light->set_color(color_acescg);
  84. // Set sun intensity (in lux)
  85. float intensity = std::max(0.0, std::sin(spherical.y) * 108000.0f);
  86. sun_light->set_intensity(intensity);
  87. sun_light->set_translation({0, 500, 0});
  88. //sun_light->set_rotation(math::look_rotation(math::normalize(transform.local.translation), {0, 1, 0}));
  89. sun_light->set_rotation(sun_az_el_rotation);
  90. //sun_light->set_rotation(sun_elevation_rotation);
  91. if (this->sky_pass)
  92. {
  93. this->sky_pass->set_sun_coordinates(sun_az_el_rotation * float3{0, 0, -1}, {static_cast<float>(spherical.z), static_cast<float>(spherical.y)});
  94. }
  95. }
  96. });
  97. if (sky_pass)
  98. {
  99. // Calculate local time
  100. double time_correction = observer_location[2] / (math::two_pi<double> / 24.0);
  101. double local_jd = universal_time + time_correction / 24.0 - 0.5;
  102. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  103. sky_pass->set_time_of_day(local_time);
  104. }
  105. }
  106. void astronomy_system::set_universal_time(double time)
  107. {
  108. universal_time = time;
  109. update_axial_rotation();
  110. }
  111. void astronomy_system::set_time_scale(double scale)
  112. {
  113. days_per_timestep = scale / seconds_per_day;
  114. }
  115. void astronomy_system::set_observer_location(const double3& location)
  116. {
  117. observer_location = location;
  118. update_sidereal_time();
  119. }
  120. void astronomy_system::set_obliquity(double angle)
  121. {
  122. obliquity = angle;
  123. update_ecliptic_to_horizontal();
  124. }
  125. void astronomy_system::set_axial_rotation_speed(double speed)
  126. {
  127. axial_rotation_speed = speed;
  128. update_axial_rotation();
  129. }
  130. void astronomy_system::set_axial_rotation_at_epoch(double angle)
  131. {
  132. axial_rotation_at_epoch = angle;
  133. update_axial_rotation();
  134. }
  135. void astronomy_system::set_sky_pass(::sky_pass* pass)
  136. {
  137. sky_pass = pass;
  138. }
  139. void astronomy_system::set_sun_light(scene::directional_light* light)
  140. {
  141. sun_light = light;
  142. }
  143. void astronomy_system::update_axial_rotation()
  144. {
  145. axial_rotation = math::wrap_radians<double>(axial_rotation_at_epoch + universal_time * axial_rotation_speed);
  146. update_sidereal_time();
  147. }
  148. void astronomy_system::update_sidereal_time()
  149. {
  150. lst = math::wrap_radians<double>(axial_rotation + observer_location[2]);
  151. update_ecliptic_to_horizontal();
  152. }
  153. void astronomy_system::update_ecliptic_to_horizontal()
  154. {
  155. ecliptic_to_horizontal = coordinates::rectangular::ecliptic::to_horizontal(obliquity, observer_location[1], lst);
  156. }
  157. } // namespace ecs