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

275 lines
10 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/apparent-size.hpp"
  21. #include "ecs/components/celestial-body-component.hpp"
  22. #include "ecs/components/transform-component.hpp"
  23. #include "renderer/passes/sky-pass.hpp"
  24. #include "color/color.hpp"
  25. #include "physics/orbit/orbit.hpp"
  26. #include "physics/time/ut1.hpp"
  27. #include "geom/cartesian.hpp"
  28. #include <iostream>
  29. namespace ecs {
  30. static constexpr double seconds_per_day = 24.0 * 60.0 * 60.0;
  31. astronomy_system::astronomy_system(ecs::registry& registry):
  32. entity_system(registry),
  33. universal_time(0.0),
  34. days_per_timestep(1.0 / seconds_per_day),
  35. observer_location{0.0, 0.0, 0.0},
  36. lst(0.0),
  37. obliquity(0.0),
  38. axial_rotation(0.0),
  39. axial_rotation_at_epoch(0.0),
  40. axial_rotation_speed(0.0),
  41. sky_pass(nullptr),
  42. sun_light(nullptr)
  43. {}
  44. void astronomy_system::update(double t, double dt)
  45. {
  46. // Add scaled timestep to current time
  47. set_universal_time(universal_time + dt * days_per_timestep);
  48. set_universal_time(0.0);
  49. // Update horizontal (topocentric) positions of intrasolar celestial bodies
  50. registry.view<celestial_body_component, transform_component>().each(
  51. [&](ecs::entity entity, auto& body, auto& transform)
  52. {
  53. double time_correction = observer_location[2] / (math::two_pi<double> / 24.0);
  54. double local_jd = universal_time + time_correction / 24.0 - 0.5;
  55. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  56. double local_lst = local_time / 24.0f * math::two_pi<float>;
  57. // Transform orbital position from ecliptic space to horizontal space
  58. //double3 horizontal = ecliptic_to_horizontal * body.orbital_state.r;
  59. double3 horizontal = ecliptic_to_horizontal * double3{1, 0, 0};
  60. // Subtract observer's radial distance (planet radius + observer's altitude)
  61. //horizontal.z -= observer_location[0];
  62. // Convert Cartesian horizontal coordinates to spherical
  63. double3 spherical = geom::cartesian::to_spherical(horizontal);
  64. // Find angular radius
  65. double angular_radius = astro::find_angular_radius(body.radius, spherical[0]);
  66. // Transform into local coordinates
  67. const double3x3 horizontal_to_local = math::rotate_x(-math::half_pi<double>) * math::rotate_z(-math::half_pi<double>);
  68. double3 translation = horizontal_to_local * horizontal;
  69. double3x3 rotation = horizontal_to_local * ecliptic_to_horizontal;
  70. // Set local transform of transform component
  71. transform.local.translation = math::type_cast<float>(translation);
  72. transform.local.rotation = math::normalize(math::type_cast<float>(math::quaternion_cast(rotation)));
  73. transform.local.scale = math::type_cast<float>(double3{body.radius, body.radius, body.radius});
  74. if (sun_light != nullptr)
  75. {
  76. const double universal_time_cy = universal_time * 2.7397e-5;
  77. const double3 solar_system_barycenter = {0, 0, 0};
  78. physics::orbit::elements<double> earth_elements;
  79. earth_elements.a = 1.00000261 + 0.00000562 * universal_time_cy;
  80. earth_elements.e = 0.01671123 + -0.00004392 * universal_time_cy;
  81. earth_elements.i = math::radians(-0.00001531) + math::radians(-0.01294668) * universal_time_cy;
  82. earth_elements.raan = 0.0;
  83. const double earth_elements_mean_longitude = math::radians(100.46457166) + math::radians(35999.37244981) * universal_time_cy;
  84. const double earth_elements_longitude_perihelion = math::radians(102.93768193) + math::radians(0.32327364) * universal_time_cy;
  85. earth_elements.w = earth_elements_longitude_perihelion - earth_elements.raan;
  86. earth_elements.ta = earth_elements_mean_longitude - earth_elements_longitude_perihelion;
  87. // Calculate semi-minor axis, b
  88. double b = physics::orbit::derive_semiminor_axis(earth_elements.a, earth_elements.e);
  89. // Solve Kepler's equation for eccentric anomaly (E)
  90. double ea = physics::orbit::kepler_ea(earth_elements.e, earth_elements.ta, 10, 1e-6);
  91. // Calculate radial distance, r; and true anomaly, v
  92. double xv = earth_elements.a * (std::cos(ea) - earth_elements.e);
  93. double yv = b * std::sin(ea);
  94. double r = std::sqrt(xv * xv + yv * yv);
  95. double ta = std::atan2(yv, xv);
  96. // Position of the body in perifocal space
  97. const math::vector3<double> earth_position_pqw = math::quaternion<double>::rotate_z(ta) * math::vector3<double>{r, 0, 0};
  98. const double earth_axial_tilt = math::radians(23.45);
  99. const double earth_axial_rotation = physics::time::ut1::era(universal_time);
  100. const double earth_radius_au = 4.2635e-5;
  101. const double observer_altitude = earth_radius_au;
  102. const double observer_latitude = math::radians(0.0);
  103. const double observer_longitude = math::radians(0.0);
  104. const physics::frame<double> earth_inertial_to_pqw = physics::orbit::inertial::to_perifocal(solar_system_barycenter, earth_elements.raan, earth_elements.i, earth_elements.w);
  105. const math::vector3<double> earth_position_inertial = earth_inertial_to_pqw.inverse() * earth_position_pqw;
  106. const math::vector3<double> sun_position_intertial = math::vector3<double>{0, 0, 0};
  107. const physics::frame<double> earth_inertial_to_bci = physics::orbit::inertial::to_bci(earth_position_inertial, earth_elements.i, earth_axial_tilt);
  108. const physics::frame<double> earth_inertial_to_bcbf = physics::orbit::inertial::to_bcbf(earth_position_inertial, earth_elements.i, earth_axial_tilt, earth_axial_rotation);
  109. const physics::frame<double> earth_bcbf_to_topo = physics::orbit::bcbf::to_topocentric(observer_altitude, observer_latitude, observer_longitude);
  110. const math::vector3<double> sun_position_earth_bci = earth_inertial_to_bci * sun_position_intertial;
  111. const math::vector3<double> sun_position_earth_bcbf = earth_inertial_to_bcbf * sun_position_intertial;
  112. const math::vector3<double> sun_position_earth_topo = earth_bcbf_to_topo * sun_position_earth_bcbf;
  113. const math::vector3<double> sun_radec = geom::cartesian::to_spherical(sun_position_earth_bci);
  114. const math::vector3<double> sun_azel = geom::cartesian::to_spherical(sun_position_earth_topo);
  115. const double sun_az = sun_azel.z;
  116. const double sun_el = sun_azel.y;
  117. double sun_ra = sun_radec.z;
  118. const double sun_dec = sun_radec.y;
  119. if (sun_ra < 0.0)
  120. sun_ra += math::two_pi<double>;
  121. std::cout << "ra: " << (sun_ra / math::two_pi<double> * 24.0) << "; dec: " << math::degrees(sun_dec) << std::endl;
  122. std::cout << "az: " << math::degrees(math::pi<double> - sun_az) << "; el: " << math::degrees(sun_el) << std::endl;
  123. float az = spherical.z;
  124. float el = spherical.y;
  125. if (az < 0.0f)
  126. az += math::two_pi<float>;
  127. //std::cout << "local: " << translation << std::endl;
  128. //std::cout << "az: " << math::degrees(az) << "; ";
  129. //std::cout << "el: " << math::degrees(el) << std::endl;
  130. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(static_cast<float>(spherical.z), float3{0, 1, 0});
  131. math::quaternion<float> sun_elevation_rotation = math::angle_axis(static_cast<float>(spherical.y), float3{1, 0, 0});
  132. math::quaternion<float> sun_az_el_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  133. // Set sun color
  134. float cct = 3000.0f + std::sin(spherical.y) * 5000.0f;
  135. float3 color_xyz = color::cct::to_xyz(cct);
  136. float3 color_acescg = color::xyz::to_acescg(color_xyz);
  137. sun_light->set_color(color_acescg);
  138. // Set sun intensity (in lux)
  139. float intensity = std::max(0.0, std::sin(spherical.y) * 108000.0f);
  140. sun_light->set_intensity(intensity);
  141. //sun_light->set_translation({0, 500, 0});
  142. sun_light->set_translation(transform.local.translation);
  143. //sun_light->set_rotation(transform.local.rotation);
  144. //sun_light->set_rotation(sun_az_el_rotation);
  145. //sun_light->set_rotation(sun_elevation_rotation);
  146. sun_light->set_rotation(math::look_rotation(math::normalize(-transform.local.translation), {0, 0, -1}));
  147. if (this->sky_pass)
  148. {
  149. this->sky_pass->set_sun_coordinates(transform.local.rotation * float3{0, 0, -1}, {static_cast<float>(spherical.z), static_cast<float>(spherical.y)});
  150. }
  151. }
  152. });
  153. if (sky_pass)
  154. {
  155. // Calculate local time
  156. double time_correction = observer_location[2] / (math::two_pi<double> / 24.0);
  157. double local_jd = universal_time + time_correction / 24.0 - 0.5;
  158. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  159. sky_pass->set_time_of_day(local_time);
  160. }
  161. }
  162. void astronomy_system::set_universal_time(double time)
  163. {
  164. universal_time = time;
  165. update_axial_rotation();
  166. }
  167. void astronomy_system::set_time_scale(double scale)
  168. {
  169. days_per_timestep = scale / seconds_per_day;
  170. }
  171. void astronomy_system::set_observer_location(const double3& location)
  172. {
  173. observer_location = location;
  174. update_sidereal_time();
  175. }
  176. void astronomy_system::set_obliquity(double angle)
  177. {
  178. obliquity = angle;
  179. update_ecliptic_to_horizontal();
  180. }
  181. void astronomy_system::set_axial_rotation_speed(double speed)
  182. {
  183. axial_rotation_speed = speed;
  184. update_axial_rotation();
  185. }
  186. void astronomy_system::set_axial_rotation_at_epoch(double angle)
  187. {
  188. axial_rotation_at_epoch = angle;
  189. update_axial_rotation();
  190. }
  191. void astronomy_system::set_sky_pass(::sky_pass* pass)
  192. {
  193. sky_pass = pass;
  194. }
  195. void astronomy_system::set_sun_light(scene::directional_light* light)
  196. {
  197. sun_light = light;
  198. }
  199. void astronomy_system::update_axial_rotation()
  200. {
  201. axial_rotation = math::wrap_radians<double>(axial_rotation_at_epoch + universal_time * axial_rotation_speed);
  202. update_sidereal_time();
  203. }
  204. void astronomy_system::update_sidereal_time()
  205. {
  206. lst = math::wrap_radians<double>(axial_rotation + observer_location[2]);
  207. update_ecliptic_to_horizontal();
  208. }
  209. void astronomy_system::update_ecliptic_to_horizontal()
  210. {
  211. //ecliptic_to_horizontal = coordinates::rectangular::ecliptic::to_horizontal(obliquity, observer_location[1], lst);
  212. }
  213. } // namespace ecs