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

124 lines
3.1 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 <cmath>
  25. weather_system::weather_system(entt::registry& registry):
  26. entity_system(registry),
  27. ambient_light(nullptr),
  28. sun_light(nullptr),
  29. moon_light(nullptr),
  30. shadow_light(nullptr),
  31. sky_pass(nullptr),
  32. shadow_map_pass(nullptr),
  33. material_pass(nullptr),
  34. time_of_day(0.0f),
  35. time_scale(1.0f),
  36. sun_direction{0.0f, -1.0f, 0.0f}
  37. {}
  38. void weather_system::update(double t, double dt)
  39. {
  40. set_time_of_day(time_of_day + dt * time_scale);
  41. }
  42. void weather_system::set_ambient_light(::ambient_light* light)
  43. {
  44. this->ambient_light = light;
  45. }
  46. void weather_system::set_sun_light(directional_light* light)
  47. {
  48. sun_light = light;
  49. if (sky_pass)
  50. {
  51. sky_pass->set_sun_light(sun_light);
  52. }
  53. }
  54. void weather_system::set_moon_light(directional_light* light)
  55. {
  56. moon_light = light;
  57. }
  58. void weather_system::set_sky_pass(::sky_pass* pass)
  59. {
  60. sky_pass = pass;
  61. if (sky_pass)
  62. {
  63. sky_pass->set_sun_light(sun_light);
  64. }
  65. }
  66. void weather_system::set_shadow_map_pass(::shadow_map_pass* pass)
  67. {
  68. shadow_map_pass = pass;
  69. if (shadow_map_pass)
  70. {
  71. shadow_map_pass->set_light(shadow_light);
  72. }
  73. }
  74. void weather_system::set_material_pass(::material_pass* pass)
  75. {
  76. material_pass = pass;
  77. if (material_pass)
  78. {
  79. material_pass->set_shadow_strength(0.75f);
  80. }
  81. }
  82. void weather_system::set_time_of_day(float t)
  83. {
  84. static constexpr float seconds_per_day = 24.0f * 60.0f * 60.0f;
  85. time_of_day = std::fmod(t, seconds_per_day);
  86. sun_azimuth = 0.0f;
  87. sun_elevation = (time_of_day / seconds_per_day) * math::two_pi<float> - math::half_pi<float>;
  88. math::quaternion<float> sun_azimuth_rotation = math::angle_axis(sun_azimuth, float3{0, 1, 0});
  89. math::quaternion<float> sun_elevation_rotation = math::angle_axis(sun_elevation, float3{-1, 0, 0});
  90. math::quaternion<float> sun_rotation = math::normalize(sun_azimuth_rotation * sun_elevation_rotation);
  91. sun_direction = math::normalize(sun_rotation * float3{0, 0, -1});
  92. if (sun_light)
  93. {
  94. sun_light->set_rotation(sun_rotation);
  95. }
  96. shadow_light = sun_light;
  97. if (shadow_map_pass)
  98. {
  99. shadow_map_pass->set_light(shadow_light);
  100. }
  101. }
  102. void weather_system::set_time_scale(float scale)
  103. {
  104. time_scale = scale;
  105. }