|
2 | 2 | // This file is part of CosmoScout VR // |
3 | 3 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
4 | 4 |
|
5 | | -// SPDX-FileCopyrightText: 2017 Eric Bruneton |
6 | | -// SPDX-License-Identifier: BSD-3-Clause |
7 | | - |
8 | | -// This file has been directly copied from here: |
9 | | -// https://github.com/ebruneton/precomputed_atmospheric_scattering/blob/master/atmosphere/definitions.glsl |
10 | | -// The only differences should be related to formatting. The documentation below can also be read |
11 | | -// online at: |
12 | | -// https://ebruneton.github.io/precomputed_atmospheric_scattering/atmosphere/definitions.glsl.html |
13 | | - |
14 | | -/*<h2>atmosphere/definitions.glsl</h2> |
15 | | -
|
16 | | -<p>This GLSL file defines the physical types and constants which are used in the |
17 | | -main <a href="functions.glsl.html">functions</a> of our atmosphere model, in |
18 | | -such a way that they can be compiled by a GLSL compiler (a |
19 | | -<a href="reference/definitions.h.html">C++ equivalent</a> of this file |
20 | | -provides the same types and constants in C++, to allow the same functions to be |
21 | | -compiled by a C++ compiler - see the <a href="../index.html">Introduction</a>). |
22 | | -
|
23 | | -<h3>Physical quantities</h3> |
24 | | -
|
25 | | -<p>The physical quantities we need for our atmosphere model are |
26 | | -<a href="https://en.wikipedia.org/wiki/Radiometry">radiometric</a> and |
27 | | -<a href="https://en.wikipedia.org/wiki/Photometry_(optics)">photometric</a> |
28 | | -quantities. In GLSL we can't define custom numeric types to enforce the |
29 | | -homogeneity of expressions at compile time, so we define all the physical |
30 | | -quantities as <code>float</code>, with preprocessor macros (there is no |
31 | | -<code>typedef</code> in GLSL). |
32 | | -
|
33 | | -<p>We start with six base quantities: length, wavelength, angle, solid angle, |
34 | | -power and luminous power (wavelength is also a length, but we distinguish the |
35 | | -two for increased clarity). |
36 | | -*/ |
37 | | - |
38 | | -#define Length float |
39 | | -#define Wavelength float |
40 | | -#define Angle float |
41 | | -#define SolidAngle float |
42 | | -#define Power float |
43 | | -#define LuminousPower float |
44 | | - |
45 | | -/* |
46 | | -<p>From this we "derive" the irradiance, radiance, spectral irradiance, |
47 | | -spectral radiance, luminance, etc, as well pure numbers, area, volume, etc (the |
48 | | -actual derivation is done in the <a href="reference/definitions.h.html">C++ |
49 | | -equivalent</a> of this file). |
50 | | -*/ |
51 | | - |
52 | | -#define Number float |
53 | | -#define InverseLength float |
54 | | -#define Area float |
55 | | -#define Volume float |
56 | | -#define NumberDensity float |
57 | | -#define Irradiance float |
58 | | -#define Radiance float |
59 | | -#define SpectralPower float |
60 | | -#define SpectralIrradiance float |
61 | | -#define SpectralRadiance float |
62 | | -#define SpectralRadianceDensity float |
63 | | -#define ScatteringCoefficient float |
64 | | -#define InverseSolidAngle float |
65 | | -#define LuminousIntensity float |
66 | | -#define Luminance float |
67 | | -#define Illuminance float |
68 | | - |
69 | | -/* |
70 | | -<p>We also need vectors of physical quantities, mostly to represent functions |
71 | | -depending on the wavelength. In this case the vector elements correspond to |
72 | | -values of a function at some predefined wavelengths. Again, in GLSL we can't |
73 | | -define custom vector types to enforce the homogeneity of expressions at compile |
74 | | -time, so we define these vector types as <code>vec3</code>, with preprocessor |
75 | | -macros. The full definitions are given in the |
76 | | -<a href="reference/definitions.h.html">C++ equivalent</a> of this file). |
77 | | -*/ |
78 | | - |
79 | | -// A generic function from Wavelength to some other type. |
80 | | -#define AbstractSpectrum vec3 |
81 | | -// A function from Wavelength to Number. |
82 | | -#define DimensionlessSpectrum vec3 |
83 | | -// A function from Wavelength to SpectralPower. |
84 | | -#define PowerSpectrum vec3 |
85 | | -// A function from Wavelength to SpectralIrradiance. |
86 | | -#define IrradianceSpectrum vec3 |
87 | | -// A function from Wavelength to SpectralRadiance. |
88 | | -#define RadianceSpectrum vec3 |
89 | | -// A function from Wavelength to SpectralRadianceDensity. |
90 | | -#define RadianceDensitySpectrum vec3 |
91 | | -// A function from Wavelength to ScaterringCoefficient. |
92 | | -#define ScatteringSpectrum vec3 |
93 | | - |
94 | | -// A position in 3D (3 length values). |
95 | | -#define Position vec3 |
96 | | -// A unit direction vector in 3D (3 unitless values). |
97 | | -#define Direction vec3 |
98 | | -// A vector of 3 luminance values. |
99 | | -#define Luminance3 vec3 |
100 | | -// A vector of 3 illuminance values. |
101 | | -#define Illuminance3 vec3 |
102 | | - |
103 | | -/* |
104 | | -<p>Finally, we also need precomputed textures containing physical quantities in |
105 | | -each texel. Since we can't define custom sampler types to enforce the |
106 | | -homogeneity of expressions at compile time in GLSL, we define these texture |
107 | | -types as <code>sampler2D</code> and <code>sampler3D</code>, with preprocessor |
108 | | -macros. The full definitions are given in the |
109 | | -<a href="reference/definitions.h.html">C++ equivalent</a> of this file). |
110 | | -*/ |
111 | | - |
112 | | -#define TransmittanceTexture sampler2D |
113 | | -#define AbstractScatteringTexture sampler3D |
114 | | -#define ReducedScatteringTexture sampler3D |
115 | | -#define ScatteringTexture sampler3D |
116 | | -#define ScatteringDensityTexture sampler3D |
117 | | -#define IrradianceTexture sampler2D |
118 | | - |
119 | | -/* |
120 | | -<h3>Physical units</h3> |
121 | | -
|
122 | | -<p>We can then define the units for our six base physical quantities: |
123 | | -meter (m), nanometer (nm), radian (rad), steradian (sr), watt (watt) and lumen |
124 | | -(lm): |
125 | | -*/ |
126 | | - |
127 | | -const Length m = 1.0; |
128 | | -const Wavelength nm = 1.0; |
129 | | -const Angle rad = 1.0; |
130 | | -const SolidAngle sr = 1.0; |
131 | | -const Power watt = 1.0; |
132 | | -const LuminousPower lm = 1.0; |
133 | | - |
134 | | -/* |
135 | | -<p>From which we can derive the units for some derived physical quantities, |
136 | | -as well as some derived units (kilometer km, kilocandela kcd, degree deg): |
137 | | -*/ |
| 5 | +// SPDX-FileCopyrightText: German Aerospace Center (DLR) <[email protected]> |
| 6 | +// SPDX-License-Identifier: MIT |
138 | 7 |
|
139 | 8 | const float PI = 3.14159265358979323846; |
140 | 9 |
|
141 | | -const Length km = 1000.0 * m; |
142 | | -const Area m2 = m * m; |
143 | | -const Volume m3 = m * m * m; |
144 | | -const Angle pi = PI * rad; |
145 | | -const Angle deg = pi / 180.0; |
146 | | -const Irradiance watt_per_square_meter = watt / m2; |
147 | | -const Radiance watt_per_square_meter_per_sr = watt / (m2 * sr); |
148 | | -const SpectralIrradiance watt_per_square_meter_per_nm = watt / (m2 * nm); |
149 | | -const SpectralRadiance watt_per_square_meter_per_sr_per_nm = watt / (m2 * sr * nm); |
150 | | -const SpectralRadianceDensity watt_per_cubic_meter_per_sr_per_nm = watt / (m3 * sr * nm); |
151 | | -const LuminousIntensity cd = lm / sr; |
152 | | -const LuminousIntensity kcd = 1000.0 * cd; |
153 | | -const Luminance cd_per_square_meter = cd / m2; |
154 | | -const Luminance kcd_per_square_meter = kcd / m2; |
| 10 | +// This texture contains all the phase functions for the scattering components of the atmosphere. |
| 11 | +// The u coordinate maps to the scattering angle. Forward scattering is on the left (u == 0, theta |
| 12 | +// == 0) and back scattering on the right (u == 1, theta == 180). |
| 13 | +// The v coordinate maps to the various components. The phase function of the first scattering |
| 14 | +// component is stored in the top row of pixels, the second in the next and so on. So usually the |
| 15 | +// phase function for molecules is in the top row and the phase function for aerosols is in the |
| 16 | +// bottom row. |
| 17 | +uniform sampler2D uPhaseTexture; |
| 18 | + |
| 19 | +// This texture contains all the density functions for the components of the atmosphere. The density |
| 20 | +// function maps a relative density value in [0..1] to each altitude. The u coordinate corresponds |
| 21 | +// to the altitude; atmosphere's bottom is on the left (u == 0) and the top is the right (u == 1). |
| 22 | +// The v coordinate maps to the various components. The density function of the first component is |
| 23 | +// stored in the top row of pixels, the second in the next and so on. So usually the molecules are |
| 24 | +// in the first row, aerosols in the second row, and ozone is in the last row. |
| 25 | +// The density_texture is only sampled during preprocessing. It is not used at runtime in the final |
| 26 | +// fragment shader. |
| 27 | +uniform sampler2D uDensityTexture; |
| 28 | + |
| 29 | +// Scattering components can absorb and scatter light. Air molecules and aerosols in Earth's |
| 30 | +// atmosphere are both modelled using scattering components. |
| 31 | +struct ScatteringComponent { |
| 32 | + |
| 33 | + // The vertical texture coordinate of the component's the phse function in phase_texture. |
| 34 | + float phaseTextureV; |
| 35 | + |
| 36 | + // The vertical texture coordinate of the component's the density function in density_texture. |
| 37 | + float densityTextureV; |
| 38 | + |
| 39 | + // The extinction coefficient beta_ext in m^-1. |
| 40 | + vec3 extinction; |
| 41 | + |
| 42 | + // The extinction coefficient beta_sca in m^-1. |
| 43 | + vec3 scattering; |
| 44 | +}; |
155 | 45 |
|
156 | | -/* |
157 | | -<h3>Atmosphere parameters</h3> |
| 46 | +// Absorbing components can absorb light but do not scatter light. Ozone in Earth's atmosphere is |
| 47 | +// modelled using an absorbing component. |
| 48 | +struct AbsorbingComponent { |
158 | 49 |
|
159 | | -<p>Using the above types, we can now define the parameters of our atmosphere |
160 | | -model. We start with the definition of density profiles, which are needed for |
161 | | -parameters that depend on the altitude: |
162 | | -*/ |
| 50 | + // The vertical texture coordinate of the component's the density function in density_texture. |
| 51 | + float densityTextureV; |
163 | 52 |
|
164 | | -// An atmosphere layer of width 'width', and whose density is defined as |
165 | | -// 'exp_term' * exp('exp_scale' * h) + 'linear_term' * h + 'constant_term', |
166 | | -// clamped to [0,1], and where h is the altitude. |
167 | | -struct DensityProfileLayer { |
168 | | - Length width; |
169 | | - Number exp_term; |
170 | | - InverseLength exp_scale; |
171 | | - InverseLength linear_term; |
172 | | - Number constant_term; |
| 53 | + // The extinction coefficient beta_ext in m^-1. |
| 54 | + vec3 extinction; |
173 | 55 | }; |
174 | 56 |
|
175 | | -// An atmosphere density profile made of several layers on top of each other |
176 | | -// (from bottom to top). The width of the last layer is ignored, i.e. it always |
177 | | -// extend to the top atmosphere boundary. The profile values vary between 0 |
178 | | -// (null density) to 1 (maximum density). |
179 | | -struct DensityProfile { |
180 | | - DensityProfileLayer layers[2]; |
181 | | -}; |
182 | | - |
183 | | -/* |
184 | | -The atmosphere parameters are then defined by the following struct: |
185 | | -*/ |
186 | | - |
187 | | -struct AtmosphereParameters { |
188 | | - // The solar irradiance at the top of the atmosphere. |
189 | | - IrradianceSpectrum solar_irradiance; |
190 | | - // The sun's angular radius. Warning: the implementation uses approximations |
191 | | - // that are valid only if this angle is smaller than 0.1 radians. |
192 | | - Angle sun_angular_radius; |
193 | | - // The distance between the planet center and the bottom of the atmosphere. |
194 | | - Length bottom_radius; |
195 | | - // The distance between the planet center and the top of the atmosphere. |
196 | | - Length top_radius; |
197 | | - // The density profile of air molecules, i.e. a function from altitude to |
198 | | - // dimensionless values between 0 (null density) and 1 (maximum density). |
199 | | - DensityProfile rayleigh_density; |
200 | | - // The scattering coefficient of air molecules at the altitude where their |
201 | | - // density is maximum (usually the bottom of the atmosphere), as a function of |
202 | | - // wavelength. The scattering coefficient at altitude h is equal to |
203 | | - // 'rayleigh_scattering' times 'rayleigh_density' at this altitude. |
204 | | - ScatteringSpectrum rayleigh_scattering; |
205 | | - // The density profile of aerosols, i.e. a function from altitude to |
206 | | - // dimensionless values between 0 (null density) and 1 (maximum density). |
207 | | - DensityProfile mie_density; |
208 | | - // The scattering coefficient of aerosols at the altitude where their density |
209 | | - // is maximum (usually the bottom of the atmosphere), as a function of |
210 | | - // wavelength. The scattering coefficient at altitude h is equal to |
211 | | - // 'mie_scattering' times 'mie_density' at this altitude. |
212 | | - ScatteringSpectrum mie_scattering; |
213 | | - // The extinction coefficient of aerosols at the altitude where their density |
214 | | - // is maximum (usually the bottom of the atmosphere), as a function of |
215 | | - // wavelength. The extinction coefficient at altitude h is equal to |
216 | | - // 'mie_extinction' times 'mie_density' at this altitude. |
217 | | - ScatteringSpectrum mie_extinction; |
218 | | - // The asymetry parameter for the Cornette-Shanks phase function for the |
219 | | - // aerosols. |
220 | | - Number mie_phase_function_g; |
221 | | - // The density profile of air molecules that absorb light (e.g. ozone), i.e. |
222 | | - // a function from altitude to dimensionless values between 0 (null density) |
223 | | - // and 1 (maximum density). |
224 | | - DensityProfile absorption_density; |
225 | | - // The extinction coefficient of molecules that absorb light (e.g. ozone) at |
226 | | - // the altitude where their density is maximum, as a function of wavelength. |
227 | | - // The extinction coefficient at altitude h is equal to |
228 | | - // 'absorption_extinction' times 'absorption_density' at this altitude. |
229 | | - ScatteringSpectrum absorption_extinction; |
230 | | - // The average albedo of the ground. |
231 | | - DimensionlessSpectrum ground_albedo; |
232 | | - // The cosine of the maximum Sun zenith angle for which atmospheric scattering |
233 | | - // must be precomputed (for maximum precision, use the smallest Sun zenith |
234 | | - // angle yielding negligible sky light radiance values. For instance, for the |
235 | | - // Earth case, 102 degrees is a good choice - yielding mu_s_min = -0.2). |
236 | | - Number mu_s_min; |
| 57 | +// An atmosphere consists of two scattering components and an absorbing component. The scattering |
| 58 | +// components can absorb light as well, but the absorbing component can not scatter light. |
| 59 | +struct AtmosphereComponents { |
| 60 | + ScatteringComponent molecules; |
| 61 | + ScatteringComponent aerosols; |
| 62 | + AbsorbingComponent ozone; |
237 | 63 | }; |
0 commit comments