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

80 lines
3.0 KiB

  1. /*
  2. * Copyright (C) 2021 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 "ecs/systems/atmosphere-system.hpp"
  20. #include "physics/atmosphere.hpp"
  21. namespace ecs {
  22. atmosphere_system::atmosphere_system(ecs::registry& registry):
  23. entity_system(registry)
  24. {
  25. // RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
  26. rgb_wavelengths_nm = {602.224, 541.069, 448.143};
  27. rgb_wavelengths_m = rgb_wavelengths_nm * 1e-9;
  28. registry.on_construct<ecs::atmosphere_component>().connect<&atmosphere_system::on_atmosphere_construct>(this);
  29. registry.on_replace<ecs::atmosphere_component>().connect<&atmosphere_system::on_atmosphere_replace>(this);
  30. }
  31. void atmosphere_system::update(double t, double dt)
  32. {}
  33. void atmosphere_system::update_coefficients(ecs::entity entity)
  34. {
  35. // Abort if entity has no atmosphere component
  36. if (!registry.has<atmosphere_component>(entity))
  37. return;
  38. // Get atmosphere component of the entity
  39. atmosphere_component& atmosphere = registry.get<atmosphere_component>(entity);
  40. // Calculate polarization factors
  41. const double rayleigh_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.rayleigh_density);
  42. const double mie_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.mie_density);
  43. // Calculate Rayleigh scattering coefficients
  44. atmosphere.rayleigh_scattering =
  45. {
  46. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.x, atmosphere.rayleigh_density, rayleigh_polarization),
  47. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.y, atmosphere.rayleigh_density, rayleigh_polarization),
  48. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.z, atmosphere.rayleigh_density, rayleigh_polarization)
  49. };
  50. // Calculate Mie scattering coefficients
  51. const double mie_scattering = physics::atmosphere::scattering_mie(atmosphere.mie_density, mie_polarization);
  52. atmosphere.mie_scattering =
  53. {
  54. mie_scattering,
  55. mie_scattering,
  56. mie_scattering
  57. };
  58. }
  59. void atmosphere_system::on_atmosphere_construct(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
  60. {
  61. update_coefficients(entity);
  62. }
  63. void atmosphere_system::on_atmosphere_replace(ecs::registry& registry, ecs::entity entity, ecs::atmosphere_component& atmosphere)
  64. {
  65. update_coefficients(entity);
  66. }
  67. } // namespace ecs