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

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