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

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