Blender V2.61 - r43446

svm_sky.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
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 Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 CCL_NAMESPACE_BEGIN
00020 
00021 __device float3 xyY_to_xyz(float x, float y, float Y)
00022 {
00023     float X, Z;
00024 
00025     if(y != 0.0f) X = (x / y) * Y;
00026     else X = 0.0f;
00027 
00028     if(y != 0.0f && Y != 0.0f) Z = (1.0f - x - y) / y * Y;
00029     else Z = 0.0f;
00030 
00031     return make_float3(X, Y, Z);
00032 }
00033 
00034 __device float3 xyz_to_rgb(float x, float y, float z)
00035 {
00036     return make_float3(3.240479f * x + -1.537150f * y + -0.498535f * z,
00037                       -0.969256f * x +  1.875991f * y +  0.041556f * z,
00038                        0.055648f * x + -0.204043f * y +  1.057311f * z);
00039 }
00040 
00041 /*
00042  * "A Practical Analytic Model for Daylight"
00043  * A. J. Preetham, Peter Shirley, Brian Smits
00044  */
00045 
00046 __device float sky_angle_between(float thetav, float phiv, float theta, float phi)
00047 {
00048     float cospsi = sinf(thetav)*sinf(theta)*cosf(phi - phiv) + cosf(thetav)*cosf(theta);
00049     return safe_acosf(cospsi);
00050 }
00051 
00052 __device float sky_perez_function(__constant float *lam, float theta, float gamma)
00053 {
00054     float ctheta = cosf(theta);
00055     float cgamma = cosf(gamma);
00056 
00057     return (1.0f + lam[0]*expf(lam[1]/ctheta)) * (1.0f + lam[2]*expf(lam[3]*gamma)  + lam[4]*cgamma*cgamma);
00058 }
00059 
00060 __device float3 sky_radiance(KernelGlobals *kg, float3 dir)
00061 {
00062     /* convert vector to spherical coordinates */
00063     float2 spherical = direction_to_spherical(dir);
00064     float theta = spherical.x;
00065     float phi = spherical.y;
00066 
00067     /* angle between sun direction and dir */
00068     float gamma = sky_angle_between(theta, phi, kernel_data.sunsky.theta, kernel_data.sunsky.phi);
00069 
00070     /* clamp theta to horizon */
00071     theta = min(theta, M_PI_2_F - 0.001f);
00072 
00073     /* compute xyY color space values */
00074     float x = kernel_data.sunsky.zenith_x * sky_perez_function(kernel_data.sunsky.perez_x, theta, gamma);
00075     float y = kernel_data.sunsky.zenith_y * sky_perez_function(kernel_data.sunsky.perez_y, theta, gamma);
00076     float Y = kernel_data.sunsky.zenith_Y * sky_perez_function(kernel_data.sunsky.perez_Y, theta, gamma);
00077 
00078     /* convert to RGB */
00079     float3 xyz = xyY_to_xyz(x, y, Y);
00080     return xyz_to_rgb(xyz.x, xyz.y, xyz.z);
00081 }
00082 
00083 __device void svm_node_tex_sky(KernelGlobals *kg, ShaderData *sd, float *stack, uint dir_offset, uint out_offset)
00084 {
00085     float3 dir = stack_load_float3(stack, dir_offset);
00086     float3 f = sky_radiance(kg, dir);
00087 
00088     stack_store_float3(stack, out_offset, f);
00089 }
00090 
00091 CCL_NAMESPACE_END
00092