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

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