Stellarium 0.12.3
Skylight.hpp
1 /*
2  * Copyright (C) 2003 Fabien Chereau
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
17  */
18 
19 // Class which computes the daylight sky color
20 // Fast implementation of the algorithm from the article
21 // "A Practical Analytic Model for Daylight" by A. J. Preetham, Peter Shirley and Brian Smits.
22 
23 #ifndef _SKYLIGHT_HPP_
24 #define _SKYLIGHT_HPP_
25 
26 #include <cmath>
27 #include <QDebug>
28 #include "StelUtils.hpp"
29 
30 typedef struct {
31  float zenithAngle; // zenithAngle : angular distance to the zenith in radian
32  float distSun; // distSun : angular distance to the sun in radian
33  float color[3]; // 3 component color, can be RGB or CIE color system
35 
36 typedef struct {
37  float pos[3]; // Vector to the position (vertical = pos[2])
38  float color[3]; // 3 component color, can be RGB or CIE color system
40 
41 class Skylight
42 {
43 public:
44  Skylight();
45  virtual ~Skylight();
46  // Set the fixed parameters and precompute what can be
47  // This function has to be called once before any call to get_*_value()
48  void setParams(float sunZenithAngle, float turbidity);
49  // Compute the sky color at the given position in the xyY color system and store it in position.color
50  // void getxyYValue(skylightStruct * position);
51  // Return the current zenith color
52  inline void getZenithColor(float * v) const;
53 
54  // Same functions but in vector mode : faster because prevents extra cosine calculations
55  // The position vectors MUST be normalized, and the vertical z component is the third one
56  void setParamsv(const float * sunPos, float turbidity);
57 
58  // Compute the sky color at the given position in the CIE color system and store it in p.color
59  // p.color[0] is CIE x color component
60  // p.color[1] is CIE y color component
61  // p.color[2] is undefined (CIE Y color component (luminance) if uncommented)
62  void getxyYValuev(skylightStruct2& p) const
63  {
64  const float cosDistSun = sunPos[0]*p.pos[0] + sunPos[1]*p.pos[1] + sunPos[2]*p.pos[2];
65  const float distSun = StelUtils::fastAcos(cosDistSun);
66  const float cosDistSun_q = cosDistSun*cosDistSun;
67 
68  Q_ASSERT(p.pos[2] >= 0.f);
69  const float oneOverCosZenithAngle = (p.pos[2]==0.) ? 1e99 : 1.f / p.pos[2];
70  p.color[0] = term_x * (1.f + Ax * std::exp(Bx*oneOverCosZenithAngle))
71  * (1.f + Cx * std::exp(Dx*distSun) + Ex * cosDistSun_q);
72 
73  p.color[1] = term_y * (1.f + Ay * std::exp(By*oneOverCosZenithAngle))
74  * (1.f + Cy * std::exp(Dy*distSun) + Ey * cosDistSun_q);
75 
76 // p.color[2] = term_Y * (1.f + AY * std::exp(BY*oneOverCosZenithAngle))
77 // * (1.f + CY * std::exp(DY*distSun) + EY * cosDistSun_q);
78 
79 
80  if (/*p.color[2] < 0. || */p.color[0] < 0. || p.color[1] < 0.)
81  {
82  p.color[0] = 0.25;
83  p.color[1] = 0.25;
84  p.color[2] = 0.;
85  }
86  }
87 
88  void getShadersParams(Vec3f& asunPos, float& aterm_x, float& aAx, float& aBx, float& aCx, float& aDx, float& aEx,
89  float& aterm_y, float& aAy, float& aBy, float& aCy, float& aDy, float& aEy) const
90  {
91  asunPos=sunPos;
92  aterm_x=term_x;aAx=Ax;aBx=Bx;aCx=Cx;aDx=Dx;aEx=Ex;
93  aterm_y=term_y;aAy=Ay;aBy=By;aCy=Cy;aDy=Dy;aEy=Ey;
94  }
95 
96 
97 
98 private:
99  float thetas; // angular distance between the zenith and the sun in radian
100  float T; // Turbidity : i.e. sky "clarity"
101  // 1 : pure air
102  // 2 : exceptionnally clear
103  // 4 : clear
104  // 8 : light haze
105  // 25 : haze
106  // 64 : thin fog
107 
108  // Computed variables depending on the 2 above
109  float zenithLuminance; // Y color component of the CIE color at zenith (luminance)
110  float zenithColorX; // x color component of the CIE color at zenith
111  float zenithColorY; // y color component of the CIE color at zenith
112 
113  float eyeLumConversion; // luminance conversion for an eye adapted to screen luminance
114  // (around 40 cd/m^2)
115 
116  float AY, BY, CY, DY, EY; // Distribution coefficients for the luminance distribution function
117  float Ax, Bx, Cx, Dx, Ex; // Distribution coefficients for x distribution function
118  float Ay, By, Cy, Dy, Ey; // Distribution coefficients for y distribution function
119 
120  float term_x; // Precomputed term for x calculation
121  float term_y; // Precomputed term for y calculation
122  float term_Y; // Precomputed term for luminance calculation
123 
124  float sunPos[3];
125 
126  // Compute CIE Y (luminance) for zenith in cd/m^2
127  inline void computeZenithLuminance(void);
128  // Compute CIE x and y color components
129  inline void computeZenithColor(void);
130  // Compute the luminance distribution coefficients
131  inline void computeLuminanceDistributionCoefs(void);
132  // Compute the color distribution coefficients
133  inline void computeColorDistributionCoefs(void);
134 };
135 
136 // Return the current zenith color in xyY color system
137 inline void Skylight::getZenithColor(float * v) const
138 {
139  v[0] = zenithColorX;
140  v[1] = zenithColorY;
141  v[2] = zenithLuminance;
142 }
143 
144 // Compute CIE luminance for zenith in cd/m^2
145 inline void Skylight::computeZenithLuminance(void)
146 {
147  zenithLuminance = 1000.f * ((4.0453f*T - 4.9710f) * std::tan( (0.4444f - T/120.f) * (M_PI-2.f*thetas) ) -
148  0.2155f*T + 2.4192f);
149  if (zenithLuminance<=0.f) zenithLuminance=0.00000000001;
150 }
151 
152 // Compute CIE x and y color components
153 // Edit: changed some coefficients to get new sky color
154 inline void Skylight::computeZenithColor(void)
155 {
156  static float thetas2;
157  static float thetas3;
158  static float T2;
159 
160  thetas2 = thetas * thetas;
161  thetas3 = thetas2 * thetas;
162  T2 = T * T;
163 
164  zenithColorX = ( 0.00216f*thetas3 - 0.00375f*thetas2 + 0.00209f*thetas) * T2 +
165  (-0.02903f*thetas3 + 0.06377f*thetas2 - 0.03202f*thetas + 0.00394f) * T +
166  ( 0.10169f*thetas3 - 0.21196f*thetas2 + 0.06052f*thetas + 0.25886f);
167 
168  zenithColorY = ( 0.00275f*thetas3 - 0.00610f*thetas2 + 0.00317f*thetas) * T2 +
169  (-0.04214f*thetas3 + 0.08970f*thetas2 - 0.04153f*thetas + 0.00516f) * T +
170  ( 0.14535f*thetas3 - 0.26756f*thetas2 + 0.06670f*thetas + 0.26688f);
171 
172 }
173 
174 // Compute the luminance distribution coefficients
175 // Edit: changed some coefficients to get new sky color
176 inline void Skylight::computeLuminanceDistributionCoefs(void)
177 {
178  AY = 0.2787f*T - 1.0630f;
179  BY =-0.3554f*T + 0.4275f;
180  CY =-0.0227f*T + 6.3251f;
181  DY = 0.1206f*T - 2.5771f;
182  EY =-0.0670f*T + 0.3703f;
183  // with BY>0 the formulas in getxyYValuev make no sense
184  Q_ASSERT(BY <= 0.0);
185 }
186 
187 // Compute the color distribution coefficients
188 // Edit: changed some coefficients to get new sky color
189 inline void Skylight::computeColorDistributionCoefs(void)
190 {
191  Ax =-0.0148f*T - 0.1703f;
192  Bx =-0.0664f*T + 0.0011f;
193  Cx =-0.0005f*T + 0.2127f;
194  Dx =-0.0641f*T - 0.8992f;
195  Ex =-0.0035f*T + 0.0453f;
196 
197  Ay =-0.0131f*T - 0.2498f;
198  By =-0.0951f*T + 0.0092f;
199  Cy =-0.0082f*T + 0.2404f;
200  Dy =-0.0438f*T - 1.0539f;
201  Ey =-0.0109f*T + 0.0531f;
202  // with Bx,By>0 the formulas in getxyYValuev make no sense
203  Q_ASSERT(Bx <= 0.0);
204  Q_ASSERT(By <= 0.0);
205 }
206 
207 
208 #endif // _SKYLIGHT_H_
209