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

308 lines
11 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. // RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
  58. rgb_wavelengths_nm = {602.224, 541.069, 448.143};
  59. rgb_wavelengths_m = rgb_wavelengths_nm * 1e-9;
  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 either reference body or orbit have not been set
  68. if (!reference_orbit || !reference_body)
  69. return;
  70. // Determine axial rotation at current time
  71. const double reference_axial_rotation = reference_body->axial_rotation + reference_body->angular_frequency * universal_time;
  72. // Construct reference frame which transforms coordinates from inertial space to reference body BCBF space
  73. inertial_to_bcbf = physics::orbit::inertial::to_bcbf
  74. (
  75. reference_orbit->state.r,
  76. reference_orbit->elements.i,
  77. reference_body->axial_tilt,
  78. reference_axial_rotation
  79. );
  80. // Construct reference frame which transforms coordinates from inertial space to reference body topocentric space
  81. inertial_to_topocentric = inertial_to_bcbf * bcbf_to_topocentric;
  82. // Set the transform component translations of orbiting bodies to their topocentric positions
  83. registry.view<orbit_component, transform_component>().each(
  84. [&](ecs::entity entity, const auto& orbit, auto& transform)
  85. {
  86. // Transform Cartesian position vector (r) from inertial space to topocentric space
  87. const math::vector3<double> r_topocentric = inertial_to_topocentric * orbit.state.r;
  88. // Update local transform
  89. transform.local.translation = math::type_cast<float>(r_topocentric);
  90. });
  91. // Update blackbody lighting
  92. registry.view<celestial_body_component, orbit_component, blackbody_component>().each(
  93. [&](ecs::entity entity, const auto& celestial_body, const auto& orbit, const auto& blackbody)
  94. {
  95. // Calculate blackbody inertial basis
  96. double3 blackbody_forward_inertial = math::normalize(reference_orbit->state.r - orbit.state.r);
  97. double3 blackbody_up_inertial = {0, 0, 1};
  98. // Transform blackbody inertial position and basis into topocentric space
  99. double3 blackbody_position_topocentric = inertial_to_topocentric * orbit.state.r;
  100. double3 blackbody_forward_topocentric = inertial_to_topocentric.rotation * blackbody_forward_inertial;
  101. double3 blackbody_up_topocentric = inertial_to_topocentric.rotation * blackbody_up_inertial;
  102. // Calculate distance from observer to blackbody
  103. double blackbody_distance = math::length(blackbody_position_topocentric);
  104. // Calculate blackbody distance attenuation
  105. double distance_attenuation = 1.0 / (blackbody_distance * blackbody_distance);
  106. // Init atmospheric transmittance
  107. double3 atmospheric_transmittance = {1.0, 1.0, 1.0};
  108. // Get atmosphere component of reference body (if any)
  109. if (reference_atmosphere)
  110. {
  111. // Altitude of observer in meters
  112. geom::ray<double> sample_ray;
  113. sample_ray.origin = {0, observer_location[0], 0};
  114. sample_ray.direction = math::normalize(blackbody_position_topocentric);
  115. geom::sphere<double> exosphere;
  116. exosphere.center = {0, 0, 0};
  117. exosphere.radius = reference_body->radius + reference_atmosphere->exosphere_altitude;
  118. auto intersection_result = geom::ray_sphere_intersection(sample_ray, exosphere);
  119. if (std::get<0>(intersection_result))
  120. {
  121. double3 sample_start = sample_ray.origin;
  122. double3 sample_end = sample_ray.extrapolate(std::get<2>(intersection_result));
  123. double optical_depth_r = physics::atmosphere::optical_depth(sample_start, sample_end, reference_body->radius, reference_atmosphere->rayleigh_scale_height, 32);
  124. double optical_depth_k = physics::atmosphere::optical_depth(sample_start, sample_end, reference_body->radius, reference_atmosphere->mie_scale_height, 32);
  125. double optical_depth_o = 0.0;
  126. atmospheric_transmittance = transmittance(optical_depth_r, optical_depth_k, optical_depth_o, reference_atmosphere->rayleigh_scattering, reference_atmosphere->mie_scattering);
  127. }
  128. }
  129. if (sun_light != nullptr)
  130. {
  131. // Update blackbody light transform
  132. sun_light->set_translation(math::normalize(math::type_cast<float>(blackbody_position_topocentric)));
  133. sun_light->set_rotation
  134. (
  135. math::look_rotation
  136. (
  137. math::type_cast<float>(blackbody_forward_topocentric),
  138. math::type_cast<float>(blackbody_up_topocentric)
  139. )
  140. );
  141. // Update blackbody light color and intensity
  142. sun_light->set_color(math::type_cast<float>(blackbody.luminous_intensity * atmospheric_transmittance));
  143. sun_light->set_intensity(static_cast<float>(distance_attenuation));
  144. // Upload blackbody params to sky pass
  145. if (this->sky_pass)
  146. {
  147. this->sky_pass->set_sun_position(math::type_cast<float>(blackbody_position_topocentric));
  148. this->sky_pass->set_sun_color(math::type_cast<float>(blackbody.luminous_intensity * distance_attenuation));
  149. double blackbody_angular_radius = std::asin((celestial_body.radius * 2.0) / (blackbody_distance * 2.0));
  150. this->sky_pass->set_sun_angular_radius(static_cast<float>(blackbody_angular_radius));
  151. }
  152. }
  153. });
  154. // Update sky pass topocentric frame
  155. if (sky_pass != nullptr)
  156. {
  157. // Upload topocentric frame to sky pass
  158. sky_pass->set_topocentric_frame
  159. (
  160. physics::frame<float>
  161. {
  162. math::type_cast<float>(inertial_to_topocentric.translation),
  163. math::type_cast<float>(inertial_to_topocentric.rotation)
  164. }
  165. );
  166. // Upload observer altitude to sky pass
  167. float observer_altitude = observer_location[0] - reference_body->radius;
  168. sky_pass->set_observer_altitude(observer_altitude);
  169. // Upload atmosphere params to sky pass
  170. if (reference_atmosphere)
  171. {
  172. sky_pass->set_scale_heights(reference_atmosphere->rayleigh_scale_height, reference_atmosphere->mie_scale_height);
  173. sky_pass->set_scattering_coefficients(math::type_cast<float>(reference_atmosphere->rayleigh_scattering), math::type_cast<float>(reference_atmosphere->mie_scattering));
  174. sky_pass->set_mie_anisotropy(reference_atmosphere->mie_anisotropy);
  175. sky_pass->set_atmosphere_radii(reference_body->radius, reference_body->radius + reference_atmosphere->exosphere_altitude);
  176. }
  177. }
  178. }
  179. void astronomy_system::set_universal_time(double time)
  180. {
  181. universal_time = time;
  182. }
  183. void astronomy_system::set_time_scale(double scale)
  184. {
  185. time_scale = scale;
  186. }
  187. void astronomy_system::set_reference_body(ecs::entity entity)
  188. {
  189. reference_entity = entity;
  190. reference_orbit = nullptr;
  191. reference_body = nullptr;
  192. reference_atmosphere = nullptr;
  193. if (reference_entity != entt::null)
  194. {
  195. if (registry.has<ecs::orbit_component>(reference_entity))
  196. reference_orbit = &registry.get<ecs::orbit_component>(reference_entity);
  197. if (registry.has<ecs::celestial_body_component>(reference_entity))
  198. reference_body = &registry.get<ecs::celestial_body_component>(reference_entity);
  199. if (registry.has<ecs::atmosphere_component>(reference_entity))
  200. reference_atmosphere = &registry.get<ecs::atmosphere_component>(reference_entity);
  201. }
  202. }
  203. void astronomy_system::set_observer_location(const double3& location)
  204. {
  205. observer_location = location;
  206. // Construct reference frame which transforms coordinates from SEZ to EZS
  207. sez_to_ezs = physics::frame<double>
  208. {
  209. {0, 0, 0},
  210. math::normalize
  211. (
  212. math::quaternion<double>::rotate_x(-math::half_pi<double>) *
  213. math::quaternion<double>::rotate_z(-math::half_pi<double>)
  214. )
  215. };
  216. // Construct reference frame which transforms coordinates from EZS to SEZ
  217. ezs_to_sez = sez_to_ezs.inverse();
  218. // Construct reference frame which transforms coordinates from BCBF space to topocentric space
  219. bcbf_to_topocentric = physics::orbit::bcbf::to_topocentric
  220. (
  221. observer_location[0], // Radial distance
  222. observer_location[1], // Latitude
  223. observer_location[2] // Longitude
  224. ) * sez_to_ezs;
  225. }
  226. void astronomy_system::set_sun_light(scene::directional_light* light)
  227. {
  228. sun_light = light;
  229. }
  230. void astronomy_system::set_sky_pass(::sky_pass* pass)
  231. {
  232. this->sky_pass = pass;
  233. }
  234. void astronomy_system::on_atmosphere_construct(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
  235. {
  236. on_atmosphere_replace(registry, entity, atmosphere);
  237. }
  238. void astronomy_system::on_atmosphere_replace(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
  239. {
  240. // Calculate polarization factors
  241. const double rayleigh_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.rayleigh_density);
  242. const double mie_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.mie_density);
  243. // Calculate Rayleigh scattering coefficients
  244. atmosphere.rayleigh_scattering =
  245. {
  246. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.x, atmosphere.rayleigh_density, rayleigh_polarization),
  247. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.y, atmosphere.rayleigh_density, rayleigh_polarization),
  248. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.z, atmosphere.rayleigh_density, rayleigh_polarization)
  249. };
  250. // Calculate Mie scattering coefficients
  251. const double mie_scattering = physics::atmosphere::scattering_mie(atmosphere.mie_density, mie_polarization);
  252. atmosphere.mie_scattering =
  253. {
  254. mie_scattering,
  255. mie_scattering,
  256. mie_scattering
  257. };
  258. }
  259. } // namespace ecs