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

184 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 "astro/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 "astro/blackbody.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 = astro::rectangular_to_spherical(horizontal);
  56. spherical.z -= math::pi<double>;
  57. // Find angular radius
  58. double angular_radius = astro::find_angular_radius(body.radius, spherical.x);
  59. // Transform into local right-handed coordinates
  60. double3 translation = astro::horizontal_to_right_handed * horizontal;
  61. double3x3 rotation = astro::horizontal_to_right_handed * ecliptic_to_horizontal;
  62. // Set local transform of transform component
  63. transform.local.translation = math::type_cast<float>(translation);
  64. transform.local.rotation = math::type_cast<float>(math::quaternion_cast(rotation));
  65. transform.local.scale = math::type_cast<float>(double3{body.radius, body.radius, body.radius});
  66. if (sun_light != nullptr)
  67. {
  68. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(static_cast<float>(spherical.z), float3{0, 1, 0});
  69. math::quaternion<float> sun_elevation_rotation = math::angle_axis(static_cast<float>(spherical.y), float3{-1, 0, 0});
  70. math::quaternion<float> sun_az_el_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  71. //sun_az_el_rotation = math::angle_axis((float)universal_time * math::two_pi<float>, float3{1, 0, 0});
  72. //
  73. //sun_light->look_at({0, 0, 0}, {0, -1, 0}, {0, 0, 1});
  74. // Set sun color
  75. float correlated_temperature = 3000.0f + std::sin(spherical.y) * 5000.0f;
  76. float3 correlated_color = math::type_cast<float>(astro::blackbody(correlated_temperature));
  77. sun_light->set_color(correlated_color);
  78. // Set sun intensity (in lux)
  79. float intensity = std::max(0.0, std::sin(spherical.y) * 108000.0f);
  80. sun_light->set_intensity(intensity);
  81. sun_light->set_translation({0, 500, 0});
  82. //sun_light->set_rotation(math::look_rotation(math::normalize(transform.local.translation), {0, 1, 0}));
  83. sun_light->set_rotation(sun_az_el_rotation);
  84. //sun_light->set_rotation(sun_elevation_rotation);
  85. if (this->sky_pass)
  86. {
  87. this->sky_pass->set_sun_coordinates(sun_az_el_rotation * float3{0, 0, -1}, {static_cast<float>(spherical.z), static_cast<float>(spherical.y)});
  88. }
  89. }
  90. });
  91. if (sky_pass)
  92. {
  93. // Calculate local time
  94. double time_correction = observer_location[2] / (math::two_pi<double> / 24.0);
  95. double local_jd = universal_time + time_correction / 24.0 - 0.5;
  96. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  97. sky_pass->set_time_of_day(local_time);
  98. }
  99. }
  100. void astronomy_system::set_universal_time(double time)
  101. {
  102. universal_time = time;
  103. update_axial_rotation();
  104. }
  105. void astronomy_system::set_time_scale(double scale)
  106. {
  107. days_per_timestep = scale / seconds_per_day;
  108. }
  109. void astronomy_system::set_observer_location(const double3& location)
  110. {
  111. observer_location = location;
  112. update_sidereal_time();
  113. }
  114. void astronomy_system::set_obliquity(double angle)
  115. {
  116. obliquity = angle;
  117. update_ecliptic_to_horizontal();
  118. }
  119. void astronomy_system::set_axial_rotation_speed(double speed)
  120. {
  121. axial_rotation_speed = speed;
  122. update_axial_rotation();
  123. }
  124. void astronomy_system::set_axial_rotation_at_epoch(double angle)
  125. {
  126. axial_rotation_at_epoch = angle;
  127. update_axial_rotation();
  128. }
  129. void astronomy_system::set_sky_pass(::sky_pass* pass)
  130. {
  131. sky_pass = pass;
  132. }
  133. void astronomy_system::set_sun_light(scene::directional_light* light)
  134. {
  135. sun_light = light;
  136. }
  137. void astronomy_system::update_axial_rotation()
  138. {
  139. axial_rotation = math::wrap_radians<double>(axial_rotation_at_epoch + universal_time * axial_rotation_speed);
  140. update_sidereal_time();
  141. }
  142. void astronomy_system::update_sidereal_time()
  143. {
  144. lst = math::wrap_radians<double>(axial_rotation + observer_location[2]);
  145. update_ecliptic_to_horizontal();
  146. }
  147. void astronomy_system::update_ecliptic_to_horizontal()
  148. {
  149. ecliptic_to_horizontal = astro::ecliptic_to_horizontal(obliquity, observer_location[1], lst);
  150. }
  151. } // namespace ecs