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

544 lines
16 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. void find_sun_ecliptic(double jd, double* longitude, double* latitude, double* distance)
  52. {
  53. const double t = (jd - 2451545.0) / 36525.0;
  54. const double m = 6.24 + 628.302 * t;
  55. *longitude = 4.895048 + 628.331951 * t + (0.033417 - 0.000084 * t) * std::sin(m) + 0.000351 * std::sin(m * 2.0);
  56. *latitude = 0.0;
  57. *distance = 1.000140 - (0.016708 - 0.000042 * t) * std::cos(m) - 0.000141 * std::cos(m * 2.0);
  58. }
  59. /**
  60. * Calculates the ecliptic geocentric coordinates of the moon, given a Julian day.
  61. *
  62. * @param[in] jd Julian day.
  63. * @param[out] longitude Ecliptic longitude of the moon, in radians.
  64. * @param[out] latitude Ecliptic latitude of the moon, in radians.
  65. * @param[out] distance Distance to the moon, in Earth radii.
  66. * @return Array containing the ecliptic longitude and latitude of the moon, in radians.
  67. *
  68. * @see A Physically-Based Night Sky Model
  69. */
  70. void find_moon_ecliptic(double jd, double* longitude, double* latitude, double* distance)
  71. {
  72. const double t = (jd - 2451545.0) / 36525.0;
  73. const double l1 = 3.8104 + 8399.7091 * t;
  74. const double m1 = 2.3554 + 8328.6911 * t;
  75. const double m = 6.2300 + 628.3019 * t;
  76. const double d = 5.1985 + 7771.3772 * t;
  77. const double d2 = d * 2.0;
  78. const double f = 1.6280 + 8433.4663 * t;
  79. *longitude = l1
  80. + 0.1098 * std::sin(m1)
  81. + 0.0222 * std::sin(d2 - m1)
  82. + 0.0115 * std::sin(d2)
  83. + 0.0037 * std::sin(m1 * 2.0)
  84. - 0.0032 * std::sin(m)
  85. - 0.0020 * std::sin(d2)
  86. + 0.0010 * std::sin(d2 - m1 * 2.0)
  87. + 0.0010 * std::sin(d2 - m - m1)
  88. + 0.0009 * std::sin(d2 + m1)
  89. + 0.0008 * std::sin(d2 - m)
  90. + 0.0007 * std::sin(m1 - m)
  91. - 0.0006 * std::sin(d)
  92. - 0.0005 * std::sin(m + m1);
  93. *latitude = 0.0895 * sin(f)
  94. + 0.0049 * std::sin(m1 + f)
  95. + 0.0048 * std::sin(m1 - f)
  96. + 0.0030 * std::sin(d2 - f)
  97. + 0.0010 * std::sin(d2 + f - m1)
  98. + 0.0008 * std::sin(d2 - f - m1)
  99. + 0.0006 * std::sin(d2 + f);
  100. *distance = 1.0 / (0.016593
  101. + 0.000904 * std::cos(m1)
  102. + 0.000166 * std::cos(d2 - m1)
  103. + 0.000137 * std::cos(d2)
  104. + 0.000049 * std::cos(m1 * 2.0)
  105. + 0.000015 * std::cos(d2 + m1)
  106. + 0.000009 * std::cos(d2 - m));
  107. }
  108. void ecliptic_to_equatorial(double longitude, double latitude, double ecl, double* right_ascension, double* declination)
  109. {
  110. double eclip_x = std::cos(longitude) * std::cos(latitude);
  111. double eclip_y = std::sin(longitude) * std::cos(latitude);
  112. double eclip_z = std::sin(latitude);
  113. double equat_x = eclip_x;
  114. double equat_y = eclip_y * std::cos(ecl) - eclip_z * std::sin(ecl);
  115. double equat_z = eclip_y * std::sin(ecl) + eclip_z * std::cos(ecl);
  116. *right_ascension = std::atan2(equat_y, equat_x);
  117. *declination = std::atan2(equat_z, sqrt(equat_x * equat_x + equat_y * equat_y));
  118. }
  119. void equatorial_to_horizontal(double right_ascension, double declination, double lmst, double latitude, double* azimuth, double* elevation)
  120. {
  121. double hour_angle = lmst - right_ascension;
  122. double x = std::cos(hour_angle) * std::cos(declination);
  123. double y = std::sin(hour_angle) * std::cos(declination);
  124. double z = std::sin(declination);
  125. double horiz_x = x * std::cos(math::half_pi<double> - latitude) - z * std::sin(math::half_pi<double> - latitude);
  126. double horiz_y = y;
  127. double horiz_z = x * std::sin(math::half_pi<double> - latitude) + z * std::cos(math::half_pi<double> - latitude);
  128. *azimuth = std::atan2(horiz_y, horiz_x) + math::pi<double>;
  129. *elevation = std::atan2(horiz_z, std::sqrt(horiz_x * horiz_x + horiz_y * horiz_y));
  130. }
  131. /**
  132. * Calculates the Greenwich mean sidereal time (GMST) from a Julian day.
  133. *
  134. * @param jd Julian day.
  135. * @return GMST, in radians.
  136. */
  137. static double jd_to_gmst(double jd)
  138. {
  139. return math::wrap_radians<double>(4.894961212 + 6.300388098 * (jd - 2451545.0));
  140. }
  141. weather_system::weather_system(entt::registry& registry):
  142. entity_system(registry),
  143. ambient_light(nullptr),
  144. sun_light(nullptr),
  145. moon_light(nullptr),
  146. shadow_light(nullptr),
  147. sky_pass(nullptr),
  148. shadow_map_pass(nullptr),
  149. material_pass(nullptr),
  150. time_scale(1.0f),
  151. sky_palette(nullptr),
  152. shadow_palette(nullptr),
  153. sun_direction{0.0f, -1.0f, 0.0f},
  154. coordinates{0.0f, 0.0f},
  155. jd(0.0)
  156. {}
  157. void weather_system::update(double t, double dt)
  158. {
  159. jd += (dt * time_scale) / seconds_per_day;
  160. const float latitude = coordinates[0];
  161. const float longitude = coordinates[1];
  162. // Time correction
  163. double tc = longitude / (math::two_pi<double> / 24.0);
  164. //double pst_tc = -7.0;
  165. double local_jd = jd + tc / 24.0 - 0.5;
  166. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  167. double hour = local_time;
  168. // Calculate equation of time
  169. //float eot_b = (360.0f / 365.0f) * (day_of_year - 81.0f);
  170. //float eot = 9.87f * std::sin(eot_b * 2.0f) - 7.53f * std::cos(eot_b) - 1.5f * std::sin(eot_b);
  171. // Calculate local mean sidereal time (LST)
  172. //double tc = longitude / (math::two_pi<double> / 24.0); // Time correction
  173. //double ut = local_time + tc; // Universal time
  174. double gmst = jd_to_gmst(jd);
  175. double lmst = gmst + longitude;
  176. // Calculate sun position
  177. //float local_solar_time = local_time;// + eot / 60.0f;
  178. /*
  179. float sun_declination = math::radians(23.45f) * std::sin((math::two_pi<float> / 365.0f) * (284.0f + day_of_year));
  180. float sun_hour_angle = math::radians(15.0f) * (local_solar_time - 12.0f);
  181. sun_elevation = std::asin(std::sin(sun_declination) * std::sin(latitude) + std::cos(sun_declination) * std::cos(sun_hour_angle) * std::cos(latitude));
  182. 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));
  183. if (sun_hour_angle > 0.0f)
  184. sun_azimuth = math::two_pi<float> - sun_azimuth;
  185. */
  186. // J2000 day
  187. double d = jd - 2451545.0;
  188. // Obliquity of the ecliptic
  189. double ecl = math::radians<double>(23.4393 - 3.563e-7 * d);
  190. // Calculation sun position
  191. double sun_longitude;
  192. double sun_latitude;
  193. double sun_distance;
  194. double sun_right_ascension;
  195. double sun_declination;
  196. double sun_azimuth;
  197. double sun_elevation;
  198. find_sun_ecliptic(jd, &sun_longitude, &sun_latitude, &sun_distance);
  199. ecliptic_to_equatorial(sun_longitude, sun_latitude, ecl, &sun_right_ascension, &sun_declination);
  200. equatorial_to_horizontal(sun_right_ascension, sun_declination, lmst, latitude, &sun_azimuth, &sun_elevation);
  201. // Calculate moon position
  202. double moon_longitude;
  203. double moon_latitude;
  204. double moon_distance;
  205. double moon_right_ascension;
  206. double moon_declination;
  207. double moon_azimuth;
  208. double moon_elevation;
  209. find_moon_ecliptic(jd, &moon_longitude, &moon_latitude, &moon_distance);
  210. ecliptic_to_equatorial(moon_longitude, moon_latitude, ecl, &moon_right_ascension, &moon_declination);
  211. equatorial_to_horizontal(moon_right_ascension, moon_declination, lmst, latitude, &moon_azimuth, &moon_elevation);
  212. /*
  213. std::cout.precision(10);
  214. std::cout << std::fixed;
  215. //std::cout << "gmst: " << math::degrees<double>(gmst) << std::endl;
  216. std::cout << "JD: " << jd << std::endl;
  217. std::cout << "PST: " << pst_time << std::endl;
  218. std::cout << "AZ: " << math::degrees(sun_azimuth) << std::endl;
  219. std::cout << "EL: " << math::degrees(sun_elevation) << std::endl;
  220. std::cout << "DEC: " << math::degrees(sun_declination) << std::endl;
  221. //std::cout << "eOT: " << eot << std::endl;
  222. */
  223. if (sun_light)
  224. {
  225. math::quaternion<float> sun_azimuth_rotation = math::angle_axis((float)sun_azimuth, float3{0, 1, 0});
  226. math::quaternion<float> sun_elevation_rotation = math::angle_axis((float)sun_elevation, float3{-1, 0, 0});
  227. math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  228. sun_direction = math::normalize(sun_rotation * float3{0, 0, -1});
  229. sun_light->set_rotation(sun_rotation);
  230. }
  231. if (moon_light)
  232. {
  233. math::quaternion<float> moon_azimuth_rotation = math::angle_axis((float)moon_azimuth, float3{0, 1, 0});
  234. math::quaternion<float> moon_elevation_rotation = math::angle_axis((float)moon_elevation, float3{-1, 0, 0});
  235. math::quaternion<float> moon_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
  236. moon_light->set_rotation(moon_rotation);
  237. }
  238. std::size_t hour_index = static_cast<std::size_t>(hour);
  239. float lerp_factor = hour - std::floor(hour);
  240. if (sky_pass)
  241. {
  242. const std::array<float4, 4>& gradient0 = sky_gradients[hour_index];
  243. const std::array<float4, 4>& gradient1 = sky_gradients[(hour_index + 1) % sky_gradients.size()];
  244. std::array<float4, 4> gradient;
  245. for (int i = 0; i < 4; ++i)
  246. {
  247. gradient[i] = math::lerp(gradient0[i], gradient1[i], lerp_factor);
  248. }
  249. float3 sun_color0 = sun_colors[hour_index];
  250. float3 sun_color1 = sun_colors[(hour_index + 1) % sun_colors.size()];
  251. float3 sun_color = math::lerp(sun_color0, sun_color1, lerp_factor);
  252. float3 moon_color0 = moon_colors[hour_index];
  253. float3 moon_color1 = moon_colors[(hour_index + 1) % moon_colors.size()];
  254. float3 moon_color = math::lerp(moon_color0, moon_color1, lerp_factor);
  255. float3 ambient_color0 = ambient_colors[hour_index];
  256. float3 ambient_color1 = ambient_colors[(hour_index + 1) % sun_colors.size()];
  257. float3 ambient_color = math::lerp(ambient_color0, ambient_color1, lerp_factor);
  258. sun_light->set_color(sun_color);
  259. moon_light->set_color(moon_color);
  260. moon_light->set_intensity(1.0f);
  261. ambient_light->set_color(ambient_color);
  262. sky_pass->set_sky_gradient(gradient);
  263. sky_pass->set_time_of_day(hour * 60.0 * 60.0);
  264. }
  265. shadow_light = sun_light;
  266. if (shadow_map_pass)
  267. {
  268. if (sun_elevation < 0.0f)
  269. {
  270. shadow_map_pass->set_light(moon_light);
  271. }
  272. else
  273. {
  274. shadow_map_pass->set_light(sun_light);
  275. }
  276. }
  277. if (material_pass)
  278. {
  279. float shadow_strength0 = shadow_strengths[hour_index];
  280. float shadow_strength1 = shadow_strengths[(hour_index + 1) % shadow_strengths.size()];
  281. float shadow_strength = math::lerp(shadow_strength0, shadow_strength1, lerp_factor);
  282. material_pass->set_shadow_strength(shadow_strength);
  283. }
  284. }
  285. void weather_system::set_coordinates(const float2& coordinates)
  286. {
  287. this->coordinates = coordinates;
  288. }
  289. void weather_system::set_ambient_light(::ambient_light* light)
  290. {
  291. this->ambient_light = light;
  292. }
  293. void weather_system::set_sun_light(directional_light* light)
  294. {
  295. sun_light = light;
  296. if (sky_pass)
  297. {
  298. sky_pass->set_sun_light(sun_light);
  299. }
  300. }
  301. void weather_system::set_moon_light(directional_light* light)
  302. {
  303. moon_light = light;
  304. if (sky_pass)
  305. {
  306. sky_pass->set_moon_light(moon_light);
  307. }
  308. }
  309. void weather_system::set_sky_pass(::sky_pass* pass)
  310. {
  311. sky_pass = pass;
  312. if (sky_pass)
  313. {
  314. sky_pass->set_sun_light(sun_light);
  315. sky_pass->set_moon_light(moon_light);
  316. }
  317. }
  318. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  319. {
  320. shadow_map_pass = pass;
  321. if (shadow_map_pass)
  322. {
  323. shadow_map_pass->set_light(shadow_light);
  324. }
  325. }
  326. void weather_system::set_material_pass(::material_pass* pass)
  327. {
  328. material_pass = pass;
  329. if (material_pass)
  330. {
  331. material_pass->set_shadow_strength(0.75f);
  332. }
  333. }
  334. void weather_system::set_time(int year, int month, int day, int hour, int minute, int second, double tc)
  335. {
  336. double time = ((static_cast<double>(hour) - tc) + ((static_cast<double>(minute) + static_cast<double>(second) / 60.0) / 60.0)) / 24.0;
  337. jd = julian_day(year, month, day, time);
  338. }
  339. void weather_system::set_time_scale(float scale)
  340. {
  341. time_scale = scale;
  342. }
  343. void weather_system::set_sky_palette(const ::image* image)
  344. {
  345. sky_palette = image;
  346. if (sky_palette)
  347. {
  348. unsigned int w = image->get_width();
  349. unsigned int h = image->get_height();
  350. unsigned int c = image->get_channels();
  351. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  352. for (unsigned int x = 0; x < w; ++x)
  353. {
  354. std::array<float4, 4> gradient;
  355. for (unsigned int y = 0; y < std::min<unsigned int>(4, h); ++y)
  356. {
  357. unsigned int i = y * w * c + x * c;
  358. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  359. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  360. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  361. gradient[y] = {r, g, b, static_cast<float>(y) * (1.0f / 3.0f)};
  362. }
  363. sky_gradients.push_back(gradient);
  364. }
  365. }
  366. }
  367. void weather_system::set_sun_palette(const ::image* image)
  368. {
  369. sun_palette = image;
  370. if (sun_palette)
  371. {
  372. unsigned int w = image->get_width();
  373. unsigned int h = image->get_height();
  374. unsigned int c = image->get_channels();
  375. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  376. for (unsigned int x = 0; x < w; ++x)
  377. {
  378. float3 color;
  379. unsigned int y = 0;
  380. unsigned int i = y * w * c + x * c;
  381. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  382. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  383. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  384. color = {r, g, b};
  385. sun_colors.push_back(color);
  386. }
  387. }
  388. }
  389. void weather_system::set_moon_palette(const ::image* image)
  390. {
  391. moon_palette = image;
  392. if (moon_palette)
  393. {
  394. unsigned int w = image->get_width();
  395. unsigned int h = image->get_height();
  396. unsigned int c = image->get_channels();
  397. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  398. for (unsigned int x = 0; x < w; ++x)
  399. {
  400. float3 color;
  401. unsigned int y = 0;
  402. unsigned int i = y * w * c + x * c;
  403. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  404. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  405. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  406. color = {r, g, b};
  407. moon_colors.push_back(color);
  408. }
  409. }
  410. }
  411. void weather_system::set_ambient_palette(const ::image* image)
  412. {
  413. ambient_palette = image;
  414. if (ambient_palette)
  415. {
  416. unsigned int w = image->get_width();
  417. unsigned int h = image->get_height();
  418. unsigned int c = image->get_channels();
  419. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  420. for (unsigned int x = 0; x < w; ++x)
  421. {
  422. float3 color;
  423. unsigned int y = 0;
  424. unsigned int i = y * w * c + x * c;
  425. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  426. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  427. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  428. color = {r, g, b};
  429. ambient_colors.push_back(color);
  430. }
  431. }
  432. }
  433. void weather_system::set_shadow_palette(const ::image* image)
  434. {
  435. shadow_palette = image;
  436. if (shadow_palette)
  437. {
  438. unsigned int w = image->get_width();
  439. unsigned int h = image->get_height();
  440. unsigned int c = image->get_channels();
  441. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  442. for (unsigned int x = 0; x < w; ++x)
  443. {
  444. unsigned int y = 0;
  445. unsigned int i = y * w * c + x * c;
  446. float r = 1.0f - (static_cast<float>(pixels[i]) / 255.0f);
  447. shadow_strengths.push_back(r);
  448. }
  449. }
  450. }