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

169 lines
4.9 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. #ifndef ANTKEEPER_PHYSICS_ATMOSPHERE_HPP
  20. #define ANTKEEPER_PHYSICS_ATMOSPHERE_HPP
  21. #include "physics/constants.hpp"
  22. #include "math/constants.hpp"
  23. #include <cmath>
  24. namespace physics {
  25. /// Atmosphere-related functions.
  26. namespace atmosphere {
  27. /**
  28. * Calculates the density of exponentially-distributed atmospheric particles at a given altitude.
  29. *
  30. * @param d0 Density at sea level.
  31. * @param z Height above sea level.
  32. * @param sh Scale height of the particle type.
  33. * @return Particle density at altitude.
  34. *
  35. * @see https://en.wikipedia.org/wiki/Scale_height
  36. * @see https://en.wikipedia.org/wiki/Barometric_formula
  37. */
  38. template <class T>
  39. T density(T d0, T z, T sh)
  40. {
  41. return d0 * std::exp(-z / sh);
  42. }
  43. /**
  44. * Calculates a particle polarizability factor used in computing scattering coefficients.
  45. *
  46. * @param ior Atmospheric index of refraction.
  47. * @param density Molecular density.
  48. * @return Polarization factor.
  49. *
  50. * @see Elek, Oskar. (2009). Rendering Parametrizable Planetary Atmospheres with Multiple Scattering in Real-Time.
  51. * @see Real-Time Spectral Scattering in Large-Scale Natural Participating Media.
  52. */
  53. template <class T>
  54. T polarization(T ior, T density)
  55. {
  56. const T ior2m1 = ior * ior - T(1.0);
  57. const T num = T(2) * math::pi<T> * math::pi<T> * ior2m1 * ior2m1;
  58. const T den = T(3) * density * density;
  59. return num / den;
  60. }
  61. /**
  62. * Calculates a Rayleigh scattering coefficient at sea level (wavelength-dependent).
  63. *
  64. * @param wavelength Wavelength of light, in meters.
  65. * @param density Molecular density of Rayleigh particles at sea level.
  66. * @param polarization Rayleigh particle polarization factor.
  67. *
  68. * @see atmosphere::polarization
  69. *
  70. * @see Elek, Oskar. (2009). Rendering Parametrizable Planetary Atmospheres with Multiple Scattering in Real-Time.
  71. * @see Real-Time Spectral Scattering in Large-Scale Natural Participating Media.
  72. */
  73. template <class T>
  74. T scattering_rayleigh(T wavelength, T density, T polarization)
  75. {
  76. const T wavelength2 = wavelength * wavelength;
  77. return T(4) * math::pi<T> * density / (wavelength2 * wavelength2) * polarization;
  78. }
  79. /**
  80. * Calculates a Mie scattering coefficient at sea level (wavelength-independent).
  81. *
  82. * @param density Molecular density of Mie particles at sea level.
  83. * @param polarization Mie particle polarization factor.
  84. *
  85. * @see atmosphere::polarization
  86. *
  87. * @see Elek, Oskar. (2009). Rendering Parametrizable Planetary Atmospheres with Multiple Scattering in Real-Time.
  88. * @see Real-Time Spectral Scattering in Large-Scale Natural Participating Media.
  89. */
  90. template <class T>
  91. T scattering_mie(T density, T polarization)
  92. {
  93. return T(4) * math::pi<T> * density * polarization;
  94. }
  95. /**
  96. * Calculates attenuation due to extinction (absorption + out-scattering).
  97. *
  98. * @param ec Extinction coefficient (absorption coefficient + scattering coefficient).
  99. * @param s Scale factor.
  100. * @return Attenuation factor.
  101. */
  102. template <class T>
  103. T extinction(T ec, T s)
  104. {
  105. return std::exp(-(ec * s));
  106. }
  107. /**
  108. * Calculates the single-scattering albedo (SSA) given a scattering coefficient and an extinction coefficient.
  109. *
  110. * @param s Scattering coefficient.
  111. * @param e Extinction coefficient.
  112. * @return Single-scattering albedo.
  113. */
  114. template <class T>
  115. T albedo(T s, T e)
  116. {
  117. return s / t;
  118. }
  119. /**
  120. * Approximates the optical depth of exponentially-distributed atmospheric particles between two points using the trapezoidal rule.
  121. *
  122. * @param a Start point.
  123. * @param b End point.
  124. * @param r Radius of the planet.
  125. * @param sh Scale height of the atmospheric particles.
  126. * @param n Number of samples.
  127. * @return Optical depth between @p a and @p b.
  128. */
  129. template <class T>
  130. T optical_depth(const math::vector3<T>& a, const math::vector3<T>& b, T r, T sh, std::size_t n)
  131. {
  132. sh = T(-1) / sh;
  133. const T h = math::length(b - a) / T(n);
  134. math::vector3<T> dy = (b - a) / T(n);
  135. math::vector3<T> y = a + dy;
  136. T f_x = std::exp((math::length(a) - r) * sh);
  137. T f_y = std::exp((math::length(y) - r) * sh);
  138. T sum = (f_x + f_y);
  139. for (std::size_t i = 1; i < n; ++i)
  140. {
  141. f_x = f_y;
  142. y += dy;
  143. f_y = std::exp((math::length(y) - r) * sh);
  144. sum += (f_x + f_y);
  145. }
  146. return sum / T(2) * h;
  147. }
  148. } // namespace atmosphere
  149. } // namespace physics
  150. #endif // ANTKEEPER_PHYSICS_ATMOSPHERE_HPP