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

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