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

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