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

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