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

541 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 = std::atan2(horiz_y, horiz_x) + math::pi<double>;
  135. *elevation = 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. coordinates{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 = coordinates[0];
  167. const float longitude = coordinates[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. if (sky_pass)
  250. {
  251. const std::array<float4, 4>& gradient0 = sky_gradients[hour_index];
  252. const std::array<float4, 4>& gradient1 = sky_gradients[(hour_index + 1) % sky_gradients.size()];
  253. std::array<float4, 4> gradient;
  254. for (int i = 0; i < 4; ++i)
  255. {
  256. gradient[i] = math::lerp(gradient0[i], gradient1[i], lerp_factor);
  257. }
  258. float3 sun_color0 = sun_colors[hour_index];
  259. float3 sun_color1 = sun_colors[(hour_index + 1) % sun_colors.size()];
  260. float3 sun_color = math::lerp(sun_color0, sun_color1, lerp_factor);
  261. float3 moon_color0 = moon_colors[hour_index];
  262. float3 moon_color1 = moon_colors[(hour_index + 1) % moon_colors.size()];
  263. float3 moon_color = math::lerp(moon_color0, moon_color1, lerp_factor);
  264. float3 ambient_color0 = ambient_colors[hour_index];
  265. float3 ambient_color1 = ambient_colors[(hour_index + 1) % sun_colors.size()];
  266. float3 ambient_color = math::lerp(ambient_color0, ambient_color1, lerp_factor);
  267. sun_light->set_color(sun_color);
  268. moon_light->set_color(moon_color);
  269. moon_light->set_intensity(1.0f);
  270. ambient_light->set_color(ambient_color);
  271. sky_pass->set_sky_gradient(gradient);
  272. sky_pass->set_time_of_day(hour * 60.0 * 60.0);
  273. sky_pass->set_observer_coordinates(coordinates);
  274. sky_pass->set_sun_coordinates(sun_position, sun_az_el);
  275. sky_pass->set_moon_coordinates(moon_position, moon_az_el);
  276. }
  277. shadow_light = sun_light;
  278. if (shadow_map_pass)
  279. {
  280. if (sun_elevation < 0.0f)
  281. {
  282. shadow_map_pass->set_light(moon_light);
  283. }
  284. else
  285. {
  286. shadow_map_pass->set_light(sun_light);
  287. }
  288. }
  289. if (material_pass)
  290. {
  291. float shadow_strength0 = shadow_strengths[hour_index];
  292. float shadow_strength1 = shadow_strengths[(hour_index + 1) % shadow_strengths.size()];
  293. float shadow_strength = math::lerp(shadow_strength0, shadow_strength1, lerp_factor);
  294. material_pass->set_shadow_strength(shadow_strength);
  295. }
  296. }
  297. void weather_system::set_coordinates(const float2& coordinates)
  298. {
  299. this->coordinates = coordinates;
  300. }
  301. void weather_system::set_ambient_light(::ambient_light* light)
  302. {
  303. this->ambient_light = light;
  304. }
  305. void weather_system::set_sun_light(directional_light* light)
  306. {
  307. sun_light = light;
  308. }
  309. void weather_system::set_moon_light(directional_light* light)
  310. {
  311. moon_light = light;
  312. }
  313. void weather_system::set_sky_pass(::sky_pass* pass)
  314. {
  315. sky_pass = pass;
  316. }
  317. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  318. {
  319. shadow_map_pass = pass;
  320. if (shadow_map_pass)
  321. {
  322. shadow_map_pass->set_light(shadow_light);
  323. }
  324. }
  325. void weather_system::set_material_pass(::material_pass* pass)
  326. {
  327. material_pass = pass;
  328. if (material_pass)
  329. {
  330. material_pass->set_shadow_strength(0.75f);
  331. }
  332. }
  333. void weather_system::set_time(int year, int month, int day, int hour, int minute, int second, double tc)
  334. {
  335. double time = ((static_cast<double>(hour) - tc) + ((static_cast<double>(minute) + static_cast<double>(second) / 60.0) / 60.0)) / 24.0;
  336. jd = julian_day(year, month, day, time);
  337. }
  338. void weather_system::set_time_scale(float scale)
  339. {
  340. time_scale = scale;
  341. }
  342. void weather_system::set_sky_palette(const ::image* image)
  343. {
  344. sky_palette = image;
  345. if (sky_palette)
  346. {
  347. unsigned int w = image->get_width();
  348. unsigned int h = image->get_height();
  349. unsigned int c = image->get_channels();
  350. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  351. for (unsigned int x = 0; x < w; ++x)
  352. {
  353. std::array<float4, 4> gradient;
  354. for (unsigned int y = 0; y < std::min<unsigned int>(4, h); ++y)
  355. {
  356. unsigned int i = y * w * c + x * c;
  357. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  358. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  359. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  360. gradient[y] = {r, g, b, static_cast<float>(y) * (1.0f / 3.0f)};
  361. }
  362. sky_gradients.push_back(gradient);
  363. }
  364. }
  365. }
  366. void weather_system::set_sun_palette(const ::image* image)
  367. {
  368. sun_palette = image;
  369. if (sun_palette)
  370. {
  371. unsigned int w = image->get_width();
  372. unsigned int h = image->get_height();
  373. unsigned int c = image->get_channels();
  374. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  375. for (unsigned int x = 0; x < w; ++x)
  376. {
  377. float3 color;
  378. unsigned int y = 0;
  379. unsigned int i = y * w * c + x * c;
  380. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  381. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  382. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  383. color = {r, g, b};
  384. sun_colors.push_back(color);
  385. }
  386. }
  387. }
  388. void weather_system::set_moon_palette(const ::image* image)
  389. {
  390. moon_palette = image;
  391. if (moon_palette)
  392. {
  393. unsigned int w = image->get_width();
  394. unsigned int h = image->get_height();
  395. unsigned int c = image->get_channels();
  396. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  397. for (unsigned int x = 0; x < w; ++x)
  398. {
  399. float3 color;
  400. unsigned int y = 0;
  401. unsigned int i = y * w * c + x * c;
  402. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  403. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  404. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  405. color = {r, g, b};
  406. moon_colors.push_back(color);
  407. }
  408. }
  409. }
  410. void weather_system::set_ambient_palette(const ::image* image)
  411. {
  412. ambient_palette = image;
  413. if (ambient_palette)
  414. {
  415. unsigned int w = image->get_width();
  416. unsigned int h = image->get_height();
  417. unsigned int c = image->get_channels();
  418. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  419. for (unsigned int x = 0; x < w; ++x)
  420. {
  421. float3 color;
  422. unsigned int y = 0;
  423. unsigned int i = y * w * c + x * c;
  424. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  425. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  426. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  427. color = {r, g, b};
  428. ambient_colors.push_back(color);
  429. }
  430. }
  431. }
  432. void weather_system::set_shadow_palette(const ::image* image)
  433. {
  434. shadow_palette = image;
  435. if (shadow_palette)
  436. {
  437. unsigned int w = image->get_width();
  438. unsigned int h = image->get_height();
  439. unsigned int c = image->get_channels();
  440. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  441. for (unsigned int x = 0; x < w; ++x)
  442. {
  443. unsigned int y = 0;
  444. unsigned int i = y * w * c + x * c;
  445. float r = 1.0f - (static_cast<float>(pixels[i]) / 255.0f);
  446. shadow_strengths.push_back(r);
  447. }
  448. }
  449. }