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

111 lines
4.5 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/blackbody-system.hpp"
  20. #include "color/color.hpp"
  21. #include "physics/light/blackbody.hpp"
  22. #include "physics/light/photometry.hpp"
  23. #include "math/quadrature.hpp"
  24. namespace ecs {
  25. blackbody_system::blackbody_system(ecs::registry& registry):
  26. entity_system(registry)
  27. {
  28. // RGB wavelengths determined by matching wavelengths to XYZ, transforming XYZ to ACEScg, then selecting the max wavelengths for R, G, and B.
  29. rgb_wavelengths_nm = {602.224, 541.069, 448.143};
  30. rgb_wavelengths_m = rgb_wavelengths_nm * 1e-9;
  31. // Construct a range of sample wavelengths in the visible spectrum
  32. visible_wavelengths_nm.resize(780 - 280);
  33. std::iota(visible_wavelengths_nm.begin(), visible_wavelengths_nm.end(), 280);
  34. registry.on_construct<ecs::blackbody_component>().connect<&blackbody_system::on_blackbody_construct>(this);
  35. registry.on_replace<ecs::blackbody_component>().connect<&blackbody_system::on_blackbody_replace>(this);
  36. registry.on_construct<ecs::celestial_body_component>().connect<&blackbody_system::on_celestial_body_construct>(this);
  37. registry.on_replace<ecs::celestial_body_component>().connect<&blackbody_system::on_celestial_body_replace>(this);
  38. }
  39. void blackbody_system::update(double t, double dt)
  40. {}
  41. void blackbody_system::update_luminous_intensity(ecs::entity entity)
  42. {
  43. // Abort if entity has no blackbody component
  44. if (!registry.has<blackbody_component>(entity))
  45. return;
  46. // Get blackbody component of the entity
  47. blackbody_component& blackbody = registry.get<blackbody_component>(entity);
  48. // Clear luminous intensity
  49. blackbody.luminous_intensity = {0, 0, 0};
  50. // Abort if entity has no celestial body component
  51. if (!registry.has<celestial_body_component>(entity))
  52. return;
  53. // Get celestial body component of the entity
  54. const celestial_body_component& celestial_body = registry.get<celestial_body_component>(entity);
  55. // Calculate (spherical) surface area of the celestial body
  56. const double surface_area = 4.0 * math::pi<double> * celestial_body.radius * celestial_body.radius;
  57. // Construct a lambda function which calculates the blackbody's RGB luminous intensity of a given wavelength
  58. auto rgb_luminous_intensity = [blackbody, surface_area](double wavelength_nm) -> double3
  59. {
  60. // Convert wavelength from nanometers to meters
  61. const double wavelength_m = wavelength_nm * 1e-9;
  62. // Calculate the spectral intensity of the wavelength
  63. const double spectral_intensity = physics::light::blackbody::spectral_intensity<double>(blackbody.temperature, surface_area, wavelength_m);
  64. // Calculate the ACEScg color of the wavelength using CIE color matching functions
  65. double3 spectral_color = color::xyz::to_acescg(color::xyz::match(wavelength_nm));
  66. // Scale the spectral color by spectral intensity
  67. return spectral_color * spectral_intensity * 1e-9 * physics::light::max_luminous_efficacy<double>;
  68. };
  69. // Integrate the blackbody RGB luminous intensity over wavelengths in the visible spectrum
  70. blackbody.luminous_intensity = math::quadrature::simpson(rgb_luminous_intensity, visible_wavelengths_nm.begin(), visible_wavelengths_nm.end());
  71. }
  72. void blackbody_system::on_blackbody_construct(ecs::registry& registry, ecs::entity entity, ecs::blackbody_component& blackbody)
  73. {
  74. update_luminous_intensity(entity);
  75. }
  76. void blackbody_system::on_blackbody_replace(ecs::registry& registry, ecs::entity entity, ecs::blackbody_component& blackbody)
  77. {
  78. update_luminous_intensity(entity);
  79. }
  80. void blackbody_system::on_celestial_body_construct(ecs::registry& registry, ecs::entity entity, ecs::celestial_body_component& celestial_body)
  81. {
  82. update_luminous_intensity(entity);
  83. }
  84. void blackbody_system::on_celestial_body_replace(ecs::registry& registry, ecs::entity entity, ecs::celestial_body_component& celestial_body)
  85. {
  86. update_luminous_intensity(entity);
  87. }
  88. } // namespace ecs