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

486 lines
15 KiB

  1. /*
  2. * Copyright (C) 2020 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 "game/systems/weather-system.hpp"
  20. #include "scene/directional-light.hpp"
  21. #include "scene/ambient-light.hpp"
  22. #include "renderer/passes/sky-pass.hpp"
  23. #include "renderer/passes/shadow-map-pass.hpp"
  24. #include "renderer/passes/material-pass.hpp"
  25. #include "utility/gamma.hpp"
  26. #include "resources/image.hpp"
  27. #include "game/astronomy/celestial-coordinates.hpp"
  28. #include <cmath>
  29. #include <iostream>
  30. static constexpr double hours_per_day = 24.0;
  31. static constexpr double minutes_per_day = hours_per_day * 60.0;
  32. static constexpr double seconds_per_day = minutes_per_day * 60.0;
  33. /**
  34. *
  35. * @param year Gregorian year
  36. * @param month Month (1 = January, 12 = December)
  37. * @param day Day (1-31)
  38. * @param time Universal time in decimal hours.
  39. */
  40. static double julian_day(int year, int month, int day, double time)
  41. {
  42. if (month < 3)
  43. {
  44. month += 12;
  45. year -= 1;
  46. }
  47. double y = static_cast<double>(year);
  48. double m = static_cast<double>(month);
  49. double d = static_cast<double>(day);
  50. return std::floor(365.25 * y) + std::floor(30.6001 * (m + 1.0)) - 15.0 + 1720996.5 + d + time;
  51. }
  52. /**
  53. * Calculates the ecliptic rectangular geocentric coordinates of the sun, with distance in AU.
  54. */
  55. double3 calculate_sun_ecliptic(double jd)
  56. {
  57. const double t = (jd - 2451545.0) / 36525.0;
  58. const double m = 6.24 + 628.302 * t;
  59. const double longitude = 4.895048 + 628.331951 * t + (0.033417 - 0.000084 * t) * std::sin(m) + 0.000351 * std::sin(m * 2.0);
  60. const double latitude = 0.0;
  61. const double distance = 1.000140 - (0.016708 - 0.000042 * t) * std::cos(m) - 0.000141 * std::cos(m * 2.0);
  62. double3 ecliptic;
  63. ecliptic.x = distance * std::cos(longitude) * std::cos(latitude);
  64. ecliptic.y = distance * std::sin(longitude) * std::cos(latitude);
  65. ecliptic.z = distance * std::sin(latitude);
  66. return ecliptic;
  67. }
  68. /**
  69. * Calculates the ecliptic rectangular geocentric coordinates of the moon, with distance in Earth radii.
  70. */
  71. double3 calculate_moon_ecliptic(double jd)
  72. {
  73. const double t = (jd - 2451545.0) / 36525.0;
  74. const double l1 = 3.8104 + 8399.7091 * t;
  75. const double m1 = 2.3554 + 8328.6911 * t;
  76. const double m = 6.2300 + 628.3019 * t;
  77. const double d = 5.1985 + 7771.3772 * t;
  78. const double d2 = d * 2.0;
  79. const double f = 1.6280 + 8433.4663 * t;
  80. const double longitude = l1
  81. + 0.1098 * std::sin(m1)
  82. + 0.0222 * std::sin(d2 - m1)
  83. + 0.0115 * std::sin(d2)
  84. + 0.0037 * std::sin(m1 * 2.0)
  85. - 0.0032 * std::sin(m)
  86. - 0.0020 * std::sin(d2)
  87. + 0.0010 * std::sin(d2 - m1 * 2.0)
  88. + 0.0010 * std::sin(d2 - m - m1)
  89. + 0.0009 * std::sin(d2 + m1)
  90. + 0.0008 * std::sin(d2 - m)
  91. + 0.0007 * std::sin(m1 - m)
  92. - 0.0006 * std::sin(d)
  93. - 0.0005 * std::sin(m + m1);
  94. const double latitude = 0.0895 * sin(f)
  95. + 0.0049 * std::sin(m1 + f)
  96. + 0.0048 * std::sin(m1 - f)
  97. + 0.0030 * std::sin(d2 - f)
  98. + 0.0010 * std::sin(d2 + f - m1)
  99. + 0.0008 * std::sin(d2 - f - m1)
  100. + 0.0006 * std::sin(d2 + f);
  101. const double r = 1.0 / (0.016593
  102. + 0.000904 * std::cos(m1)
  103. + 0.000166 * std::cos(d2 - m1)
  104. + 0.000137 * std::cos(d2)
  105. + 0.000049 * std::cos(m1 * 2.0)
  106. + 0.000015 * std::cos(d2 + m1)
  107. + 0.000009 * std::cos(d2 - m));
  108. double3 ecliptic;
  109. ecliptic.x = r * std::cos(longitude) * std::cos(latitude);
  110. ecliptic.y = r * std::sin(longitude) * std::cos(latitude);
  111. ecliptic.z = r * std::sin(latitude);
  112. return ecliptic;
  113. }
  114. double3x3 find_moon_ecliptic_rotation(double jd)
  115. {
  116. const double t = (jd - 2451545.0) / 36525.0;
  117. const double l1 = 3.8104 + 8399.7091 * t;
  118. const double f = 1.6280 + 8433.4663 * t;
  119. const double az0 = f + math::pi<double>;
  120. const double ax = 0.026920;
  121. const double az1 = l1 - f;
  122. double3x3 rz0 =
  123. {
  124. cos(az0), -sin(az0), 0,
  125. sin(az0), cos(az0), 0,
  126. 0, 0, 1
  127. };
  128. double3x3 rx =
  129. {
  130. 1, 0, 0,
  131. 0, cos(ax), -sin(ax),
  132. 0, sin(ax), cos(ax)
  133. };
  134. double3x3 rz1 =
  135. {
  136. cos(az1), -sin(az1), 0,
  137. sin(az1), cos(az1), 0,
  138. 0, 0, 1
  139. };
  140. return rz0 * rx * rz1;
  141. }
  142. /// @see http://www.stjarnhimlen.se/comp/ppcomp.html
  143. /// @see http://www.geoastro.de/elevazmoon/basics/index.htm
  144. void ecliptic_to_equatorial(double longitude, double latitude, double ecl, double* right_ascension, double* declination)
  145. {
  146. double eclip_x = std::cos(longitude) * std::cos(latitude);
  147. double eclip_y = std::sin(longitude) * std::cos(latitude);
  148. double eclip_z = std::sin(latitude);
  149. double equat_x = eclip_x;
  150. double equat_y = eclip_y * std::cos(ecl) - eclip_z * std::sin(ecl);
  151. double equat_z = eclip_y * std::sin(ecl) + eclip_z * std::cos(ecl);
  152. *right_ascension = std::atan2(equat_y, equat_x);
  153. *declination = std::atan2(equat_z, sqrt(equat_x * equat_x + equat_y * equat_y));
  154. }
  155. /// @see http://www.stjarnhimlen.se/comp/ppcomp.html
  156. /// @see http://www.geoastro.de/elevazmoon/basics/index.htm
  157. void equatorial_to_horizontal(double right_ascension, double declination, double lmst, double latitude, double* azimuth, double* elevation)
  158. {
  159. double hour_angle = lmst - right_ascension;
  160. double x = std::cos(hour_angle) * std::cos(declination);
  161. double y = std::sin(hour_angle) * std::cos(declination);
  162. double z = std::sin(declination);
  163. double horiz_x = x * std::cos(math::half_pi<double> - latitude) - z * std::sin(math::half_pi<double> - latitude);
  164. double horiz_y = y;
  165. double horiz_z = x * std::sin(math::half_pi<double> - latitude) + z * std::cos(math::half_pi<double> - latitude);
  166. *azimuth = math::wrap_radians<double>(std::atan2(horiz_y, horiz_x) + math::pi<double>);
  167. *elevation = math::wrap_radians<double>(std::atan2(horiz_z, std::sqrt(horiz_x * horiz_x + horiz_y * horiz_y)));
  168. }
  169. double3x3 horizontal_to_right_handed()
  170. {
  171. return double3x3
  172. {
  173. 0.0, 0.0, 1.0,
  174. 1.0, 0.0, 0.0,
  175. 0.0, -1.0, 0.0
  176. };
  177. }
  178. /**
  179. * Calculates the Greenwich mean sidereal time (GMST) from a Julian day.
  180. *
  181. * @param jd Julian day.
  182. * @return GMST, in radians.
  183. */
  184. static double jd_to_gmst(double jd)
  185. {
  186. return math::wrap_radians<double>(4.894961212 + 6.300388098 * (jd - 2451545.0));
  187. }
  188. weather_system::weather_system(entt::registry& registry):
  189. entity_system(registry),
  190. ambient_light(nullptr),
  191. sun_light(nullptr),
  192. moon_light(nullptr),
  193. shadow_light(nullptr),
  194. sky_pass(nullptr),
  195. shadow_map_pass(nullptr),
  196. material_pass(nullptr),
  197. time_scale(1.0f),
  198. sun_direction{0.0f, -1.0f, 0.0f},
  199. location{0.0f, 0.0f, 0.0f},
  200. jd(0.0)
  201. {}
  202. void weather_system::update(double t, double dt)
  203. {
  204. jd += (dt * time_scale) / seconds_per_day;
  205. const float latitude = location[0];
  206. const float longitude = location[1];
  207. // Calculate local mean sidereal time (LMST)
  208. double time_correction = longitude / (math::two_pi<double> / 24.0);
  209. double local_jd = jd + time_correction / 24.0 - 0.5;
  210. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  211. double hour = local_time;
  212. double gmst = jd_to_gmst(jd);
  213. double lmst = gmst + longitude;
  214. // Obliquity of the ecliptic
  215. double ecl = math::radians<double>(23.4393 - 3.563e-7 * (jd - 2451545.0));
  216. // Solar distance in AU
  217. //double sr = ...
  218. // Apparent radius in degrees
  219. //double sradius = 0.2666 / sr;
  220. double3x3 ecliptic_to_horizontal = ast::ecliptic_to_horizontal(ecl, latitude, lmst);
  221. double3 sun_ecliptic = calculate_sun_ecliptic(jd);
  222. double3 sun_horizontal = ecliptic_to_horizontal * sun_ecliptic;
  223. double3 sun_spherical = ast::rectangular_to_spherical(sun_horizontal);
  224. double3 sun_positiond = horizontal_to_right_handed() * sun_horizontal;
  225. float2 sun_az_el = {static_cast<float>(sun_spherical.z) - math::pi<float>, static_cast<float>(sun_spherical.y)};
  226. float3 sun_position = math::normalize(float3{static_cast<float>(sun_positiond.x), static_cast<float>(sun_positiond.y), static_cast<float>(sun_positiond.z)});
  227. double3 moon_ecliptic = calculate_moon_ecliptic(jd);
  228. double3 moon_horizontal = ecliptic_to_horizontal * moon_ecliptic;
  229. moon_horizontal.z -= 1.0; // Subtract one earth radius, for position of observer
  230. double3 moon_spherical = ast::rectangular_to_spherical(moon_horizontal);
  231. double3 moon_positiond = horizontal_to_right_handed() * moon_horizontal;
  232. float2 moon_az_el = {static_cast<float>(moon_spherical.z) - math::pi<float>, static_cast<float>(moon_spherical.y)};
  233. float3 moon_position = math::normalize(float3{static_cast<float>(moon_positiond.x), static_cast<float>(moon_positiond.y), static_cast<float>(moon_positiond.z)});
  234. //std::cout << "new moon: " << math::degrees(moon_az_el[0]) << ", " << math::degrees(moon_az_el[1]) << std::endl;
  235. double3x3 moon_rotation_matrix = horizontal_to_right_handed() * ecliptic_to_horizontal;
  236. math::quaternion<double> moon_rotationd = math::normalize(math::quaternion_cast(moon_rotation_matrix) * math::angle_axis(math::half_pi<double>, double3{0, 1, 0}) * math::angle_axis(-math::half_pi<double>, double3{0, 0, -1}));
  237. math::quaternion<float> moon_rotation =
  238. {
  239. static_cast<float>(moon_rotationd.w),
  240. static_cast<float>(moon_rotationd.x),
  241. static_cast<float>(moon_rotationd.y),
  242. static_cast<float>(moon_rotationd.z)
  243. };
  244. if (sun_light)
  245. {
  246. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_az_el[0], float3{0, 1, 0});
  247. math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_az_el[1], float3{-1, 0, 0});
  248. math::quaternion<float> sun_az_el_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  249. sun_light->set_rotation(sun_az_el_rotation);
  250. }
  251. if (moon_light)
  252. {
  253. math::quaternion<float> moon_azimuth_rotation = math::angle_axis(moon_az_el[0], float3{0, 1, 0});
  254. math::quaternion<float> moon_elevation_rotation = math::angle_axis(moon_az_el[1], float3{-1, 0, 0});
  255. math::quaternion<float> moon_az_el_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
  256. moon_light->set_rotation(moon_az_el_rotation);
  257. }
  258. std::size_t hour_index = static_cast<std::size_t>(hour);
  259. float lerp_factor = hour - std::floor(hour);
  260. float sun_gradient_position = static_cast<float>(std::max<double>(0.0, ((sun_az_el[1] + math::half_pi<double>) / math::pi<double>)));
  261. float moon_gradient_position = static_cast<float>(std::max<double>(0.0, ((moon_az_el[1] + math::half_pi<double>) / math::pi<double>)));
  262. float sky_gradient_position = sun_gradient_position;
  263. float ambient_gradient_position = sun_gradient_position;
  264. if (sky_pass)
  265. {
  266. float3 horizon_color = interpolate_gradient(horizon_colors, sun_gradient_position);
  267. float3 zenith_color = interpolate_gradient(zenith_colors, sun_gradient_position);
  268. float3 sun_color = interpolate_gradient(sun_colors, sun_gradient_position);
  269. float3 moon_color = interpolate_gradient(moon_colors, moon_gradient_position);
  270. float3 ambient_color = interpolate_gradient(ambient_colors, ambient_gradient_position);
  271. sun_light->set_color(sun_color);
  272. sun_light->set_intensity(1.0f);
  273. moon_light->set_color(moon_color);
  274. moon_light->set_intensity(1.0f);
  275. ambient_light->set_color(ambient_color);
  276. ambient_light->set_intensity(0.5f);
  277. sky_pass->set_horizon_color(horizon_color);
  278. sky_pass->set_zenith_color(zenith_color);
  279. sky_pass->set_time_of_day(static_cast<float>(hour * 60.0 * 60.0));
  280. sky_pass->set_observer_location(location[0], location[1], location[2]);
  281. sky_pass->set_sun_coordinates(sun_position, sun_az_el);
  282. sky_pass->set_moon_coordinates(moon_position, moon_az_el);
  283. sky_pass->set_julian_day(static_cast<float>(jd));
  284. sky_pass->set_moon_rotation(moon_rotation);
  285. }
  286. shadow_light = sun_light;
  287. if (shadow_map_pass)
  288. {
  289. if (sun_az_el[1] < 0.0f)
  290. {
  291. shadow_map_pass->set_light(moon_light);
  292. }
  293. else
  294. {
  295. shadow_map_pass->set_light(sun_light);
  296. }
  297. }
  298. if (material_pass)
  299. {
  300. float shadow_strength = interpolate_gradient(shadow_strengths, sun_gradient_position).x;
  301. material_pass->set_shadow_strength(shadow_strength);
  302. }
  303. }
  304. void weather_system::set_location(float latitude, float longitude, float altitude)
  305. {
  306. location = {latitude, longitude, altitude};
  307. }
  308. void weather_system::set_ambient_light(::ambient_light* light)
  309. {
  310. ambient_light = light;
  311. }
  312. void weather_system::set_sun_light(directional_light* light)
  313. {
  314. sun_light = light;
  315. }
  316. void weather_system::set_moon_light(directional_light* light)
  317. {
  318. moon_light = light;
  319. }
  320. void weather_system::set_sky_pass(::sky_pass* pass)
  321. {
  322. sky_pass = pass;
  323. if (sky_pass)
  324. {
  325. sky_pass->set_moon_angular_radius(math::radians(1.0f));
  326. sky_pass->set_sun_angular_radius(math::radians(1.1f));
  327. }
  328. }
  329. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  330. {
  331. shadow_map_pass = pass;
  332. if (shadow_map_pass)
  333. {
  334. shadow_map_pass->set_light(shadow_light);
  335. }
  336. }
  337. void weather_system::set_material_pass(::material_pass* pass)
  338. {
  339. material_pass = pass;
  340. }
  341. void weather_system::set_time(int year, int month, int day, int hour, int minute, int second, double tc)
  342. {
  343. double time = ((static_cast<double>(hour) - tc) + ((static_cast<double>(minute) + static_cast<double>(second) / 60.0) / 60.0)) / 24.0;
  344. jd = julian_day(year, month, day, time);
  345. }
  346. void weather_system::set_time_scale(float scale)
  347. {
  348. time_scale = scale;
  349. }
  350. void weather_system::set_sky_palette(const ::image* image)
  351. {
  352. load_palette(&horizon_colors, image, 0);
  353. load_palette(&zenith_colors, image, 1);
  354. }
  355. void weather_system::set_sun_palette(const ::image* image)
  356. {
  357. load_palette(&sun_colors, image, 0);
  358. }
  359. void weather_system::set_moon_palette(const ::image* image)
  360. {
  361. load_palette(&moon_colors, image, 0);
  362. }
  363. void weather_system::set_ambient_palette(const ::image* image)
  364. {
  365. load_palette(&ambient_colors, image, 0);
  366. }
  367. void weather_system::set_shadow_palette(const ::image* image)
  368. {
  369. load_palette(&shadow_strengths, image, 0);
  370. }
  371. void weather_system::load_palette(std::vector<float3>* palette, const ::image* image, unsigned int row)
  372. {
  373. unsigned int w = image->get_width();
  374. unsigned int h = image->get_height();
  375. unsigned int c = image->get_channels();
  376. unsigned int y = std::min<unsigned int>(row, h - 1);
  377. palette->clear();
  378. if (image->is_hdr())
  379. {
  380. const float* pixels = static_cast<const float*>(image->get_pixels());
  381. for (unsigned int x = 0; x < w; ++x)
  382. {
  383. unsigned int i = y * w * c + x * c;
  384. float r = pixels[i];
  385. float g = pixels[i + 1];
  386. float b = pixels[i + 2];
  387. palette->push_back(float3{r, g, b});
  388. }
  389. }
  390. else
  391. {
  392. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  393. for (unsigned int x = 0; x < w; ++x)
  394. {
  395. unsigned int i = y * w * c + x * c;
  396. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  397. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  398. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  399. palette->push_back(float3{r, g, b});
  400. }
  401. }
  402. }
  403. float3 weather_system::interpolate_gradient(const std::vector<float3>& gradient, float position)
  404. {
  405. position *= static_cast<float>(gradient.size() - 1);
  406. int index0 = static_cast<int>(position) % gradient.size();
  407. int index1 = (index0 + 1) % gradient.size();
  408. return math::lerp<float3>(gradient[index0], gradient[index1], position - std::floor(position));
  409. }