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

165 lines
4.6 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 polarization factor used in computing scattering coefficients.
  44. *
  45. * @param ior Atmospheric index of refraction at sea level.
  46. * @param density Molecular density at sea level.
  47. * @return Polarization factor.
  48. *
  49. * @see Elek, Oskar. (2009). Rendering Parametrizable Planetary Atmospheres with Multiple Scattering in Real-Time.
  50. */
  51. template <class T>
  52. T polarization(T ior, T density)
  53. {
  54. const T ior2 = ior * ior;
  55. const T num = T(2) * math::pi<T> * math::pi<T> * ((ior2 - T(1.0)) * (ior2 - T(1.0)));
  56. const T den = T(3) * density * density;
  57. return num / den;
  58. }
  59. /**
  60. * Calculates a Rayleigh scattering coefficient at sea level (wavelength-dependent).
  61. *
  62. * @param wavelength Wavelength of light, in meters.
  63. * @param density Molecular density of Rayleigh particles at sea level.
  64. * @param polarization Rayleigh particle polarization factor.
  65. *
  66. * @see atmosphere::polarization
  67. *
  68. * @see Elek, Oskar. (2009). Rendering Parametrizable Planetary Atmospheres with Multiple Scattering in Real-Time.
  69. */
  70. template <class T>
  71. T scatter_rayleigh(T wavelength, T density, T polarization)
  72. {
  73. const T wavelength2 = wavelength * wavelength;
  74. return T(4) * math::pi<T> * density / (wavelength2 * wavelength2) * polarization;
  75. }
  76. /**
  77. * Calculates a Mie scattering coefficient at sea level (wavelength-independent).
  78. *
  79. * @param density Molecular density of Mie particles at sea level.
  80. * @param polarization Mie particle polarization factor.
  81. *
  82. * @see atmosphere::polarization
  83. *
  84. * @see Elek, Oskar. (2009). Rendering Parametrizable Planetary Atmospheres with Multiple Scattering in Real-Time.
  85. */
  86. template <class T>
  87. T scatter_mie(T density, T polarization)
  88. {
  89. return T(4) * math::pi<T> * density * polarization;
  90. }
  91. /**
  92. * Calculates an extinction coefficient given a scattering coefficient and an absorbtion coefficient.
  93. *
  94. * @param s Scattering coefficient.
  95. * @param a Absorbtion coefficient.
  96. * @return Extinction coefficient.
  97. */
  98. template <class T>
  99. T extinction(T s, T a)
  100. {
  101. return s + a;
  102. }
  103. /**
  104. * Calculates the single-scattering albedo (SSA) given a scattering coefficient and an extinction coefficient.
  105. *
  106. * @param s Scattering coefficient.
  107. * @param e Extinction coefficient.
  108. * @return Single-scattering albedo.
  109. */
  110. template <class T>
  111. T albedo(T s, T e)
  112. {
  113. return s / t;
  114. }
  115. /**
  116. * Approximates the optical depth of exponentially-distributed atmospheric particles between two points using the trapezoidal rule.
  117. *
  118. * @param a Start point.
  119. * @param b End point.
  120. * @param r Radius of the planet.
  121. * @param sh Scale height of the atmospheric particles.
  122. * @param n Number of samples.
  123. * @return Optical depth between @p a and @p b.
  124. */
  125. template <class T>
  126. T optical_depth(const math::vector3<T>& a, const math::vector3<T>& b, T r, T sh, std::size_t n)
  127. {
  128. sh = T(-1) / sh;
  129. const T h = math::length(b - a) / T(n);
  130. math::vector3<T> dy = (b - a) / T(n);
  131. math::vector3<T> y = a + dy;
  132. T f_x = std::exp((math::length(a) - r) * sh);
  133. T f_y = std::exp((math::length(y) - r) * sh);
  134. T sum = (f_x + f_y);
  135. for (std::size_t i = 1; i < n; ++i)
  136. {
  137. f_x = f_y;
  138. y += dy;
  139. f_y = std::exp((math::length(y) - r) * sh);
  140. sum += (f_x + f_y);
  141. }
  142. return sum / T(2) * h;
  143. }
  144. } // namespace atmosphere
  145. } // namespace physics
  146. #endif // ANTKEEPER_PHYSICS_ATMOSPHERE_HPP