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

351 lines
13 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/orbit-component.hpp"
  22. #include "ecs/components/blackbody-component.hpp"
  23. #include "ecs/components/atmosphere-component.hpp"
  24. #include "ecs/components/transform-component.hpp"
  25. #include "geom/intersection.hpp"
  26. #include "color/color.hpp"
  27. #include "physics/orbit/orbit.hpp"
  28. #include "physics/time/ut1.hpp"
  29. #include "physics/light/blackbody.hpp"
  30. #include "physics/light/photometry.hpp"
  31. #include "physics/light/luminosity.hpp"
  32. #include "physics/light/refraction.hpp"
  33. #include "physics/atmosphere.hpp"
  34. #include "math/quadrature.hpp"
  35. #include "geom/cartesian.hpp"
  36. #include <iostream>
  37. namespace ecs {
  38. template <class T>
  39. 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)
  40. {
  41. math::vector3<T> transmittance_r = beta_r * depth_r;
  42. math::vector3<T> transmittance_m = beta_m * 1.1 * depth_m;
  43. math::vector3<T> transmittance_o = {0, 0, 0};
  44. math::vector3<T> t = transmittance_r + transmittance_m + transmittance_o;
  45. t.x = std::exp(-t.x);
  46. t.y = std::exp(-t.y);
  47. t.z = std::exp(-t.z);
  48. return t;
  49. }
  50. astronomy_system::astronomy_system(ecs::registry& registry):
  51. entity_system(registry),
  52. universal_time(0.0),
  53. time_scale(1.0),
  54. reference_body(entt::null),
  55. reference_body_axial_tilt(0.0),
  56. reference_body_axial_rotation(0.0),
  57. sun_light(nullptr),
  58. sky_pass(nullptr)
  59. {
  60. // RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
  61. rgb_wavelengths_nm = {602.224, 541.069, 448.143};
  62. rgb_wavelengths_m = rgb_wavelengths_nm * 1e-9;
  63. registry.on_construct<ecs::blackbody_component>().connect<&astronomy_system::on_blackbody_construct>(this);
  64. registry.on_replace<ecs::blackbody_component>().connect<&astronomy_system::on_blackbody_replace>(this);
  65. registry.on_construct<ecs::atmosphere_component>().connect<&astronomy_system::on_atmosphere_construct>(this);
  66. registry.on_replace<ecs::atmosphere_component>().connect<&astronomy_system::on_atmosphere_replace>(this);
  67. }
  68. void astronomy_system::update(double t, double dt)
  69. {
  70. // Add scaled timestep to current time
  71. set_universal_time(universal_time + dt * time_scale);
  72. // Abort if reference body has not been set
  73. if (reference_body == entt::null)
  74. return;
  75. // Abort if reference body has no orbit component
  76. if (!registry.has<ecs::orbit_component>(reference_body))
  77. return;
  78. // Update axial rotation of reference body
  79. reference_body_axial_rotation = physics::time::ut1::era(universal_time);
  80. // Get orbit component of reference body
  81. const auto& reference_orbit = registry.get<ecs::orbit_component>(reference_body);
  82. /// Construct reference frame which transforms coordinates from inertial space to reference body BCBF space
  83. inertial_to_bcbf = physics::orbit::inertial::to_bcbf
  84. (
  85. reference_orbit.state.r,
  86. reference_orbit.elements.i,
  87. reference_body_axial_tilt,
  88. reference_body_axial_rotation
  89. );
  90. /// Construct reference frame which transforms coordinates from inertial space to reference body topocentric space
  91. inertial_to_topocentric = inertial_to_bcbf * bcbf_to_topocentric;
  92. // Set the transform component translations of orbiting bodies to their topocentric positions
  93. registry.view<orbit_component, transform_component>().each(
  94. [&](ecs::entity entity, auto& orbit, auto& transform)
  95. {
  96. // Transform Cartesian position vector (r) from inertial space to topocentric space
  97. const math::vector3<double> r_topocentric = inertial_to_topocentric * orbit.state.r;
  98. // Update local transform
  99. transform.local.translation = math::type_cast<float>(r_topocentric);
  100. });
  101. const double earth_radius = 6.3781e6;
  102. // Update blackbody lighting
  103. registry.view<blackbody_component, orbit_component>().each(
  104. [&](ecs::entity entity, auto& blackbody, auto& orbit)
  105. {
  106. // Calculate blackbody inertial basis
  107. double3 blackbody_forward_inertial = math::normalize(reference_orbit.state.r - orbit.state.r);
  108. double3 blackbody_up_inertial = {0, 0, 1};
  109. // Transform blackbody inertial position and basis into topocentric space
  110. double3 blackbody_position_topocentric = inertial_to_topocentric * orbit.state.r;
  111. double3 blackbody_forward_topocentric = inertial_to_topocentric.rotation * blackbody_forward_inertial;
  112. double3 blackbody_up_topocentric = inertial_to_topocentric.rotation * blackbody_up_inertial;
  113. // Calculate distance from observer to blackbody
  114. double blackbody_distance = math::length(blackbody_position_topocentric);
  115. // Calculate blackbody distance attenuation
  116. double distance_attenuation = 1.0 / (blackbody_distance * blackbody_distance);
  117. // Init atmospheric transmittance
  118. double3 atmospheric_transmittance = {1.0, 1.0, 1.0};
  119. // Get atmosphere component of reference body (if any)
  120. if (this->registry.has<ecs::atmosphere_component>(reference_body))
  121. {
  122. const ecs::atmosphere_component& atmosphere = this->registry.get<ecs::atmosphere_component>(reference_body);
  123. // Altitude of observer in meters
  124. geom::ray<double> sample_ray;
  125. sample_ray.origin = {0, observer_location[0], 0};
  126. sample_ray.direction = math::normalize(blackbody_position_topocentric);
  127. geom::sphere<double> exosphere;
  128. exosphere.center = {0, 0, 0};
  129. exosphere.radius = earth_radius + atmosphere.exosphere_altitude;
  130. auto intersection_result = geom::ray_sphere_intersection(sample_ray, exosphere);
  131. if (std::get<0>(intersection_result))
  132. {
  133. double3 sample_start = sample_ray.origin;
  134. double3 sample_end = sample_ray.extrapolate(std::get<2>(intersection_result));
  135. double optical_depth_r = physics::atmosphere::optical_depth(sample_start, sample_end, earth_radius, atmosphere.rayleigh_scale_height, 32);
  136. double optical_depth_k = physics::atmosphere::optical_depth(sample_start, sample_end, earth_radius, atmosphere.mie_scale_height, 32);
  137. double optical_depth_o = 0.0;
  138. atmospheric_transmittance = transmittance(optical_depth_r, optical_depth_k, optical_depth_o, atmosphere.rayleigh_scattering, atmosphere.mie_scattering);
  139. }
  140. }
  141. if (sun_light != nullptr)
  142. {
  143. // Update blackbody light transform
  144. sun_light->set_translation(math::normalize(math::type_cast<float>(blackbody_position_topocentric)));
  145. sun_light->set_rotation
  146. (
  147. math::look_rotation
  148. (
  149. math::type_cast<float>(blackbody_forward_topocentric),
  150. math::type_cast<float>(blackbody_up_topocentric)
  151. )
  152. );
  153. // Update blackbody light color and intensity
  154. sun_light->set_color(math::type_cast<float>(blackbody.luminous_intensity * atmospheric_transmittance));
  155. sun_light->set_intensity(static_cast<float>(distance_attenuation));
  156. // Upload blackbody params to sky pass
  157. if (this->sky_pass)
  158. {
  159. this->sky_pass->set_sun_position(math::type_cast<float>(blackbody_position_topocentric));
  160. this->sky_pass->set_sun_color(math::type_cast<float>(blackbody.luminous_intensity * distance_attenuation));
  161. double blackbody_angular_radius = std::asin((blackbody.radius * 2.0) / (blackbody_distance * 2.0));
  162. this->sky_pass->set_sun_angular_radius(static_cast<float>(blackbody_angular_radius));
  163. }
  164. }
  165. });
  166. // Update sky pass topocentric frame
  167. if (sky_pass != nullptr)
  168. {
  169. // Upload topocentric frame to sky pass
  170. sky_pass->set_topocentric_frame
  171. (
  172. physics::frame<float>
  173. {
  174. math::type_cast<float>(inertial_to_topocentric.translation),
  175. math::type_cast<float>(inertial_to_topocentric.rotation)
  176. }
  177. );
  178. // Upload observer altitude to sky pass
  179. float observer_altitude = observer_location[0] - earth_radius;
  180. sky_pass->set_observer_altitude(observer_altitude);
  181. // Upload atmosphere params to sky pass
  182. if (this->registry.has<ecs::atmosphere_component>(reference_body))
  183. {
  184. const ecs::atmosphere_component& atmosphere = this->registry.get<ecs::atmosphere_component>(reference_body);
  185. sky_pass->set_scale_heights(atmosphere.rayleigh_scale_height, atmosphere.mie_scale_height);
  186. sky_pass->set_scattering_coefficients(math::type_cast<float>(atmosphere.rayleigh_scattering), math::type_cast<float>(atmosphere.mie_scattering));
  187. sky_pass->set_mie_anisotropy(atmosphere.mie_anisotropy);
  188. sky_pass->set_atmosphere_radii(earth_radius, earth_radius + atmosphere.exosphere_altitude);
  189. }
  190. }
  191. }
  192. void astronomy_system::set_universal_time(double time)
  193. {
  194. universal_time = time;
  195. }
  196. void astronomy_system::set_time_scale(double scale)
  197. {
  198. time_scale = scale;
  199. }
  200. void astronomy_system::set_reference_body(ecs::entity entity)
  201. {
  202. reference_body = entity;
  203. }
  204. void astronomy_system::set_reference_body_axial_tilt(double angle)
  205. {
  206. reference_body_axial_tilt = angle;
  207. }
  208. void astronomy_system::set_observer_location(const double3& location)
  209. {
  210. observer_location = location;
  211. // Construct reference frame which transforms coordinates from SEZ to EZS
  212. sez_to_ezs = physics::frame<double>
  213. {
  214. {0, 0, 0},
  215. math::normalize
  216. (
  217. math::quaternion<double>::rotate_x(-math::half_pi<double>) *
  218. math::quaternion<double>::rotate_z(-math::half_pi<double>)
  219. )
  220. };
  221. // Construct reference frame which transforms coordinates from EZS to SEZ
  222. ezs_to_sez = sez_to_ezs.inverse();
  223. // Construct reference frame which transforms coordinates from BCBF space to topocentric space
  224. bcbf_to_topocentric = physics::orbit::bcbf::to_topocentric
  225. (
  226. observer_location[0], // Radial distance
  227. observer_location[1], // Latitude
  228. observer_location[2] // Longitude
  229. ) * sez_to_ezs;
  230. }
  231. void astronomy_system::set_sun_light(scene::directional_light* light)
  232. {
  233. sun_light = light;
  234. }
  235. void astronomy_system::set_sky_pass(::sky_pass* pass)
  236. {
  237. this->sky_pass = pass;
  238. }
  239. void astronomy_system::on_blackbody_construct(ecs::registry& registry, ecs::entity entity, ecs::blackbody_component& blackbody)
  240. {
  241. on_blackbody_replace(registry, entity, blackbody);
  242. }
  243. void astronomy_system::on_blackbody_replace(ecs::registry& registry, ecs::entity entity, ecs::blackbody_component& blackbody)
  244. {
  245. // Calculate the surface area of a spherical blackbody
  246. const double surface_area = 4.0 * math::pi<double> * blackbody.radius * blackbody.radius;
  247. // Construct a lambda function which calculates the blackbody's RGB luminous intensity of a given wavelength
  248. auto rgb_luminous_intensity = [blackbody, surface_area](double wavelength_nm) -> double3
  249. {
  250. // Convert wavelength from nanometers to meters
  251. const double wavelength_m = wavelength_nm * 1e-9;
  252. // Calculate the spectral intensity of the wavelength
  253. const double spectral_intensity = physics::light::blackbody::spectral_intensity<double>(blackbody.temperature, surface_area, wavelength_m);
  254. // Calculate the ACEScg color of the wavelength using CIE color matching functions
  255. double3 spectral_color = color::xyz::to_acescg(color::xyz::match(wavelength_nm));
  256. // Scale the spectral color by spectral intensity
  257. return spectral_color * spectral_intensity * 1e-9 * physics::light::max_luminous_efficacy<double>;
  258. };
  259. // Construct a range of sample wavelengths in the visible spectrum
  260. std::vector<double> samples(780 - 280);
  261. std::iota(samples.begin(), samples.end(), 280);
  262. // Integrate the blackbody RGB luminous intensity over wavelengths in the visible spectrum
  263. blackbody.luminous_intensity = math::quadrature::simpson(rgb_luminous_intensity, samples.begin(), samples.end());
  264. }
  265. void astronomy_system::on_atmosphere_construct(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
  266. {
  267. on_atmosphere_replace(registry, entity, atmosphere);
  268. }
  269. void astronomy_system::on_atmosphere_replace(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
  270. {
  271. // Calculate polarization factors
  272. const double rayleigh_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.rayleigh_density);
  273. const double mie_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.mie_density);
  274. // Calculate Rayleigh scattering coefficients
  275. atmosphere.rayleigh_scattering =
  276. {
  277. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.x, atmosphere.rayleigh_density, rayleigh_polarization),
  278. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.y, atmosphere.rayleigh_density, rayleigh_polarization),
  279. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.z, atmosphere.rayleigh_density, rayleigh_polarization)
  280. };
  281. // Calculate Mie scattering coefficients
  282. const double mie_scattering = physics::atmosphere::scattering_mie(atmosphere.mie_density, mie_polarization);
  283. atmosphere.mie_scattering =
  284. {
  285. mie_scattering,
  286. mie_scattering,
  287. mie_scattering
  288. };
  289. }
  290. } // namespace ecs