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

86 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 "entity/systems/atmosphere.hpp"
  20. #include "physics/atmosphere.hpp"
  21. namespace entity {
  22. namespace system {
  23. atmosphere::atmosphere(entity::registry& registry):
  24. updatable(registry),
  25. rgb_wavelengths_nm{0, 0, 0},
  26. rgb_wavelengths_m{0, 0, 0}
  27. {
  28. registry.on_construct<entity::component::atmosphere>().connect<&atmosphere::on_atmosphere_construct>(this);
  29. registry.on_replace<entity::component::atmosphere>().connect<&atmosphere::on_atmosphere_replace>(this);
  30. }
  31. void atmosphere::update(double t, double dt)
  32. {}
  33. void atmosphere::set_rgb_wavelengths(const double3& wavelengths)
  34. {
  35. rgb_wavelengths_nm = wavelengths;
  36. rgb_wavelengths_m = wavelengths * 1e-9;
  37. }
  38. void atmosphere::update_coefficients(entity::id entity_id)
  39. {
  40. // Abort if entity has no atmosphere component
  41. if (!registry.has<component::atmosphere>(entity_id))
  42. return;
  43. // Get atmosphere component of the entity
  44. component::atmosphere& atmosphere = registry.get<component::atmosphere>(entity_id);
  45. // Calculate polarization factors
  46. const double rayleigh_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.rayleigh_density);
  47. const double mie_polarization = physics::atmosphere::polarization(atmosphere.index_of_refraction, atmosphere.mie_density);
  48. // Calculate Rayleigh scattering coefficients
  49. atmosphere.rayleigh_scattering =
  50. {
  51. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.x, atmosphere.rayleigh_density, rayleigh_polarization),
  52. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.y, atmosphere.rayleigh_density, rayleigh_polarization),
  53. physics::atmosphere::scattering_rayleigh(rgb_wavelengths_m.z, atmosphere.rayleigh_density, rayleigh_polarization)
  54. };
  55. // Calculate Mie scattering coefficients
  56. const double mie_scattering = physics::atmosphere::scattering_mie(atmosphere.mie_density, mie_polarization);
  57. atmosphere.mie_scattering =
  58. {
  59. mie_scattering,
  60. mie_scattering,
  61. mie_scattering
  62. };
  63. }
  64. void atmosphere::on_atmosphere_construct(entity::registry& registry, entity::id entity_id, entity::component::atmosphere& atmosphere)
  65. {
  66. update_coefficients(entity_id);
  67. }
  68. void atmosphere::on_atmosphere_replace(entity::registry& registry, entity::id entity_id, entity::component::atmosphere& atmosphere)
  69. {
  70. update_coefficients(entity_id);
  71. }
  72. } // namespace system
  73. } // namespace entity