Antkeeper  0.0.1
atmosphere-system.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 
24 
27 {
28  registry.on_construct<::atmosphere_component>().connect<&atmosphere_system::on_atmosphere_construct>(this);
29  registry.on_update<::atmosphere_component>().connect<&atmosphere_system::on_atmosphere_update>(this);
30  registry.on_destroy<::atmosphere_component>().connect<&atmosphere_system::on_atmosphere_destroy>(this);
31 
32  set_rgb_wavelengths({680, 550, 440});
33 }
34 
36 {
37  registry.on_construct<::atmosphere_component>().disconnect<&atmosphere_system::on_atmosphere_construct>(this);
38  registry.on_update<::atmosphere_component>().disconnect<&atmosphere_system::on_atmosphere_update>(this);
39  registry.on_destroy<::atmosphere_component>().disconnect<&atmosphere_system::on_atmosphere_destroy>(this);
40 }
41 
42 void atmosphere_system::update(float t, float dt)
43 {}
44 
46 {
47  m_rgb_wavelengths_nm = wavelengths;
48  m_rgb_wavelengths_m = m_rgb_wavelengths_nm * 1e-9;
49 
50  // Update ozone cross sections
51  m_rgb_ozone_cross_sections =
52  {
53  physics::gas::ozone::cross_section_293k<double>(m_rgb_wavelengths_nm.x()),
54  physics::gas::ozone::cross_section_293k<double>(m_rgb_wavelengths_nm.y()),
55  physics::gas::ozone::cross_section_293k<double>(m_rgb_wavelengths_nm.z())
56  };
57 
58  // Update atmosphere components
59  registry.view<::atmosphere_component>().each
60  (
61  [&](entity::id entity_id, auto& component)
62  {
63  update_atmosphere(entity_id);
64  }
65  );
66 }
67 
69 {
70  m_sky_pass = pass;
71  update_sky_pass();
72 }
73 
75 {
76  if (entity_id != m_active_atmosphere_eid)
77  {
78  m_active_atmosphere_eid = entity_id;
79  update_sky_pass();
80  }
81 }
82 
83 void atmosphere_system::update_atmosphere(entity::id entity_id)
84 {
85  // Get atmosphere component of the entity
86  ::atmosphere_component* component = registry.try_get<::atmosphere_component>(entity_id);
87 
88  // Abort if entity has no atmosphere component
89  if (!component)
90  {
91  return;
92  }
93 
94  // Calculate Rayleigh scattering coefficients
95  const double rayleigh_density = physics::number_density(component->rayleigh_concentration);
96  const double rayleigh_polarization = physics::gas::atmosphere::polarization(component->index_of_refraction, rayleigh_density);
97  component->rayleigh_scattering =
98  {
99  physics::gas::atmosphere::scattering(rayleigh_density, rayleigh_polarization, m_rgb_wavelengths_m.x()),
100  physics::gas::atmosphere::scattering(rayleigh_density, rayleigh_polarization, m_rgb_wavelengths_m.y()),
101  physics::gas::atmosphere::scattering(rayleigh_density, rayleigh_polarization, m_rgb_wavelengths_m.z())
102  };
103 
104  // Calculate Mie scattering and extinction coefficients
105  const double mie_density = physics::number_density(component->mie_concentration);
106  const double mie_polarization = physics::gas::atmosphere::polarization(component->index_of_refraction, mie_density);
107  component->mie_scattering = physics::gas::atmosphere::scattering(mie_density, mie_polarization);
109 
110  // Calculate ozone absorption coefficients
111  const double ozone_density = physics::number_density(component->ozone_concentration);
112  component->ozone_absorption =
113  {
114  physics::gas::ozone::absorption(m_rgb_ozone_cross_sections.x(), ozone_density),
115  physics::gas::ozone::absorption(m_rgb_ozone_cross_sections.y(), ozone_density),
116  physics::gas::ozone::absorption(m_rgb_ozone_cross_sections.z(), ozone_density)
117  };
118 
119  // Update sky pass parameters
120  if (entity_id == m_active_atmosphere_eid)
121  {
122  update_sky_pass();
123  }
124 }
125 
126 void atmosphere_system::update_sky_pass()
127 {
128  // Abort if no sky pass set
129  if (!m_sky_pass)
130  {
131  return;
132  }
133 
134  // Abort if active atmosphere entity is not valid
135  if (!registry.valid(m_active_atmosphere_eid))
136  {
137  return;
138  }
139 
140  // Get atmosphere component of the entity
141  ::atmosphere_component* component = registry.try_get<::atmosphere_component>(m_active_atmosphere_eid);
142 
143  // Abort if entity has no atmosphere component
144  if (!component)
145  {
146  return;
147  }
148 
149  m_sky_pass->set_atmosphere_upper_limit(static_cast<float>(component->upper_limit));
150  m_sky_pass->set_rayleigh_parameters(static_cast<float>(component->rayleigh_scale_height), math::fvec3(component->rayleigh_scattering));
151  m_sky_pass->set_mie_parameters(static_cast<float>(component->mie_scale_height), static_cast<float>(component->mie_scattering), static_cast<float>(component->mie_extinction), static_cast<float>(component->mie_anisotropy));
152  m_sky_pass->set_ozone_parameters(static_cast<float>(component->ozone_lower_limit), static_cast<float>(component->ozone_upper_limit), static_cast<float>(component->ozone_mode), math::fvec3(component->ozone_absorption));
153  m_sky_pass->set_airglow_luminance(math::fvec3(component->airglow_luminance));
154 }
155 
156 void atmosphere_system::on_atmosphere_construct(entity::registry& registry, entity::id entity_id)
157 {
158  update_atmosphere(entity_id);
159 }
160 
161 void atmosphere_system::on_atmosphere_update(entity::registry& registry, entity::id entity_id)
162 {
163  update_atmosphere(entity_id);
164 }
165 
166 void atmosphere_system::on_atmosphere_destroy(entity::registry& registry, entity::id entity_id)
167 {
168  if (entity_id == m_active_atmosphere_eid)
169  {
170  m_active_atmosphere_eid = entt::null;
171  }
172 }
void set_rgb_wavelengths(const math::dvec3 &wavelengths)
Sets the wavelengths of red, green, and blue light.
void set_active_atmosphere(entity::id entity_id)
Sets the entity ID of the active atmosphere.
atmosphere_system(entity::registry &registry)
virtual void update(float t, float dt)
Perform's a system's update() function.
void set_sky_pass(::render::sky_pass *pass)
void set_mie_parameters(float scale_height, float scattering, float extinction, float anisotropy)
Definition: sky-pass.cpp:783
void set_rayleigh_parameters(float scale_height, const math::fvec3 &scattering)
Definition: sky-pass.cpp:768
void set_ozone_parameters(float lower_limit, float upper_limit, float mode, const math::fvec3 &absorption)
Definition: sky-pass.cpp:798
void set_airglow_luminance(const math::fvec3 &luminance)
Definition: sky-pass.cpp:813
void set_atmosphere_upper_limit(float limit)
Definition: sky-pass.cpp:751
Abstract base class for updatable systems.
entity::registry & registry
Registry on which the system operate.
entt::registry registry
Component registry type.
Definition: registry.hpp:28
entt::entity id
Entity ID type.
Definition: id.hpp:28
constexpr T e
e.
Definition: numbers.hpp:37
T extinction(T scattering, T albedo)
Calculates an extinction coefficient.
Definition: atmosphere.hpp:121
T scattering(T density, T polarization, T wavelength)
Calculates a wavelength-dependent scattering coefficient.
Definition: atmosphere.hpp:69
T polarization(T ior, T density)
Calculates a particle polarizability factor.
Definition: atmosphere.hpp:45
T absorption(T cross_section, T density)
Calculates an ozone absorption coefficient (wavelength-dependent).
Definition: ozone.hpp:573
T number_density(T c)
Calculates the number density of a substance.
double mie_albedo
Mie single-scattering albedo.
double ozone_mode
Elevation of the mode of the triangular distribution of ozone particles, in meters.
double upper_limit
Elevation of the upper limit of the atmosphere, in meters.
math::dvec3 airglow_luminance
Airglow luminance, in cd/m^2.
double mie_concentration
Molar concentration of Mie particles at sea level, in mol/m-3.
double mie_extinction
(Dependent) Mie extinction coefficient.
double rayleigh_concentration
Molar concentration of Rayleigh particles at sea level, in mol/m-3.
double ozone_upper_limit
Elevation of the upper limit of the triangular distribution of ozone particles, in meters.
double ozone_lower_limit
Elevation of the lower limit of the triangular distribution of ozone particles, in meters.
double mie_anisotropy
Mie phase function anisotropy factor.
double ozone_concentration
Concentration of ozone in the atmosphere, unitless.
double mie_scattering
(Dependent) Mie scattering coefficient.
math::dvec3 ozone_absorption
(Dependent) Ozone absorption coefficients.
math::dvec3 rayleigh_scattering
(Dependent) Rayleigh scattering coefficients.
double index_of_refraction
Index of refraction of air at sea level.
double rayleigh_scale_height
Scale height of the exponential distribution of Rayleigh particles, in meters.
double mie_scale_height
Scale height of the exponential distribution of Mie particles, in meters.
n-dimensional vector.
Definition: vector.hpp:44
constexpr element_type & x() noexcept
Returns a reference to the first element.
Definition: vector.hpp:164
constexpr element_type & y() noexcept
Returns a reference to the second element.
Definition: vector.hpp:180
constexpr element_type & z() noexcept
Returns a reference to the third element.
Definition: vector.hpp:196