Home · All Namespaces · All Classes · Functions · Coding Style · Plugins · File Structure

modules/Skylight.hpp

00001 /*
00002  * Copyright (C) 2003 Fabien Chereau
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  */
00018 
00019 // Class which computes the daylight sky color
00020 // Fast implementation of the algorithm from the article
00021 // "A Practical Analytic Model for Daylight" by A. J. Preetham, Peter Shirley and Brian Smits.
00022 
00023 #ifndef _SKYLIGHT_HPP_
00024 #define _SKYLIGHT_HPP_
00025 
00026 #include <cmath>
00027 #include <QDebug>
00028 
00029 typedef struct {
00030     float zenithAngle;  // zenithAngle : angular distance to the zenith in radian
00031     float distSun;      // distSun     : angular distance to the sun in radian
00032     float color[3];     // 3 component color, can be RGB or CIE color system
00033 } skylightStruct;
00034 
00035 typedef struct {
00036     float pos[3];       // Vector to the position (vertical = pos[2])
00037     float color[3];     // 3 component color, can be RGB or CIE color system
00038 } skylightStruct2;
00039 
00040 class Skylight
00041 {
00042 public:
00043     Skylight();
00044     virtual ~Skylight();
00045     // Set the fixed parameters and precompute what can be
00046     // This funtion has to be called once before any call to get_*_value()
00047     void setParams(float sunZenithAngle, float turbidity);
00048     // Compute the sky color at the given position in the xyY color system and store it in position.color
00049     // void getxyYValue(skylightStruct * position);
00050     // Return the current zenith color
00051     inline void getZenithColor(float * v) const;
00052 
00053     // Same functions but in vector mode : faster because prevents extra cosine calculations
00054     // The position vectors MUST be normalized, and the vertical z component is the third one
00055     void setParamsv(const float * sunPos, float turbidity);
00056     
00057     // Compute the sky color at the given position in the CIE color system and store it in p.color
00058     // p.color[0] is CIE x color component
00059     // p.color[1] is CIE y color component
00060     // p.color[2] is CIE Y color component (luminance)
00061     void getxyYValuev(skylightStruct2& p) const
00062     {
00063         const float cosDistSun = sunPos[0]*p.pos[0] + sunPos[1]*p.pos[1] + sunPos[2]*p.pos[2];
00064         const float distSun = fastAcos(cosDistSun);
00065         const float cosDistSun_q = cosDistSun*cosDistSun;
00066 
00067         Q_ASSERT(p.pos[2] >= 0.f);
00068         const float oneOverCosZenithAngle = (p.pos[2]==0.) ? 1e99 : 1.f / p.pos[2];
00069         p.color[0] = term_x * (1.f + Ax * std::exp(Bx*oneOverCosZenithAngle))
00070                 * (1.f + Cx * std::exp(Dx*distSun) + Ex * cosDistSun_q);
00071 
00072         p.color[1] = term_y * (1.f + Ay * std::exp(By*oneOverCosZenithAngle))
00073                 * (1.f + Cy * std::exp(Dy*distSun) + Ey * cosDistSun_q);
00074 
00075         p.color[2] = term_Y * (1.f + AY * std::exp(BY*oneOverCosZenithAngle))
00076                 * (1.f + CY * std::exp(DY*distSun) + EY * cosDistSun_q);
00077 
00078 
00079         if (p.color[2] < 0. || p.color[0] < 0. || p.color[1] < 0.)
00080         {
00081             p.color[0] = 0.25;
00082             p.color[1] = 0.25;
00083             p.color[2] = 0.;
00084         }
00085     }
00086 
00087 private:
00088     float thetas;  // angular distance between the zenith and the sun in radian
00089     float T;       // Turbidity : i.e. sky "clarity"
00090                    //  1 : pure air
00091                    //  2 : exceptionnally clear
00092                    //  4 : clear
00093                    //  8 : light haze
00094                    // 25 : haze
00095                    // 64 : thin fog
00096 
00097     // Computed variables depending on the 2 above
00098     float zenithLuminance;     // Y color component of the CIE color at zenith (luminance)
00099     float zenithColorX;        // x color component of the CIE color at zenith
00100     float zenithColorY;        // y color component of the CIE color at zenith
00101 
00102     float eyeLumConversion;    // luminance conversion for an eye adapted to screen luminance 
00103                                // (around 40 cd/m^2)
00104 
00105     float AY, BY, CY, DY, EY;  // Distribution coefficients for the luminance distribution function
00106     float Ax, Bx, Cx, Dx, Ex;  // Distribution coefficients for x distribution function
00107     float Ay, By, Cy, Dy, Ey;  // Distribution coefficients for y distribution function
00108 
00109     float term_x;              // Precomputed term for x calculation
00110     float term_y;              // Precomputed term for y calculation
00111     float term_Y;              // Precomputed term for luminance calculation
00112 
00113     float sunPos[3];
00114 
00115     // Compute CIE Y (luminance) for zenith in cd/m^2
00116     inline void computeZenithLuminance(void);
00117     // Compute CIE x and y color components
00118     inline void computeZenithColor(void);
00119     // Compute the luminance distribution coefficients
00120     inline void computeLuminanceDistributionCoefs(void);
00121     // Compute the color distribution coefficients
00122     inline void computeColorDistributionCoefs(void);
00123 
00126     static inline float fastAcos(float x)
00127     {
00128         return M_PI_2 - (x + x*x*x * (1.f/6.f + x*x * (3.f/40.f + 5.f/112.f * x*x)) );
00129     }
00130 };
00131 
00132 // Return the current zenith color in xyY color system
00133 inline void Skylight::getZenithColor(float * v) const
00134 {
00135     v[0] = zenithColorX;
00136     v[1] = zenithColorY;
00137     v[2] = zenithLuminance;
00138 }
00139 
00140 // Compute CIE luminance for zenith in cd/m^2
00141 inline void Skylight::computeZenithLuminance(void)
00142 {
00143     zenithLuminance = 1000.f * ((4.0453f*T - 4.9710f) * std::tan( (0.4444f - T/120.f) * (M_PI-2.f*thetas) ) -
00144         0.2155f*T + 2.4192f);
00145     if (zenithLuminance<=0.f) zenithLuminance=0.00000000001;
00146 }
00147 
00148 // Compute CIE x and y color components
00149 // Edit: changed some coefficients to get new sky color
00150 inline void Skylight::computeZenithColor(void)
00151 {
00152     static float thetas2;
00153     static float thetas3;
00154     static float T2;
00155 
00156     thetas2 = thetas * thetas;
00157     thetas3 = thetas2 * thetas;
00158     T2 = T * T;
00159 
00160     zenithColorX = ( 0.00216f*thetas3 - 0.00375f*thetas2 + 0.00209f*thetas) * T2 +
00161                    (-0.02903f*thetas3 + 0.06377f*thetas2 - 0.03202f*thetas + 0.00394f) * T +
00162                    ( 0.10169f*thetas3 - 0.21196f*thetas2 + 0.06052f*thetas + 0.25886f);
00163 
00164     zenithColorY = ( 0.00275f*thetas3 - 0.00610f*thetas2 + 0.00317f*thetas) * T2 +
00165                    (-0.04214f*thetas3 + 0.08970f*thetas2 - 0.04153f*thetas + 0.00516f) * T +
00166                    ( 0.14535f*thetas3 - 0.26756f*thetas2 + 0.06670f*thetas + 0.26688f);
00167 
00168 }
00169 
00170 // Compute the luminance distribution coefficients
00171 // Edit: changed some coefficients to get new sky color
00172 inline void Skylight::computeLuminanceDistributionCoefs(void)
00173 {
00174     AY = 0.2787f*T - 1.0630f;
00175     BY =-0.3554f*T + 0.4275f;
00176     CY =-0.0227f*T + 6.3251f;
00177     DY = 0.1206f*T - 2.5771f;
00178     EY =-0.0670f*T + 0.3703f;
00179     // with BY>0 the formulas in getxyYValuev make no sense
00180     Q_ASSERT(BY <= 0.0);
00181 }
00182 
00183 // Compute the color distribution coefficients
00184 // Edit: changed some coefficients to get new sky color
00185 inline void Skylight::computeColorDistributionCoefs(void)
00186 {
00187     Ax =-0.0148f*T - 0.1703f;
00188     Bx =-0.0664f*T + 0.0011f;
00189     Cx =-0.0005f*T + 0.2127f;
00190     Dx =-0.0641f*T - 0.8992f;
00191     Ex =-0.0035f*T + 0.0453f;
00192 
00193     Ay =-0.0131f*T - 0.2498f;
00194     By =-0.0951f*T + 0.0092f;
00195     Cy =-0.0082f*T + 0.2404f;
00196     Dy =-0.0438f*T - 1.0539f;
00197     Ey =-0.0109f*T + 0.0531f;
00198     // with Bx,By>0 the formulas in getxyYValuev make no sense
00199     Q_ASSERT(Bx <= 0.0);
00200     Q_ASSERT(By <= 0.0);
00201 }
00202 
00203 
00204 #endif // _SKYLIGHT_H_
00205 

Generated on Mon Feb 2 17:23:47 2009 for Stellarium by  doxygen 1.5.5