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

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