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

117 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 "entity/systems/blackbody.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 entity {
  25. namespace system {
  26. blackbody::blackbody(entity::registry& registry):
  27. updatable(registry),
  28. rgb_wavelengths_nm{0, 0, 0},
  29. rgb_wavelengths_m{0, 0, 0}
  30. {
  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<entity::component::blackbody>().connect<&blackbody::on_blackbody_construct>(this);
  35. registry.on_replace<entity::component::blackbody>().connect<&blackbody::on_blackbody_replace>(this);
  36. registry.on_construct<entity::component::celestial_body>().connect<&blackbody::on_celestial_body_construct>(this);
  37. registry.on_replace<entity::component::celestial_body>().connect<&blackbody::on_celestial_body_replace>(this);
  38. }
  39. void blackbody::update(double t, double dt)
  40. {}
  41. void blackbody::set_rgb_wavelengths(const double3& wavelengths)
  42. {
  43. rgb_wavelengths_nm = wavelengths;
  44. rgb_wavelengths_m = wavelengths * 1e-9;
  45. }
  46. void blackbody::update_luminous_intensity(entity::id entity_id)
  47. {
  48. // Abort if entity has no blackbody component
  49. if (!registry.has<component::blackbody>(entity_id))
  50. return;
  51. // Get blackbody component of the entity
  52. component::blackbody& blackbody = registry.get<component::blackbody>(entity_id);
  53. // Clear luminous intensity
  54. blackbody.luminous_intensity = {0, 0, 0};
  55. // Abort if entity has no celestial body component
  56. if (!registry.has<component::celestial_body>(entity_id))
  57. return;
  58. // Get celestial body component of the entity
  59. const component::celestial_body& celestial_body = registry.get<component::celestial_body>(entity_id);
  60. // Calculate (spherical) surface area of the celestial body
  61. const double surface_area = 4.0 * math::pi<double> * celestial_body.radius * celestial_body.radius;
  62. // Construct a lambda function which calculates the blackbody's RGB luminous intensity of a given wavelength
  63. auto rgb_luminous_intensity = [blackbody, surface_area](double wavelength_nm) -> double3
  64. {
  65. // Convert wavelength from nanometers to meters
  66. const double wavelength_m = wavelength_nm * 1e-9;
  67. // Calculate the spectral intensity of the wavelength
  68. const double spectral_intensity = physics::light::blackbody::spectral_intensity<double>(blackbody.temperature, surface_area, wavelength_m);
  69. // Calculate the ACEScg color of the wavelength using CIE color matching functions
  70. double3 spectral_color = color::xyz::to_acescg(color::xyz::match(wavelength_nm));
  71. // Scale the spectral color by spectral intensity
  72. return spectral_color * spectral_intensity * 1e-9 * physics::light::max_luminous_efficacy<double>;
  73. };
  74. // Integrate the blackbody RGB luminous intensity over wavelengths in the visible spectrum
  75. blackbody.luminous_intensity = math::quadrature::simpson(rgb_luminous_intensity, visible_wavelengths_nm.begin(), visible_wavelengths_nm.end());
  76. }
  77. void blackbody::on_blackbody_construct(entity::registry& registry, entity::id entity_id, entity::component::blackbody& blackbody)
  78. {
  79. update_luminous_intensity(entity_id);
  80. }
  81. void blackbody::on_blackbody_replace(entity::registry& registry, entity::id entity_id, entity::component::blackbody& blackbody)
  82. {
  83. update_luminous_intensity(entity_id);
  84. }
  85. void blackbody::on_celestial_body_construct(entity::registry& registry, entity::id entity_id, entity::component::celestial_body& celestial_body)
  86. {
  87. update_luminous_intensity(entity_id);
  88. }
  89. void blackbody::on_celestial_body_replace(entity::registry& registry, entity::id entity_id, entity::component::celestial_body& celestial_body)
  90. {
  91. update_luminous_intensity(entity_id);
  92. }
  93. } // namespace system
  94. } // namespace entity