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

179 lines
4.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 "renderer/passes/sky-pass.hpp"
  22. #include "renderer/passes/shadow-map-pass.hpp"
  23. #include "renderer/passes/material-pass.hpp"
  24. #include "utility/gamma.hpp"
  25. #include "resources/image.hpp"
  26. #include <cmath>
  27. #include <iostream>
  28. static constexpr float seconds_per_day = 24.0f * 60.0f * 60.0f;
  29. weather_system::weather_system(entt::registry& registry):
  30. entity_system(registry),
  31. ambient_light(nullptr),
  32. sun_light(nullptr),
  33. moon_light(nullptr),
  34. shadow_light(nullptr),
  35. sky_pass(nullptr),
  36. shadow_map_pass(nullptr),
  37. material_pass(nullptr),
  38. time_of_day(0.0f),
  39. time_scale(1.0f),
  40. sky_palette(nullptr),
  41. shadow_palette(nullptr),
  42. sun_direction{0.0f, -1.0f, 0.0f}
  43. {}
  44. void weather_system::update(double t, double dt)
  45. {
  46. set_time_of_day(time_of_day + dt * time_scale);
  47. }
  48. void weather_system::set_ambient_light(::ambient_light* light)
  49. {
  50. this->ambient_light = light;
  51. }
  52. void weather_system::set_sun_light(directional_light* light)
  53. {
  54. sun_light = light;
  55. if (sky_pass)
  56. {
  57. sky_pass->set_sun_light(sun_light);
  58. }
  59. }
  60. void weather_system::set_moon_light(directional_light* light)
  61. {
  62. moon_light = light;
  63. }
  64. void weather_system::set_sky_pass(::sky_pass* pass)
  65. {
  66. sky_pass = pass;
  67. if (sky_pass)
  68. {
  69. sky_pass->set_sun_light(sun_light);
  70. }
  71. }
  72. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  73. {
  74. shadow_map_pass = pass;
  75. if (shadow_map_pass)
  76. {
  77. shadow_map_pass->set_light(shadow_light);
  78. }
  79. }
  80. void weather_system::set_material_pass(::material_pass* pass)
  81. {
  82. material_pass = pass;
  83. if (material_pass)
  84. {
  85. material_pass->set_shadow_strength(0.75f);
  86. }
  87. }
  88. void weather_system::set_time_of_day(float t)
  89. {
  90. time_of_day = std::fmod(t, seconds_per_day);
  91. //sun_azimuth = 0.0f;
  92. //sun_elevation = (time_of_day / seconds_per_day) * math::two_pi<float> - math::half_pi<float>;
  93. float hour_angle = math::wrap_radians(time_of_day * (math::two_pi<float> / seconds_per_day) - math::pi<float>);
  94. float declination = math::radians(0.0f);
  95. float latitude = math::radians(0.0f);
  96. sun_elevation = std::asin(std::sin(declination) * std::sin(latitude) + std::cos(declination) * std::cos(hour_angle) * std::cos(latitude));
  97. sun_azimuth = std::acos((std::sin(declination) * std::cos(latitude) - std::cos(declination) * std::cos(hour_angle) * std::sin(latitude)) / std::cos(sun_elevation));
  98. if (hour_angle < 0.0f)
  99. sun_azimuth = math::two_pi<float> - sun_azimuth;
  100. //std::cout << "hour angle: " << math::degrees(hour_angle) << std::endl;
  101. //std::cout << "azimuth: " << math::degrees(sun_azimuth) << std::endl;
  102. //std::cout << "time: " << (time_of_day / 60.0f / 60.0f) << std::endl;
  103. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_azimuth, float3{0, 1, 0});
  104. math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_elevation, float3{-1, 0, 0});
  105. math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  106. sun_direction = math::normalize(sun_rotation * float3{0, 0, -1});
  107. if (sun_light)
  108. {
  109. sun_light->set_rotation(sun_rotation);
  110. }
  111. if (sky_pass)
  112. {
  113. sky_pass->set_sky_gradient(sky_gradient);
  114. }
  115. shadow_light = sun_light;
  116. if (shadow_map_pass)
  117. {
  118. shadow_map_pass->set_light(shadow_light);
  119. }
  120. }
  121. void weather_system::set_time_scale(float scale)
  122. {
  123. time_scale = scale;
  124. }
  125. void weather_system::set_sky_palette(const ::image* image)
  126. {
  127. sky_palette = image;
  128. if (sky_palette)
  129. {
  130. unsigned int w = image->get_width();
  131. unsigned int h = image->get_height();
  132. unsigned int c = image->get_channels();
  133. const unsigned char* pixels = static_cast<const unsigned char*>(image->get_pixels());
  134. for (unsigned int x = 0; x < w; ++x)
  135. {
  136. for (unsigned int y = 0; y < std::min<unsigned int>(4, h); ++y)
  137. {
  138. unsigned int i = y * w * c + x * c;
  139. float r = srgb_to_linear(static_cast<float>(pixels[i]) / 255.0f);
  140. float g = srgb_to_linear(static_cast<float>(pixels[i + 1]) / 255.0f);
  141. float b = srgb_to_linear(static_cast<float>(pixels[i + 2]) / 255.0f);
  142. sky_gradient[y] = {r, g, b, static_cast<float>(y) * (1.0f / 3.0f)};
  143. }
  144. }
  145. }
  146. }
  147. void weather_system::set_shadow_palette(const ::image* image)
  148. {
  149. shadow_palette = image;
  150. }