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

310 lines
9.8 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 "game/astronomy/celestial-coordinates.hpp"
  28. #include "game/astronomy/celestial-mechanics.hpp"
  29. #include "game/astronomy/celestial-time.hpp"
  30. #include <cmath>
  31. static constexpr double hours_per_day = 24.0;
  32. static constexpr double minutes_per_day = hours_per_day * 60.0;
  33. static constexpr double seconds_per_day = minutes_per_day * 60.0;
  34. weather_system::weather_system(entt::registry& registry):
  35. entity_system(registry),
  36. ambient_light(nullptr),
  37. sun_light(nullptr),
  38. moon_light(nullptr),
  39. shadow_light(nullptr),
  40. sky_pass(nullptr),
  41. shadow_map_pass(nullptr),
  42. material_pass(nullptr),
  43. time_scale(1.0f),
  44. sun_direction{0.0f, -1.0f, 0.0f},
  45. location{0.0f, 0.0f, 0.0f},
  46. jd(0.0)
  47. {}
  48. void weather_system::update(double t, double dt)
  49. {
  50. jd += (dt * time_scale) / seconds_per_day;
  51. const float latitude = location[0];
  52. const float longitude = location[1];
  53. // Calculate local time
  54. double time_correction = longitude / (math::two_pi<double> / 24.0);
  55. double local_jd = jd + time_correction / 24.0 - 0.5;
  56. double local_time = (local_jd - std::floor(local_jd)) * 24.0;
  57. double lmst = ast::jd_to_lmst(jd, longitude);
  58. double ecl = ast::approx_ecliptic_obliquity(jd);
  59. double3x3 ecliptic_to_horizontal = ast::ecliptic_to_horizontal(ecl, latitude, lmst);
  60. double3 sun_ecliptic = ast::approx_sun_ecliptic(jd);
  61. double3 sun_horizontal = ecliptic_to_horizontal * sun_ecliptic;
  62. sun_horizontal.z -= 4.25875e-5; // Subtract one earth radius (in AU), for positon of observer
  63. double3 sun_spherical = ast::rectangular_to_spherical(sun_horizontal);
  64. double3 sun_positiond = ast::horizontal_to_right_handed * sun_horizontal;
  65. float2 sun_az_el = {static_cast<float>(sun_spherical.z) - math::pi<float>, static_cast<float>(sun_spherical.y)};
  66. float3 sun_position = math::normalize(float3{static_cast<float>(sun_positiond.x), static_cast<float>(sun_positiond.y), static_cast<float>(sun_positiond.z)});
  67. double3 moon_ecliptic = ast::approx_moon_ecliptic(jd);
  68. double3 moon_horizontal = ecliptic_to_horizontal * moon_ecliptic;
  69. moon_horizontal.z -= 1.0; // Subtract one earth radius, for position of observer
  70. double3 moon_spherical = ast::rectangular_to_spherical(moon_horizontal);
  71. double3 moon_positiond = ast::horizontal_to_right_handed * moon_horizontal;
  72. float2 moon_az_el = {static_cast<float>(moon_spherical.z) - math::pi<float>, static_cast<float>(moon_spherical.y)};
  73. float3 moon_position = math::normalize(math::type_cast<float>(moon_positiond));
  74. double3x3 moon_rotation_matrix = ast::horizontal_to_right_handed * ecliptic_to_horizontal;
  75. math::quaternion<double> moon_rotationd = math::normalize(math::quaternion_cast(moon_rotation_matrix) * math::angle_axis(math::half_pi<double>, double3{0, 1, 0}) * math::angle_axis(-math::half_pi<double>, double3{0, 0, -1}));
  76. math::quaternion<float> moon_rotation =
  77. {
  78. static_cast<float>(moon_rotationd.w),
  79. static_cast<float>(moon_rotationd.x),
  80. static_cast<float>(moon_rotationd.y),
  81. static_cast<float>(moon_rotationd.z)
  82. };
  83. if (sun_light)
  84. {
  85. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_az_el[0], float3{0, 1, 0});
  86. math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_az_el[1], float3{-1, 0, 0});
  87. math::quaternion<float> sun_az_el_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  88. sun_light->set_rotation(sun_az_el_rotation);
  89. }
  90. if (moon_light)
  91. {
  92. math::quaternion<float> moon_azimuth_rotation = math::angle_axis(moon_az_el[0], float3{0, 1, 0});
  93. math::quaternion<float> moon_elevation_rotation = math::angle_axis(moon_az_el[1], float3{-1, 0, 0});
  94. math::quaternion<float> moon_az_el_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
  95. moon_light->set_rotation(moon_az_el_rotation);
  96. }
  97. float sun_gradient_position = static_cast<float>(std::max<double>(0.0, ((sun_az_el[1] + math::half_pi<double>) / math::pi<double>)));
  98. float moon_gradient_position = static_cast<float>(std::max<double>(0.0, ((moon_az_el[1] + math::half_pi<double>) / math::pi<double>)));
  99. float sky_gradient_position = sun_gradient_position;
  100. float ambient_gradient_position = sun_gradient_position;
  101. if (sky_pass)
  102. {
  103. if (sun_light)
  104. {
  105. float3 sun_color = interpolate_gradient(sun_colors, sun_gradient_position);
  106. sun_light->set_color(sun_color);
  107. sun_light->set_intensity(1.0f);
  108. }
  109. if (moon_light)
  110. {
  111. float3 moon_color = interpolate_gradient(moon_colors, moon_gradient_position);
  112. moon_light->set_color(moon_color);
  113. moon_light->set_intensity(1.0f);
  114. }
  115. if (ambient_light)
  116. {
  117. float3 ambient_color = interpolate_gradient(ambient_colors, ambient_gradient_position);
  118. ambient_light->set_color(ambient_color);
  119. ambient_light->set_intensity(0.5f);
  120. }
  121. float3 horizon_color = interpolate_gradient(horizon_colors, sun_gradient_position);
  122. float3 zenith_color = interpolate_gradient(zenith_colors, sun_gradient_position);
  123. sky_pass->set_horizon_color(horizon_color);
  124. sky_pass->set_zenith_color(zenith_color);
  125. sky_pass->set_time_of_day(static_cast<float>(local_time * 60.0 * 60.0));
  126. sky_pass->set_observer_location(location[0], location[1], location[2]);
  127. sky_pass->set_sun_coordinates(sun_position, sun_az_el);
  128. sky_pass->set_moon_coordinates(moon_position, moon_az_el);
  129. sky_pass->set_julian_day(static_cast<float>(jd));
  130. sky_pass->set_moon_rotation(moon_rotation);
  131. }
  132. shadow_light = sun_light;
  133. if (shadow_map_pass)
  134. {
  135. if (sun_az_el[1] < 0.0f)
  136. {
  137. shadow_map_pass->set_light(moon_light);
  138. }
  139. else
  140. {
  141. shadow_map_pass->set_light(sun_light);
  142. }
  143. }
  144. if (material_pass)
  145. {
  146. float shadow_strength = interpolate_gradient(shadow_strengths, sun_gradient_position).x;
  147. material_pass->set_shadow_strength(shadow_strength);
  148. }
  149. }
  150. void weather_system::set_location(float latitude, float longitude, float altitude)
  151. {
  152. location = {latitude, longitude, altitude};
  153. }
  154. void weather_system::set_ambient_light(::ambient_light* light)
  155. {
  156. ambient_light = light;
  157. }
  158. void weather_system::set_sun_light(directional_light* light)
  159. {
  160. sun_light = light;
  161. }
  162. void weather_system::set_moon_light(directional_light* light)
  163. {
  164. moon_light = light;
  165. }
  166. void weather_system::set_sky_pass(::sky_pass* pass)
  167. {
  168. sky_pass = pass;
  169. if (sky_pass)
  170. {
  171. sky_pass->set_moon_angular_radius(math::radians(1.0f));
  172. sky_pass->set_sun_angular_radius(math::radians(1.1f));
  173. }
  174. }
  175. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  176. {
  177. shadow_map_pass = pass;
  178. if (shadow_map_pass)
  179. {
  180. shadow_map_pass->set_light(shadow_light);
  181. }
  182. }
  183. void weather_system::set_material_pass(::material_pass* pass)
  184. {
  185. material_pass = pass;
  186. }
  187. void weather_system::set_time(int year, int month, int day, int hour, int minute, double second, double tc)
  188. {
  189. jd = ast::ut_to_jd(year, month, day, hour, minute, second) - tc / 24.0;
  190. }
  191. void weather_system::set_time(double time)
  192. {
  193. jd = time + 2451545.0;
  194. }
  195. void weather_system::set_time_scale(float scale)
  196. {
  197. time_scale = scale;
  198. }
  199. void weather_system::set_sky_palette(const ::image* image)
  200. {
  201. load_palette(&horizon_colors, image, 0);
  202. load_palette(&zenith_colors, image, 1);
  203. }
  204. void weather_system::set_sun_palette(const ::image* image)
  205. {
  206. load_palette(&sun_colors, image, 0);
  207. }
  208. void weather_system::set_moon_palette(const ::image* image)
  209. {
  210. load_palette(&moon_colors, image, 0);
  211. }
  212. void weather_system::set_ambient_palette(const ::image* image)
  213. {
  214. load_palette(&ambient_colors, image, 0);
  215. }
  216. void weather_system::set_shadow_palette(const ::image* image)
  217. {
  218. load_palette(&shadow_strengths, image, 0);
  219. }
  220. void weather_system::load_palette(std::vector<float3>* palette, const ::image* image, unsigned int row)
  221. {
  222. unsigned int w = image->get_width();
  223. unsigned int h = image->get_height();
  224. unsigned int c = image->get_channels();
  225. unsigned int y = std::min<unsigned int>(row, h - 1);
  226. palette->clear();
  227. if (image->is_hdr())
  228. {
  229. const float* pixels = static_cast<const float*>(image->get_pixels());
  230. for (unsigned int x = 0; x < w; ++x)
  231. {
  232. unsigned int i = y * w * c + x * c;
  233. float r = pixels[i];
  234. float g = pixels[i + 1];
  235. float b = pixels[i + 2];
  236. palette->push_back(float3{r, g, b});
  237. }
  238. }
  239. else
  240. {
  241. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  242. for (unsigned int x = 0; x < w; ++x)
  243. {
  244. unsigned int i = y * w * c + x * c;
  245. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  246. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  247. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  248. palette->push_back(float3{r, g, b});
  249. }
  250. }
  251. }
  252. float3 weather_system::interpolate_gradient(const std::vector<float3>& gradient, float position)
  253. {
  254. if (gradient.empty())
  255. return float3{0.0f, 0.0f, 0.0f};
  256. position *= static_cast<float>(gradient.size() - 1);
  257. int index0 = static_cast<int>(position) % gradient.size();
  258. int index1 = (index0 + 1) % gradient.size();
  259. return math::lerp<float3>(gradient[index0], gradient[index1], position - std::floor(position));
  260. }