paysages : Preetham approximation for sky (WIP).
git-svn-id: https://subversion.assembla.com/svn/thunderk/paysages@355 b1fd45b6-86a6-48da-8261-f70d1f35bdcc
This commit is contained in:
parent
9e21a37c04
commit
05f2c19ef6
1 changed files with 92 additions and 132 deletions
|
@ -158,85 +158,38 @@ static inline double _angleBetween(double thetav, double phiv, double theta, dou
|
||||||
return acos(cospsi);
|
return acos(cospsi);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline double _perezFunction(double A, double B, double C, double D, double E, double Theta, double Gamma)
|
static inline Color _toColor(float x, float y, float Y)
|
||||||
{
|
{
|
||||||
double cosGamma = cos(Gamma);
|
float fX, fY, fZ;
|
||||||
return (1.0 + A * exp(B / cos(Theta))) * (1.0 + C * exp(D * Gamma) + E * cosGamma * cosGamma);
|
Color result;
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct
|
fY = Y;
|
||||||
{
|
fX = x / y * Y;
|
||||||
double x;
|
fZ = ((1.0f - x - y) / y) * Y;
|
||||||
double y;
|
|
||||||
double Y;
|
|
||||||
} ColorxyY;
|
|
||||||
|
|
||||||
/* Distribution coefficients for the luminance(Y) distribution function */
|
float r, g, b;
|
||||||
static double YDC[5][2] = { { 0.1787, - 1.4630},
|
|
||||||
{-0.3554, 0.4275},
|
r = 3.2404f * fX - 1.5371f * fY - 0.4985f * fZ;
|
||||||
{-0.0227, 5.3251},
|
g = -0.9692f * fX + 1.8759f * fY + 0.0415f * fZ;
|
||||||
{ 0.1206, - 2.5771},
|
b = 0.0556f * fX - 0.2040f * fY + 1.0573f * fZ;
|
||||||
{-0.0670, 0.3703} };
|
|
||||||
|
|
||||||
/* Distribution coefficients for the x distribution function */
|
float expo = -(1.0f / 10000.0f);
|
||||||
static double xDC[5][2] = { {-0.0193, -0.2592},
|
r = 1.0f - exp(expo * r);
|
||||||
{-0.0665, 0.0008},
|
g = 1.0f - exp(expo * g);
|
||||||
{-0.0004, 0.2125},
|
b = 1.0f - exp(expo * b);
|
||||||
{-0.0641, -0.8989},
|
|
||||||
{-0.0033, 0.0452} };
|
|
||||||
|
|
||||||
/* Distribution coefficients for the y distribution function */
|
if (r < 0.0f) r = 0.0f;
|
||||||
static double yDC[5][2] = { {-0.0167, -0.2608},
|
if (g < 0.0f) g = 0.0f;
|
||||||
{-0.0950, 0.0092},
|
if (b < 0.0f) b = 0.0f;
|
||||||
{-0.0079, 0.2102},
|
|
||||||
{-0.0441, -1.6537},
|
|
||||||
{-0.0109, 0.0529} };
|
|
||||||
|
|
||||||
/* Zenith x value */
|
result.r = r;
|
||||||
static double xZC[3][4] = { {0.00166, -0.00375, 0.00209, 0},
|
result.g = g;
|
||||||
{-0.02903, 0.06377, -0.03203, 0.00394},
|
result.b = b;
|
||||||
{0.11693, -0.21196, 0.06052, 0.25886} };
|
result.a = 1.0;
|
||||||
/* Zenith y value */
|
|
||||||
static double yZC[3][4] = { { 0.00275, -0.00610, 0.00317, 0},
|
colorNormalize(&result);
|
||||||
{-0.04214, 0.08970, -0.04153, 0.00516},
|
|
||||||
{0.15346, -0.26756, 0.06670, 0.26688} };
|
return result;
|
||||||
|
|
||||||
static double _distribution(double A, double B, double C, double D, double E, double Theta, double Gamma, double ThetaSun)
|
|
||||||
{
|
|
||||||
double f0 = _perezFunction(A,B,C,D,E,Theta,Gamma);
|
|
||||||
double f1 = _perezFunction(A,B,C,D,E,0,ThetaSun);
|
|
||||||
return(f0/f1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static double _chromaticity( double ZC[3][4], double ThetaSun, double turbidity)
|
|
||||||
{
|
|
||||||
double t1 = ThetaSun;
|
|
||||||
double t2 = t1*t1;
|
|
||||||
double t3 = t2 * t1;
|
|
||||||
|
|
||||||
double c = (ZC[0][0]*t3 + ZC[0][1]*t2 + ZC[0][2]*t1 + ZC[0][3])* turbidity * turbidity +
|
|
||||||
(ZC[1][0]*t3 + ZC[1][1]*t2 + ZC[1][2]*t1 + ZC[1][3])* turbidity +
|
|
||||||
(ZC[2][0]*t3 + ZC[2][1]*t2 + ZC[2][2]*t1 + ZC[2][3]);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Color _toColor(ColorxyY src)
|
|
||||||
{
|
|
||||||
double X, Y, Z;
|
|
||||||
Color dest;
|
|
||||||
|
|
||||||
/* Convert to XYZ space */
|
|
||||||
X = src.x * (src.Y / src.y);
|
|
||||||
Y = src.Y;
|
|
||||||
Z = (1.0 - src.x - src.y)* (src.Y/src.y);
|
|
||||||
|
|
||||||
/* Convert to RGB */
|
|
||||||
dest.r = 3.240479 * X - 1.537150 * Y - 0.498535 * Z;
|
|
||||||
dest.g = - 0.969256 * X + 1.875991 * Y + 0.041556 * Z;
|
|
||||||
dest.b = 0.055648 * X - 0.204043 * Y + 1.057311 * Z;
|
|
||||||
dest.a = 1.0;
|
|
||||||
|
|
||||||
return dest;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static Color _preethamApproximate(SkyDefinition* definition, double theta, double phi)
|
static Color _preethamApproximate(SkyDefinition* definition, double theta, double phi)
|
||||||
|
@ -244,82 +197,89 @@ static Color _preethamApproximate(SkyDefinition* definition, double theta, doubl
|
||||||
double thetaSun;
|
double thetaSun;
|
||||||
double phiSun;
|
double phiSun;
|
||||||
double gamma;
|
double gamma;
|
||||||
double turbidity = 3.0;
|
double turbidity = 2.0;
|
||||||
ColorxyY skycolor;
|
|
||||||
ColorxyY Zenith;
|
|
||||||
double A,B,C,D,E;
|
|
||||||
double d,chi;
|
|
||||||
|
|
||||||
/* Handle angles */
|
/* Handle angles */
|
||||||
|
if (theta > M_PI / 2.0)
|
||||||
|
{
|
||||||
|
theta = M_PI / 2.0;
|
||||||
|
}
|
||||||
if (definition->daytime <= 0.5)
|
if (definition->daytime <= 0.5)
|
||||||
{
|
{
|
||||||
if (definition->daytime <= 0.25)
|
thetaSun = M_PI - definition->daytime * 2.0 * M_PI;
|
||||||
{
|
|
||||||
thetaSun = M_PI / 2.0 - 0.00001;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
thetaSun = M_PI - definition->daytime * 2.0 * M_PI;
|
|
||||||
}
|
|
||||||
phiSun = 0.0;
|
phiSun = 0.0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (definition->daytime >= 0.75)
|
thetaSun = (definition->daytime - 0.5) * 2.0 * M_PI;
|
||||||
{
|
|
||||||
thetaSun = M_PI / 2.0 - 0.00001;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
thetaSun = (definition->daytime - 0.5) * 2.0 * M_PI;
|
|
||||||
}
|
|
||||||
phiSun = M_PI;
|
phiSun = M_PI;
|
||||||
}
|
}
|
||||||
gamma = _angleBetween(theta, phi, thetaSun, phiSun);
|
gamma = _angleBetween(theta, phi, thetaSun, phiSun);
|
||||||
|
|
||||||
|
double cosTheta;
|
||||||
|
if (theta > M_PI / 2.0)
|
||||||
|
{
|
||||||
|
cosTheta = 0.0000001;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cosTheta = cos(theta);
|
||||||
|
}
|
||||||
|
|
||||||
|
double T = turbidity;
|
||||||
|
double T2 = T * T;
|
||||||
|
double suntheta = thetaSun;
|
||||||
|
double suntheta2 = thetaSun * thetaSun;
|
||||||
|
double suntheta3 = thetaSun * suntheta2;
|
||||||
|
|
||||||
/* Zenith luminance */
|
double Ax = -0.01925 * T - 0.25922;
|
||||||
chi = (4.0/9.0 - turbidity/120.0)*(M_PI - 2*thetaSun);
|
double Bx = -0.06651 * T + 0.00081;
|
||||||
Zenith.Y = (4.0453*turbidity - 4.9710)*tan(chi) - 0.2155*turbidity + 2.4192;
|
double Cx = -0.00041 * T + 0.21247;
|
||||||
if (Zenith.Y < 0.0) Zenith.Y = -Zenith.Y;
|
double Dx = -0.06409 * T - 0.89887;
|
||||||
|
double Ex = -0.00325 * T + 0.04517;
|
||||||
|
|
||||||
A = YDC[0][0]*turbidity + YDC[0][1];
|
double Ay = -0.01669 * T - 0.26078;
|
||||||
B = YDC[1][0]*turbidity + YDC[1][1];
|
double By = -0.09495 * T + 0.00921;
|
||||||
C = YDC[2][0]*turbidity + YDC[2][1];
|
double Cy = -0.00792 * T + 0.21023;
|
||||||
D = YDC[3][0]*turbidity + YDC[3][1];
|
double Dy = -0.04405 * T - 1.65369;
|
||||||
E = YDC[4][0]*turbidity + YDC[4][1];
|
double Ey = -0.01092 * T + 0.05291;
|
||||||
|
|
||||||
/* Sky luminance */
|
double AY = 0.17872 * T - 1.46303;
|
||||||
d = _distribution(A, B, C, D, E, theta, gamma, thetaSun);
|
double BY = -0.35540 * T + 0.42749;
|
||||||
skycolor.Y = Zenith.Y * d;
|
double CY = -0.02266 * T + 5.32505;
|
||||||
|
double DY = 0.12064 * T - 2.57705;
|
||||||
|
double EY = -0.06696 * T + 0.37027;
|
||||||
|
|
||||||
/* Zenith x */
|
double cosGamma = cos(gamma);
|
||||||
Zenith.x = _chromaticity( xZC, thetaSun, turbidity );
|
cosGamma = cosGamma < 0.0 ? 0.0 : cosGamma;
|
||||||
A = xDC[0][0]*turbidity + xDC[0][1];
|
double cosSTheta = fabs(cos(thetaSun));
|
||||||
B = xDC[1][0]*turbidity + xDC[1][1];
|
|
||||||
C = xDC[2][0]*turbidity + xDC[2][1];
|
double xz = ( 0.00165 * suntheta3 - 0.00375 * suntheta2 + 0.00209 * suntheta + 0.00000) * T2 +
|
||||||
D = xDC[3][0]*turbidity + xDC[3][1];
|
(-0.02903 * suntheta3 + 0.06377 * suntheta2 - 0.03202 * suntheta + 0.00394) * T +
|
||||||
E = xDC[4][0]*turbidity + xDC[4][1];
|
( 0.11693 * suntheta3 - 0.21196 * suntheta2 + 0.06052 * suntheta + 0.25886);
|
||||||
|
|
||||||
/* Sky x */
|
double yz = ( 0.00275 * suntheta3 - 0.00610 * suntheta2 + 0.00317 * suntheta + 0.00000) * T2 +
|
||||||
d = _distribution(A, B, C, D, E, theta, gamma, thetaSun);
|
(-0.04214 * suntheta3 + 0.08970 * suntheta2 - 0.04153 * suntheta + 0.00516) * T +
|
||||||
skycolor.x = Zenith.x * d;
|
( 0.15346 * suntheta3 - 0.26756 * suntheta2 + 0.06670 * suntheta + 0.26688);
|
||||||
|
|
||||||
|
double X = (4.0f / 9.0f - T / 120.0f) * (M_PI - 2.0 * suntheta);
|
||||||
|
double Yz = ((4.0453f * T - 4.9710) * tan(X) - 0.2155 * T + 2.4192) * 1000.0f;
|
||||||
|
|
||||||
/* Zenith y */
|
double val1, val2;
|
||||||
Zenith.y = _chromaticity( yZC, thetaSun, turbidity );
|
|
||||||
A = yDC[0][0]*turbidity + yDC[0][1];
|
val1 = ( 1 + Ax * exp(Bx / cosTheta ) ) * ( 1 + Cx * exp(Dx * gamma) + Ex * sqrt(cosGamma) );
|
||||||
B = yDC[1][0]*turbidity + yDC[1][1];
|
val2 = ( 1 + Ax * exp(Bx) ) * ( 1 + Cx * exp(Dx * suntheta) + Ex * sqrt(cosSTheta) );
|
||||||
C = yDC[2][0]*turbidity + yDC[2][1];
|
double x = xz * val1 / val2;
|
||||||
D = yDC[3][0]*turbidity + yDC[3][1];
|
|
||||||
E = yDC[4][0]*turbidity + yDC[4][1];
|
val1 = ( 1 + Ay * exp(By / cosTheta) ) * ( 1 + Cy * exp(Dy * gamma ) + Ey * sqrt(cosGamma ) );
|
||||||
|
val2 = ( 1 + Ay * exp(By ) ) * ( 1 + Cy * exp(Dy * suntheta) + Ey * sqrt(cosSTheta) );
|
||||||
|
double y = yz * val1 / val2;
|
||||||
|
|
||||||
|
val1 = ( 1 + AY * exp(BY / cosTheta) ) * ( 1 + CY * exp(DY * gamma ) + EY * sqrt(cosGamma) );
|
||||||
|
val2 = ( 1 + AY * exp(BY ) ) * ( 1 + CY * exp(DY * suntheta) + EY * sqrt(cosSTheta) );
|
||||||
|
double Y = Yz * val1 / val2;
|
||||||
|
|
||||||
/* Sky y */
|
return _toColor(x, y, Y);
|
||||||
d = _distribution(A, B, C, D, E, theta, gamma, thetaSun);
|
|
||||||
skycolor.y = Zenith.y * d;
|
|
||||||
|
|
||||||
/* Small hack on luminance value */
|
|
||||||
//skycolor.Y = 1 - exp(-(1.0/25.0) * skycolor.Y);
|
|
||||||
|
|
||||||
return _toColor(skycolor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Color skyGetColor(SkyDefinition* definition, Renderer* renderer, Vector3 eye, Vector3 look)
|
Color skyGetColor(SkyDefinition* definition, Renderer* renderer, Vector3 eye, Vector3 look)
|
||||||
|
@ -333,7 +293,7 @@ Color skyGetColor(SkyDefinition* definition, Renderer* renderer, Vector3 eye, Ve
|
||||||
look = v3Normalize(look);
|
look = v3Normalize(look);
|
||||||
dist = v3Norm(v3Sub(look, sun_position));
|
dist = v3Norm(v3Sub(look, sun_position));
|
||||||
|
|
||||||
sky_color = _preethamApproximate(definition, M_PI/2.0 - asin(look.y), acos(v3Dot(look, sun_position)));
|
sky_color = _preethamApproximate(definition, M_PI/2.0 - asin(look.y), atan2(-look.z, look.x));
|
||||||
if (dist < definition->sun_radius + definition->sun_halo_size)
|
if (dist < definition->sun_radius + definition->sun_halo_size)
|
||||||
{
|
{
|
||||||
sun_color = colorGradationGet(definition->sun_color, definition->daytime);
|
sun_color = colorGradationGet(definition->sun_color, definition->daytime);
|
||||||
|
|
Loading…
Reference in a new issue