~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to intern/cycles/kernel/osl/nodes/node_sky_texture.osl

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2011, Blender Foundation.
 
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 Foundation,
 
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "stdosl.h"
 
20
 
 
21
struct KernelSunSky {
 
22
        /* sun direction in spherical and cartesian */
 
23
        float theta, phi;
 
24
        vector dir;
 
25
 
 
26
        /* perez function parameters */
 
27
        float zenith_Y, zenith_x, zenith_y;
 
28
        float perez_Y[5], perez_x[5], perez_y[5];
 
29
};
 
30
 
 
31
color xyY_to_xyz(float x, float y, float Y)
 
32
{
 
33
        float X, Z;
 
34
 
 
35
        if(y != 0.0) X = (x / y) * Y;
 
36
        else X = 0.0;
 
37
 
 
38
        if(y != 0.0 && Y != 0.0) Z = ((1.0 - x - y) / y) * Y;
 
39
        else Z = 0.0;
 
40
 
 
41
        return color(X, Y, Z);
 
42
}
 
43
 
 
44
color xyz_to_rgb(float x, float y, float z)
 
45
{
 
46
        return color(3.240479 * x + -1.537150 * y + -0.498535 * z,
 
47
                                 -0.969256 * x + 1.875991 * y + 0.041556 * z,
 
48
                                 0.055648 * x + -0.204043 * y + 1.057311 * z);
 
49
}
 
50
 
 
51
float sky_angle_between(float thetav, float phiv, float theta, float phi)
 
52
{
 
53
        float cospsi = sin(thetav)*sin(theta)*cos(phi - phiv) + cos(thetav)*cos(theta);
 
54
 
 
55
        if(cospsi > 1.0)
 
56
                return 0.0;
 
57
        if(cospsi < -1.0)
 
58
                return M_PI;
 
59
 
 
60
        return acos(cospsi);
 
61
}
 
62
 
 
63
vector sky_spherical_coordinates(vector dir)
 
64
{
 
65
        return vector(acos(dir[2]), atan2(dir[0], dir[1]), 0);
 
66
}
 
67
 
 
68
float sky_perez_function(float lam[5], float theta, float gamma)
 
69
{
 
70
        float ctheta = cos(theta);
 
71
        float cgamma = cos(gamma);
 
72
 
 
73
        return (1.0 + lam[0]*exp(lam[1] / ctheta)) * (1.0 + lam[2]*exp(lam[3]*gamma) + lam[4]*cgamma*cgamma);
 
74
}
 
75
 
 
76
color sky_xyz_radiance(KernelSunSky sunsky, vector dir)
 
77
{
 
78
        /* convert vector to spherical coordinates */
 
79
        vector spherical = sky_spherical_coordinates(dir);
 
80
        float theta = spherical[0];
 
81
        float phi = spherical[1];
 
82
 
 
83
        /* angle between sun direction and dir */
 
84
        float gamma = sky_angle_between(theta, phi, sunsky.theta, sunsky.phi);
 
85
 
 
86
        /* clamp theta to horizon */
 
87
        theta = min(theta, M_PI_2 - 0.001);
 
88
 
 
89
        /* compute xyY color space values */
 
90
        float x = sunsky.zenith_x * sky_perez_function(sunsky.perez_x, theta, gamma);
 
91
        float y = sunsky.zenith_y * sky_perez_function(sunsky.perez_y, theta, gamma);
 
92
        float Y = sunsky.zenith_Y * sky_perez_function(sunsky.perez_Y, theta, gamma);
 
93
 
 
94
        /* convert to RGB */
 
95
        color xyz = xyY_to_xyz(x, y, Y);
 
96
        return xyz_to_rgb(xyz[0], xyz[1], xyz[2]);
 
97
}
 
98
 
 
99
void precompute_sunsky(vector dir, float turbidity, output KernelSunSky sunsky)
 
100
{
 
101
        vector spherical = sky_spherical_coordinates(dir);
 
102
        float theta = spherical[0];
 
103
        float phi = spherical[1];
 
104
 
 
105
        sunsky.theta = theta;
 
106
        sunsky.phi = phi;
 
107
        sunsky.dir = dir;
 
108
 
 
109
        float theta2 = theta*theta;
 
110
        float theta3 = theta*theta*theta;
 
111
        float T = turbidity;
 
112
        float T2 = T*T;
 
113
 
 
114
        float chi = (4.0/ 9.0- T / 120.0) * (M_PI - 2.0* theta);
 
115
        sunsky.zenith_Y = (4.0453*T - 4.9710) * tan(chi) - 0.2155*T + 2.4192;
 
116
        sunsky.zenith_Y *= 0.06;
 
117
 
 
118
        sunsky.zenith_x =
 
119
        (0.00166* theta3 - 0.00375* theta2 + 0.00209* theta)*T2 +
 
120
        (-0.02903* theta3 + 0.06377* theta2 - 0.03202* theta + 0.00394)*T +
 
121
        (0.11693* theta3 - 0.21196* theta2 + 0.06052* theta + 0.25886);
 
122
 
 
123
        sunsky.zenith_y =
 
124
        (0.00275* theta3 - 0.00610* theta2 + 0.00317* theta)*T2 +
 
125
        (-0.04214* theta3 + 0.08970* theta2 - 0.04153* theta + 0.00516)*T +
 
126
        (0.15346* theta3 - 0.26756* theta2 + 0.06670* theta + 0.26688);
 
127
 
 
128
        sunsky.perez_Y[0] = (0.1787*T - 1.4630);
 
129
        sunsky.perez_Y[1] = (-0.3554*T + 0.4275);
 
130
        sunsky.perez_Y[2] = (-0.0227*T + 5.3251);
 
131
        sunsky.perez_Y[3] = (0.1206*T - 2.5771);
 
132
        sunsky.perez_Y[4] = (-0.0670*T + 0.3703);
 
133
 
 
134
        sunsky.perez_x[0] = (-0.0193*T - 0.2592);
 
135
        sunsky.perez_x[1] = (-0.0665*T + 0.0008);
 
136
        sunsky.perez_x[2] = (-0.0004*T + 0.2125);
 
137
        sunsky.perez_x[3] = (-0.0641*T - 0.8989);
 
138
        sunsky.perez_x[4] = (-0.0033*T + 0.0452);
 
139
 
 
140
        sunsky.perez_y[0] = (-0.0167*T - 0.2608);
 
141
        sunsky.perez_y[1] = (-0.0950*T + 0.0092);
 
142
        sunsky.perez_y[2] = (-0.0079*T + 0.2102);
 
143
        sunsky.perez_y[3] = (-0.0441*T - 1.6537);
 
144
        sunsky.perez_y[4] = (-0.0109*T + 0.0529);
 
145
 
 
146
        sunsky.zenith_Y /= sky_perez_function(sunsky.perez_Y, 0, theta);
 
147
        sunsky.zenith_x /= sky_perez_function(sunsky.perez_x, 0, theta);
 
148
        sunsky.zenith_y /= sky_perez_function(sunsky.perez_y, 0, theta);
 
149
}
 
150
 
 
151
shader node_sky_texture(
 
152
        vector Vector = P,
 
153
        vector sun_direction = vector(0, 0, 1),
 
154
        float turbidity = 2.2,
 
155
        output color Color = color(0.0, 0.0, 0.0))
 
156
{
 
157
        KernelSunSky sunsky;
 
158
 
 
159
        precompute_sunsky(sun_direction, turbidity, sunsky);
 
160
        Color = sky_xyz_radiance(sunsky, Vector);
 
161
}
 
162