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

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