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

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