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

272 lines
9.7 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/blackbody-component.hpp"
  22. #include "ecs/components/transform-component.hpp"
  23. #include "geom/intersection.hpp"
  24. #include "color/color.hpp"
  25. #include "physics/orbit/orbit.hpp"
  26. #include "physics/time/ut1.hpp"
  27. #include "physics/light/photometry.hpp"
  28. #include "physics/light/luminosity.hpp"
  29. #include "physics/light/refraction.hpp"
  30. #include "physics/atmosphere.hpp"
  31. #include "geom/cartesian.hpp"
  32. #include <iostream>
  33. namespace ecs {
  34. template <class T>
  35. math::vector3<T> transmittance(T depth_r, T depth_m, T depth_o, const math::vector3<T>& beta_r, const math::vector3<T>& beta_m)
  36. {
  37. math::vector3<T> transmittance_r = beta_r * depth_r;
  38. math::vector3<T> transmittance_m = beta_m * 1.1 * depth_m;
  39. math::vector3<T> transmittance_o = {0, 0, 0};
  40. math::vector3<T> t = transmittance_r + transmittance_m + transmittance_o;
  41. t.x = std::exp(-t.x);
  42. t.y = std::exp(-t.y);
  43. t.z = std::exp(-t.z);
  44. return t;
  45. }
  46. astronomy_system::astronomy_system(ecs::registry& registry):
  47. entity_system(registry),
  48. universal_time(0.0),
  49. time_scale(1.0),
  50. reference_entity(entt::null),
  51. reference_orbit(nullptr),
  52. reference_body(nullptr),
  53. reference_atmosphere(nullptr),
  54. sun_light(nullptr),
  55. sky_pass(nullptr)
  56. {}
  57. void astronomy_system::update(double t, double dt)
  58. {
  59. // Add scaled timestep to current time
  60. set_universal_time(universal_time + dt * time_scale);
  61. // Abort if either reference body or orbit have not been set
  62. if (!reference_orbit || !reference_body)
  63. return;
  64. // Determine axial rotation at current time
  65. const double reference_axial_rotation = reference_body->axial_rotation + reference_body->angular_frequency * universal_time;
  66. // Construct reference frame which transforms coordinates from inertial space to reference body BCBF space
  67. inertial_to_bcbf = physics::orbit::inertial::to_bcbf
  68. (
  69. reference_orbit->state.r,
  70. reference_orbit->elements.i,
  71. reference_body->axial_tilt,
  72. reference_axial_rotation
  73. );
  74. // Construct reference frame which transforms coordinates from inertial space to reference body topocentric space
  75. inertial_to_topocentric = inertial_to_bcbf * bcbf_to_topocentric;
  76. // Set the transform component translations of orbiting bodies to their topocentric positions
  77. registry.view<celestial_body_component, orbit_component, transform_component>().each(
  78. [&](ecs::entity entity, const auto& celestial_body, const auto& orbit, auto& transform)
  79. {
  80. // Transform Cartesian position vector (r) from inertial space to topocentric space
  81. const math::vector3<double> r_topocentric = inertial_to_topocentric * orbit.state.r;
  82. // Update local transform
  83. transform.local.translation = math::type_cast<float>(r_topocentric);
  84. });
  85. // Update blackbody lighting
  86. registry.view<celestial_body_component, orbit_component, blackbody_component>().each(
  87. [&](ecs::entity entity, const auto& celestial_body, const auto& orbit, const auto& blackbody)
  88. {
  89. // Calculate blackbody inertial basis
  90. double3 blackbody_forward_inertial = math::normalize(reference_orbit->state.r - orbit.state.r);
  91. double3 blackbody_up_inertial = {0, 0, 1};
  92. // Transform blackbody inertial position and basis into topocentric space
  93. double3 blackbody_position_topocentric = inertial_to_topocentric * orbit.state.r;
  94. double3 blackbody_forward_topocentric = inertial_to_topocentric.rotation * blackbody_forward_inertial;
  95. double3 blackbody_up_topocentric = inertial_to_topocentric.rotation * blackbody_up_inertial;
  96. // Calculate distance from observer to blackbody
  97. double blackbody_distance = math::length(blackbody_position_topocentric);
  98. // Calculate blackbody distance attenuation
  99. double distance_attenuation = 1.0 / (blackbody_distance * blackbody_distance);
  100. // Init atmospheric transmittance
  101. double3 atmospheric_transmittance = {1.0, 1.0, 1.0};
  102. // Get atmosphere component of reference body (if any)
  103. if (reference_atmosphere)
  104. {
  105. // Altitude of observer in meters
  106. geom::ray<double> sample_ray;
  107. sample_ray.origin = {0, observer_location[0], 0};
  108. sample_ray.direction = math::normalize(blackbody_position_topocentric);
  109. geom::sphere<double> exosphere;
  110. exosphere.center = {0, 0, 0};
  111. exosphere.radius = reference_body->radius + reference_atmosphere->exosphere_altitude;
  112. auto intersection_result = geom::ray_sphere_intersection(sample_ray, exosphere);
  113. if (std::get<0>(intersection_result))
  114. {
  115. double3 sample_start = sample_ray.origin;
  116. double3 sample_end = sample_ray.extrapolate(std::get<2>(intersection_result));
  117. double optical_depth_r = physics::atmosphere::optical_depth(sample_start, sample_end, reference_body->radius, reference_atmosphere->rayleigh_scale_height, 32);
  118. double optical_depth_k = physics::atmosphere::optical_depth(sample_start, sample_end, reference_body->radius, reference_atmosphere->mie_scale_height, 32);
  119. double optical_depth_o = 0.0;
  120. atmospheric_transmittance = transmittance(optical_depth_r, optical_depth_k, optical_depth_o, reference_atmosphere->rayleigh_scattering, reference_atmosphere->mie_scattering);
  121. }
  122. }
  123. if (sun_light != nullptr)
  124. {
  125. // Update blackbody light transform
  126. sun_light->set_translation(math::normalize(math::type_cast<float>(blackbody_position_topocentric)));
  127. sun_light->set_rotation
  128. (
  129. math::look_rotation
  130. (
  131. math::type_cast<float>(blackbody_forward_topocentric),
  132. math::type_cast<float>(blackbody_up_topocentric)
  133. )
  134. );
  135. // Update blackbody light color and intensity
  136. sun_light->set_color(math::type_cast<float>(blackbody.luminous_intensity * atmospheric_transmittance));
  137. sun_light->set_intensity(static_cast<float>(distance_attenuation));
  138. // Upload blackbody params to sky pass
  139. if (this->sky_pass)
  140. {
  141. this->sky_pass->set_sun_position(math::type_cast<float>(blackbody_position_topocentric));
  142. this->sky_pass->set_sun_color(math::type_cast<float>(blackbody.luminous_intensity * distance_attenuation));
  143. double blackbody_angular_radius = std::asin((celestial_body.radius * 2.0) / (blackbody_distance * 2.0));
  144. this->sky_pass->set_sun_angular_radius(static_cast<float>(blackbody_angular_radius));
  145. }
  146. }
  147. });
  148. // Update sky pass topocentric frame
  149. if (sky_pass != nullptr)
  150. {
  151. // Upload topocentric frame to sky pass
  152. sky_pass->set_topocentric_frame
  153. (
  154. physics::frame<float>
  155. {
  156. math::type_cast<float>(inertial_to_topocentric.translation),
  157. math::type_cast<float>(inertial_to_topocentric.rotation)
  158. }
  159. );
  160. // Upload observer altitude to sky pass
  161. float observer_altitude = observer_location[0] - reference_body->radius;
  162. sky_pass->set_observer_altitude(observer_altitude);
  163. // Upload atmosphere params to sky pass
  164. if (reference_atmosphere)
  165. {
  166. sky_pass->set_scale_heights(reference_atmosphere->rayleigh_scale_height, reference_atmosphere->mie_scale_height);
  167. sky_pass->set_scattering_coefficients(math::type_cast<float>(reference_atmosphere->rayleigh_scattering), math::type_cast<float>(reference_atmosphere->mie_scattering));
  168. sky_pass->set_mie_anisotropy(reference_atmosphere->mie_anisotropy);
  169. sky_pass->set_atmosphere_radii(reference_body->radius, reference_body->radius + reference_atmosphere->exosphere_altitude);
  170. }
  171. }
  172. }
  173. void astronomy_system::set_universal_time(double time)
  174. {
  175. universal_time = time;
  176. }
  177. void astronomy_system::set_time_scale(double scale)
  178. {
  179. time_scale = scale;
  180. }
  181. void astronomy_system::set_reference_body(ecs::entity entity)
  182. {
  183. reference_entity = entity;
  184. reference_orbit = nullptr;
  185. reference_body = nullptr;
  186. reference_atmosphere = nullptr;
  187. if (reference_entity != entt::null)
  188. {
  189. if (registry.has<ecs::orbit_component>(reference_entity))
  190. reference_orbit = &registry.get<ecs::orbit_component>(reference_entity);
  191. if (registry.has<ecs::celestial_body_component>(reference_entity))
  192. reference_body = &registry.get<ecs::celestial_body_component>(reference_entity);
  193. if (registry.has<ecs::atmosphere_component>(reference_entity))
  194. reference_atmosphere = &registry.get<ecs::atmosphere_component>(reference_entity);
  195. }
  196. }
  197. void astronomy_system::set_observer_location(const double3& location)
  198. {
  199. observer_location = location;
  200. // Construct reference frame which transforms coordinates from SEZ to EZS
  201. sez_to_ezs = physics::frame<double>
  202. {
  203. {0, 0, 0},
  204. math::normalize
  205. (
  206. math::quaternion<double>::rotate_x(-math::half_pi<double>) *
  207. math::quaternion<double>::rotate_z(-math::half_pi<double>)
  208. )
  209. };
  210. // Construct reference frame which transforms coordinates from EZS to SEZ
  211. ezs_to_sez = sez_to_ezs.inverse();
  212. // Construct reference frame which transforms coordinates from BCBF space to topocentric space
  213. bcbf_to_topocentric = physics::orbit::bcbf::to_topocentric
  214. (
  215. observer_location[0], // Radial distance
  216. observer_location[1], // Latitude
  217. observer_location[2] // Longitude
  218. ) * sez_to_ezs;
  219. }
  220. void astronomy_system::set_sun_light(scene::directional_light* light)
  221. {
  222. sun_light = light;
  223. }
  224. void astronomy_system::set_sky_pass(::sky_pass* pass)
  225. {
  226. this->sky_pass = pass;
  227. }
  228. } // namespace ecs