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

315 lines
8.9 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 float seconds_per_day = 24.0f * 60.0f * 60.0f;
  30. weather_system::weather_system(entt::registry& registry):
  31. entity_system(registry),
  32. ambient_light(nullptr),
  33. sun_light(nullptr),
  34. moon_light(nullptr),
  35. shadow_light(nullptr),
  36. sky_pass(nullptr),
  37. shadow_map_pass(nullptr),
  38. material_pass(nullptr),
  39. time_of_day(0.0f),
  40. time_scale(1.0f),
  41. sky_palette(nullptr),
  42. shadow_palette(nullptr),
  43. sun_direction{0.0f, -1.0f, 0.0f}
  44. {}
  45. void weather_system::update(double t, double dt)
  46. {
  47. set_time_of_day(time_of_day + dt * time_scale);
  48. }
  49. void weather_system::set_ambient_light(::ambient_light* light)
  50. {
  51. this->ambient_light = light;
  52. }
  53. void weather_system::set_sun_light(directional_light* light)
  54. {
  55. sun_light = light;
  56. if (sky_pass)
  57. {
  58. sky_pass->set_sun_light(sun_light);
  59. }
  60. }
  61. void weather_system::set_moon_light(directional_light* light)
  62. {
  63. moon_light = light;
  64. if (sky_pass)
  65. {
  66. sky_pass->set_moon_light(moon_light);
  67. }
  68. }
  69. void weather_system::set_sky_pass(::sky_pass* pass)
  70. {
  71. sky_pass = pass;
  72. if (sky_pass)
  73. {
  74. sky_pass->set_sun_light(sun_light);
  75. sky_pass->set_moon_light(moon_light);
  76. }
  77. }
  78. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  79. {
  80. shadow_map_pass = pass;
  81. if (shadow_map_pass)
  82. {
  83. shadow_map_pass->set_light(shadow_light);
  84. }
  85. }
  86. void weather_system::set_material_pass(::material_pass* pass)
  87. {
  88. material_pass = pass;
  89. if (material_pass)
  90. {
  91. material_pass->set_shadow_strength(0.75f);
  92. }
  93. }
  94. void weather_system::set_time_of_day(float t)
  95. {
  96. time_of_day = std::fmod(seconds_per_day + fmod(t, seconds_per_day), seconds_per_day);
  97. //sun_azimuth = 0.0f;
  98. //sun_elevation = (time_of_day / seconds_per_day) * math::two_pi<float> - math::half_pi<float>;
  99. float hour_angle = math::wrap_radians(time_of_day * (math::two_pi<float> / seconds_per_day) - math::pi<float>);
  100. float declination = math::radians(0.0f);
  101. float latitude = math::radians(0.0f);
  102. sun_elevation = std::asin(std::sin(declination) * std::sin(latitude) + std::cos(declination) * std::cos(hour_angle) * std::cos(latitude));
  103. sun_azimuth = std::acos((std::sin(declination) * std::cos(latitude) - std::cos(declination) * std::cos(hour_angle) * std::sin(latitude)) / std::cos(sun_elevation));
  104. if (hour_angle < 0.0f)
  105. sun_azimuth = math::two_pi<float> - sun_azimuth;
  106. //std::cout << "hour angle: " << math::degrees(hour_angle) << std::endl;
  107. //std::cout << "azimuth: " << math::degrees(sun_azimuth) << std::endl;
  108. //std::cout << "time: " << (time_of_day / 60.0f / 60.0f) << std::endl;
  109. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_azimuth, float3{0, 1, 0});
  110. math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_elevation, float3{-1, 0, 0});
  111. math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  112. sun_direction = math::normalize(sun_rotation * float3{0, 0, -1});
  113. if (sun_light)
  114. {
  115. sun_light->set_rotation(sun_rotation);
  116. }
  117. if (moon_light)
  118. {
  119. math::quaternion<float> moon_azimuth_rotation = math::angle_axis(sun_azimuth, float3{0, 1, 0});
  120. math::quaternion<float> moon_elevation_rotation = math::angle_axis(sun_elevation, float3{1, 0, 0});
  121. math::quaternion<float> moon_rotation = math::normalize(moon_azimuth_rotation * moon_elevation_rotation);
  122. moon_light->set_rotation(moon_rotation);
  123. }
  124. if (sky_pass)
  125. {
  126. float hour = time_of_day / (60.0f * 60.0f);
  127. std::size_t hour_index = static_cast<std::size_t>(hour);
  128. const std::array<float4, 4>& gradient0 = sky_gradients[hour_index];
  129. const std::array<float4, 4>& gradient1 = sky_gradients[(hour_index + 1) % sky_gradients.size()];
  130. float t = hour - std::floor(hour);
  131. std::array<float4, 4> gradient;
  132. for (int i = 0; i < 4; ++i)
  133. {
  134. gradient[i] = math::lerp(gradient0[i], gradient1[i], t);
  135. }
  136. float3 sun_color0 = sun_colors[hour_index];
  137. float3 sun_color1 = sun_colors[(hour_index + 1) % sun_colors.size()];
  138. float3 sun_color = math::lerp(sun_color0, sun_color1, t);
  139. float3 moon_color0 = moon_colors[hour_index];
  140. float3 moon_color1 = moon_colors[(hour_index + 1) % moon_colors.size()];
  141. float3 moon_color = math::lerp(moon_color0, moon_color1, t);
  142. float3 ambient_color0 = ambient_colors[hour_index];
  143. float3 ambient_color1 = ambient_colors[(hour_index + 1) % sun_colors.size()];
  144. float3 ambient_color = math::lerp(ambient_color0, ambient_color1, t);
  145. sun_light->set_color(sun_color);
  146. moon_light->set_color(moon_color);
  147. moon_light->set_intensity(1.0f);
  148. ambient_light->set_color(ambient_color);
  149. sky_pass->set_sky_gradient(gradient);
  150. sky_pass->set_time_of_day(time_of_day);
  151. }
  152. shadow_light = sun_light;
  153. if (shadow_map_pass)
  154. {
  155. shadow_map_pass->set_light(shadow_light);
  156. }
  157. }
  158. void weather_system::set_time_scale(float scale)
  159. {
  160. time_scale = scale;
  161. }
  162. void weather_system::set_sky_palette(const ::image* image)
  163. {
  164. sky_palette = image;
  165. if (sky_palette)
  166. {
  167. unsigned int w = image->get_width();
  168. unsigned int h = image->get_height();
  169. unsigned int c = image->get_channels();
  170. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  171. for (unsigned int x = 0; x < w; ++x)
  172. {
  173. std::array<float4, 4> gradient;
  174. for (unsigned int y = 0; y < std::min<unsigned int>(4, h); ++y)
  175. {
  176. unsigned int i = y * w * c + x * c;
  177. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  178. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  179. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  180. gradient[y] = {r, g, b, static_cast<float>(y) * (1.0f / 3.0f)};
  181. }
  182. sky_gradients.push_back(gradient);
  183. }
  184. }
  185. }
  186. void weather_system::set_sun_palette(const ::image* image)
  187. {
  188. sun_palette = image;
  189. if (sun_palette)
  190. {
  191. unsigned int w = image->get_width();
  192. unsigned int h = image->get_height();
  193. unsigned int c = image->get_channels();
  194. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  195. for (unsigned int x = 0; x < w; ++x)
  196. {
  197. float3 color;
  198. unsigned int y = 0;
  199. unsigned int i = y * w * c + x * c;
  200. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  201. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  202. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  203. color = {r, g, b};
  204. sun_colors.push_back(color);
  205. }
  206. }
  207. }
  208. void weather_system::set_moon_palette(const ::image* image)
  209. {
  210. moon_palette = image;
  211. if (moon_palette)
  212. {
  213. unsigned int w = image->get_width();
  214. unsigned int h = image->get_height();
  215. unsigned int c = image->get_channels();
  216. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  217. for (unsigned int x = 0; x < w; ++x)
  218. {
  219. float3 color;
  220. unsigned int y = 0;
  221. unsigned int i = y * w * c + x * c;
  222. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  223. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  224. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  225. color = {r, g, b};
  226. moon_colors.push_back(color);
  227. }
  228. }
  229. }
  230. void weather_system::set_ambient_palette(const ::image* image)
  231. {
  232. ambient_palette = image;
  233. if (ambient_palette)
  234. {
  235. unsigned int w = image->get_width();
  236. unsigned int h = image->get_height();
  237. unsigned int c = image->get_channels();
  238. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  239. for (unsigned int x = 0; x < w; ++x)
  240. {
  241. float3 color;
  242. unsigned int y = 0;
  243. unsigned int i = y * w * c + x * c;
  244. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  245. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  246. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  247. color = {r, g, b};
  248. ambient_colors.push_back(color);
  249. }
  250. }
  251. }
  252. void weather_system::set_shadow_palette(const ::image* image)
  253. {
  254. shadow_palette = image;
  255. }