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

547 lines
17 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. sky_palette(nullptr),
  158. shadow_palette(nullptr),
  159. sun_direction{0.0f, -1.0f, 0.0f},
  160. location{0.0f, 0.0f, 0.0f},
  161. jd(0.0)
  162. {}
  163. void weather_system::update(double t, double dt)
  164. {
  165. jd += (dt * time_scale) / seconds_per_day;
  166. const float latitude = location[0];
  167. const float longitude = location[1];
  168. // Time correction
  169. double tc = longitude / (math::two_pi<double> / 24.0);
  170. //double pst_tc = -7.0;
  171. double local_jd = jd + tc / 24.0 - 0.5;
  172. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  173. double hour = local_time;
  174. // Calculate equation of time
  175. //float eot_b = (360.0f / 365.0f) * (day_of_year - 81.0f);
  176. //float eot = 9.87f * std::sin(eot_b * 2.0f) - 7.53f * std::cos(eot_b) - 1.5f * std::sin(eot_b);
  177. // Calculate local mean sidereal time (LST)
  178. //double tc = longitude / (math::two_pi<double> / 24.0); // Time correction
  179. //double ut = local_time + tc; // Universal time
  180. double gmst = jd_to_gmst(jd);
  181. double lmst = gmst + longitude;
  182. // Calculate sun position
  183. //float local_solar_time = local_time;// + eot / 60.0f;
  184. /*
  185. float sun_declination = math::radians(23.45f) * std::sin((math::two_pi<float> / 365.0f) * (284.0f + day_of_year));
  186. float sun_hour_angle = math::radians(15.0f) * (local_solar_time - 12.0f);
  187. sun_elevation = std::asin(std::sin(sun_declination) * std::sin(latitude) + std::cos(sun_declination) * std::cos(sun_hour_angle) * std::cos(latitude));
  188. 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));
  189. if (sun_hour_angle > 0.0f)
  190. sun_azimuth = math::two_pi<float> - sun_azimuth;
  191. */
  192. // J2000 day
  193. double d = jd - 2451545.0;
  194. // Obliquity of the ecliptic
  195. double ecl = math::radians<double>(23.4393 - 3.563e-7 * d);
  196. // Calculation sun coordinates
  197. double sun_longitude;
  198. double sun_latitude;
  199. double sun_distance;
  200. double sun_right_ascension;
  201. double sun_declination;
  202. double sun_azimuth;
  203. double sun_elevation;
  204. find_sun_ecliptic(jd, &sun_longitude, &sun_latitude, &sun_distance);
  205. ecliptic_to_equatorial(sun_longitude, sun_latitude, ecl, &sun_right_ascension, &sun_declination);
  206. equatorial_to_horizontal(sun_right_ascension, sun_declination, lmst, latitude, &sun_azimuth, &sun_elevation);
  207. // Calculate moon coordinates
  208. double moon_longitude;
  209. double moon_latitude;
  210. double moon_distance;
  211. double moon_right_ascension;
  212. double moon_declination;
  213. double moon_azimuth;
  214. double moon_elevation;
  215. find_moon_ecliptic(jd, &moon_longitude, &moon_latitude, &moon_distance);
  216. ecliptic_to_equatorial(moon_longitude, moon_latitude, ecl, &moon_right_ascension, &moon_declination);
  217. equatorial_to_horizontal(moon_right_ascension, moon_declination, lmst, latitude, &moon_azimuth, &moon_elevation);
  218. /*
  219. std::cout.precision(10);
  220. std::cout << std::fixed;
  221. //std::cout << "gmst: " << math::degrees<double>(gmst) << std::endl;
  222. std::cout << "JD: " << jd << std::endl;
  223. std::cout << "PST: " << pst_time << std::endl;
  224. std::cout << "AZ: " << math::degrees(sun_azimuth) << std::endl;
  225. std::cout << "EL: " << math::degrees(sun_elevation) << std::endl;
  226. std::cout << "DEC: " << math::degrees(sun_declination) << std::endl;
  227. //std::cout << "eOT: " << eot << std::endl;
  228. */
  229. float2 sun_az_el = float2{static_cast<float>(sun_azimuth), static_cast<float>(sun_elevation)};
  230. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_az_el[0], float3{0, 1, 0});
  231. math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_az_el[1], float3{-1, 0, 0});
  232. math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  233. float3 sun_position = math::normalize(sun_rotation * float3{0, 0, -1});
  234. float2 moon_az_el = float2{static_cast<float>(moon_azimuth), static_cast<float>(moon_elevation)};
  235. math::quaternion<float> moon_azimuth_rotation = math::angle_axis(moon_az_el[0], float3{0, 1, 0});
  236. math::quaternion<float> moon_elevation_rotation = math::angle_axis(moon_az_el[1], float3{-1, 0, 0});
  237. math::quaternion<float> moon_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
  238. float3 moon_position = math::normalize(moon_rotation * float3{0, 0, -1});
  239. if (sun_light)
  240. {
  241. sun_light->set_rotation(sun_rotation);
  242. }
  243. if (moon_light)
  244. {
  245. moon_light->set_rotation(moon_rotation);
  246. }
  247. std::size_t hour_index = static_cast<std::size_t>(hour);
  248. float lerp_factor = hour - std::floor(hour);
  249. float sun_gradient_position = static_cast<float>(std::max<double>(0.0, ((sun_elevation + math::half_pi<double>) / math::pi<double>)));
  250. float moon_gradient_position = static_cast<float>(std::max<double>(0.0, ((moon_elevation + math::half_pi<double>) / math::pi<double>)));
  251. float sky_gradient_position = sun_gradient_position;
  252. float ambient_gradient_position = sun_gradient_position;
  253. if (sky_pass)
  254. {
  255. //std::cout << "sungrad: " << (sun_gradient_position* static_cast<float>(sky_gradients.size() - 1)) << std::endl;
  256. //std::cout << "sunel: " << math::degrees(sun_elevation) << std::endl;
  257. std::array<float4, 4> sky_gradient;
  258. {
  259. sky_gradient_position *= static_cast<float>(sky_gradients.size() - 1);
  260. int index0 = static_cast<int>(sky_gradient_position) % sky_gradients.size();
  261. int index1 = (index0 + 1) % sky_gradients.size();
  262. sky_gradient_position -= std::floor(sky_gradient_position);
  263. for (int i = 0; i < 4; ++i)
  264. sky_gradient[i] = math::lerp(sky_gradients[index0][i], sky_gradients[index1][i], sky_gradient_position);
  265. }
  266. float3 sun_color = interpolate_gradient(sun_colors, sun_gradient_position);
  267. float3 moon_color = interpolate_gradient(moon_colors, moon_gradient_position);
  268. float3 ambient_color = interpolate_gradient(ambient_colors, ambient_gradient_position);
  269. sun_light->set_color(sun_color);
  270. moon_light->set_color(moon_color);
  271. moon_light->set_intensity(1.0f);
  272. ambient_light->set_color(ambient_color);
  273. sky_pass->set_sky_gradient(sky_gradient);
  274. sky_pass->set_time_of_day(static_cast<float>(hour * 60.0 * 60.0));
  275. sky_pass->set_observer_location(location[0], location[1], location[2]);
  276. sky_pass->set_sun_coordinates(sun_position, sun_az_el);
  277. sky_pass->set_moon_coordinates(moon_position, moon_az_el);
  278. sky_pass->set_julian_day(static_cast<float>(jd));
  279. }
  280. shadow_light = sun_light;
  281. if (shadow_map_pass)
  282. {
  283. if (sun_elevation < 0.0f)
  284. {
  285. shadow_map_pass->set_light(moon_light);
  286. }
  287. else
  288. {
  289. shadow_map_pass->set_light(sun_light);
  290. }
  291. }
  292. if (material_pass)
  293. {
  294. float shadow_strength = interpolate_gradient(shadow_strengths, sun_gradient_position);
  295. material_pass->set_shadow_strength(shadow_strength);
  296. }
  297. }
  298. void weather_system::set_location(float latitude, float longitude, float altitude)
  299. {
  300. location = {latitude, longitude, altitude};
  301. }
  302. void weather_system::set_ambient_light(::ambient_light* light)
  303. {
  304. ambient_light = light;
  305. }
  306. void weather_system::set_sun_light(directional_light* light)
  307. {
  308. sun_light = light;
  309. }
  310. void weather_system::set_moon_light(directional_light* light)
  311. {
  312. moon_light = light;
  313. }
  314. void weather_system::set_sky_pass(::sky_pass* pass)
  315. {
  316. sky_pass = pass;
  317. if (sky_pass)
  318. {
  319. sky_pass->set_moon_angular_radius(math::radians(1.0f));
  320. sky_pass->set_sun_angular_radius(math::radians(1.1f));
  321. }
  322. }
  323. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  324. {
  325. shadow_map_pass = pass;
  326. if (shadow_map_pass)
  327. {
  328. shadow_map_pass->set_light(shadow_light);
  329. }
  330. }
  331. void weather_system::set_material_pass(::material_pass* pass)
  332. {
  333. material_pass = pass;
  334. if (material_pass)
  335. {
  336. material_pass->set_shadow_strength(0.75f);
  337. }
  338. }
  339. void weather_system::set_time(int year, int month, int day, int hour, int minute, int second, double tc)
  340. {
  341. double time = ((static_cast<double>(hour) - tc) + ((static_cast<double>(minute) + static_cast<double>(second) / 60.0) / 60.0)) / 24.0;
  342. jd = julian_day(year, month, day, time);
  343. }
  344. void weather_system::set_time_scale(float scale)
  345. {
  346. time_scale = scale;
  347. }
  348. void weather_system::set_sky_palette(const ::image* image)
  349. {
  350. sky_palette = image;
  351. if (sky_palette)
  352. {
  353. unsigned int w = image->get_width();
  354. unsigned int h = image->get_height();
  355. unsigned int c = image->get_channels();
  356. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  357. for (unsigned int x = 0; x < w; ++x)
  358. {
  359. std::array<float4, 4> gradient;
  360. for (unsigned int y = 0; y < std::min<unsigned int>(4, h); ++y)
  361. {
  362. unsigned int i = y * w * c + x * c;
  363. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  364. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  365. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  366. gradient[y] = {r, g, b, static_cast<float>(y) * (1.0f / 3.0f)};
  367. }
  368. sky_gradients.push_back(gradient);
  369. }
  370. }
  371. }
  372. void weather_system::set_sun_palette(const ::image* image)
  373. {
  374. sun_palette = image;
  375. if (sun_palette)
  376. {
  377. unsigned int w = image->get_width();
  378. unsigned int h = image->get_height();
  379. unsigned int c = image->get_channels();
  380. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  381. for (unsigned int x = 0; x < w; ++x)
  382. {
  383. float3 color;
  384. unsigned int y = 0;
  385. unsigned int i = y * w * c + x * c;
  386. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  387. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  388. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  389. color = {r, g, b};
  390. sun_colors.push_back(color);
  391. }
  392. }
  393. }
  394. void weather_system::set_moon_palette(const ::image* image)
  395. {
  396. moon_palette = image;
  397. if (moon_palette)
  398. {
  399. unsigned int w = image->get_width();
  400. unsigned int h = image->get_height();
  401. unsigned int c = image->get_channels();
  402. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  403. for (unsigned int x = 0; x < w; ++x)
  404. {
  405. float3 color;
  406. unsigned int y = 0;
  407. unsigned int i = y * w * c + x * c;
  408. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  409. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  410. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  411. color = {r, g, b};
  412. moon_colors.push_back(color);
  413. }
  414. }
  415. }
  416. void weather_system::set_ambient_palette(const ::image* image)
  417. {
  418. ambient_palette = image;
  419. if (ambient_palette)
  420. {
  421. unsigned int w = image->get_width();
  422. unsigned int h = image->get_height();
  423. unsigned int c = image->get_channels();
  424. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  425. for (unsigned int x = 0; x < w; ++x)
  426. {
  427. float3 color;
  428. unsigned int y = 0;
  429. unsigned int i = y * w * c + x * c;
  430. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  431. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  432. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  433. color = {r, g, b};
  434. ambient_colors.push_back(color);
  435. }
  436. }
  437. }
  438. void weather_system::set_shadow_palette(const ::image* image)
  439. {
  440. shadow_palette = image;
  441. if (shadow_palette)
  442. {
  443. unsigned int w = image->get_width();
  444. unsigned int h = image->get_height();
  445. unsigned int c = image->get_channels();
  446. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  447. for (unsigned int x = 0; x < w; ++x)
  448. {
  449. unsigned int y = 0;
  450. unsigned int i = y * w * c + x * c;
  451. float r = 1.0f - (static_cast<float>(pixels[i]) / 255.0f);
  452. shadow_strengths.push_back(r);
  453. }
  454. }
  455. }