Blender V2.61 - r43446

stdosl.h

Go to the documentation of this file.
00001 
00002 // Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.  All Rights Reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 // * Redistributions of source code must retain the above copyright
00008 //   notice, this list of conditions and the following disclaimer.
00009 // * Redistributions in binary form must reproduce the above copyright
00010 //   notice, this list of conditions and the following disclaimer in the
00011 //   documentation and/or other materials provided with the distribution.
00012 // * Neither the name of Sony Pictures Imageworks nor the names of its
00013 //   contributors may be used to endorse or promote products derived from
00014 //   this software without specific prior written permission.
00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00016 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00017 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00018 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00019 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00022 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00023 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 
00028 
00029 #ifndef CCL_STDOSL_H
00030 #define CCL_STDOSL_H
00031 
00032 
00033 #ifndef M_PI
00034 #define M_PI       3.1415926535897932        /* pi */
00035 #define M_PI_2     1.5707963267948966        /* pi/2 */
00036 #define M_PI_4     0.7853981633974483        /* pi/4 */
00037 #define M_2_PI     0.6366197723675813        /* 2/pi */
00038 #define M_2_SQRTPI 1.1283791670955126        /* 2/sqrt(pi) */
00039 #define M_E        2.7182818284590452        /* e (Euler's number) */
00040 #define M_LN2      0.6931471805599453        /* ln(2) */
00041 #define M_LN10     2.3025850929940457        /* ln(10) */
00042 #define M_LOG2E    1.4426950408889634        /* log_2(e) */
00043 #define M_LOG10E   0.4342944819032518        /* log_10(e) */
00044 #define M_SQRT2    1.4142135623730950        /* sqrt(2) */
00045 #define M_SQRT1_2  0.7071067811865475        /* 1/sqrt(2) */
00046 #endif
00047 
00048 
00049 
00050 // Declaration of built-in functions and closures
00051 #define BUILTIN [[ int builtin = 1 ]]
00052 #define BUILTIN_DERIV [[ int builtin = 1, int deriv = 1 ]]
00053 
00054 #define PERCOMP1(name)                          \
00055     normal name (normal x) BUILTIN;             \
00056     vector name (vector x) BUILTIN;             \
00057     point  name (point x) BUILTIN;              \
00058     color  name (color x) BUILTIN;              \
00059     float  name (float x) BUILTIN;
00060 
00061 #define PERCOMP2(name)                          \
00062     normal name (normal x, normal y) BUILTIN;   \
00063     vector name (vector x, vector y) BUILTIN;   \
00064     point  name (point x, point y) BUILTIN;     \
00065     color  name (color x, color y) BUILTIN;     \
00066     float  name (float x, float y) BUILTIN;
00067 
00068 #define PERCOMP2F(name)                         \
00069     normal name (normal x, float y) BUILTIN;    \
00070     vector name (vector x, float y) BUILTIN;    \
00071     point  name (point x, float y) BUILTIN;     \
00072     color  name (color x, float y) BUILTIN;     \
00073     float  name (float x, float y) BUILTIN;
00074 
00075 
00076 // Basic math
00077 normal degrees (normal x) { return x*(180.0/M_PI); }
00078 vector degrees (vector x) { return x*(180.0/M_PI); }
00079 point  degrees (point x)  { return x*(180.0/M_PI); }
00080 color  degrees (color x)  { return x*(180.0/M_PI); }
00081 float  degrees (float x)  { return x*(180.0/M_PI); }
00082 normal radians (normal x) { return x*(M_PI/180.0); }
00083 vector radians (vector x) { return x*(M_PI/180.0); }
00084 point  radians (point x)  { return x*(M_PI/180.0); }
00085 color  radians (color x)  { return x*(M_PI/180.0); }
00086 float  radians (float x)  { return x*(M_PI/180.0); }
00087 PERCOMP1 (cos)
00088 PERCOMP1 (sin)
00089 PERCOMP1 (tan)
00090 PERCOMP1 (acos)
00091 PERCOMP1 (asin)
00092 PERCOMP1 (atan)
00093 PERCOMP2 (atan2)
00094 PERCOMP1 (cosh)
00095 PERCOMP1 (sinh)
00096 PERCOMP1 (tanh)
00097 PERCOMP2F (pow)
00098 PERCOMP1 (exp)
00099 PERCOMP1 (exp2)
00100 PERCOMP1 (expm1)
00101 PERCOMP1 (log)
00102 point  log (point a,  float b) { return log(a)/log(b); }
00103 vector log (vector a, float b) { return log(a)/log(b); }
00104 color  log (color a,  float b) { return log(a)/log(b); }
00105 float  log (float a,  float b) { return log(a)/log(b); }
00106 PERCOMP1 (log2)
00107 PERCOMP1 (log10)
00108 PERCOMP1 (logb)
00109 PERCOMP1 (sqrt)
00110 PERCOMP1 (inversesqrt)
00111 float hypot (float a, float b) { return sqrt (a*a + b*b); }
00112 float hypot (float a, float b, float c) { return sqrt (a*a + b*b + c*c); }
00113 PERCOMP1 (abs)
00114 int abs (int x) BUILTIN;
00115 PERCOMP1 (fabs)
00116 int fabs (int x) BUILTIN;
00117 PERCOMP1 (sign)
00118 PERCOMP1 (floor)
00119 PERCOMP1 (ceil)
00120 PERCOMP1 (round)
00121 PERCOMP1 (trunc)
00122 PERCOMP2 (fmod)
00123 PERCOMP2F (fmod)
00124 PERCOMP2 (mod)
00125 PERCOMP2F (mod)
00126 int    mod (int x, int y) BUILTIN;
00127 PERCOMP2 (min)
00128 PERCOMP2 (max)
00129 normal clamp (normal x, normal minval, normal maxval) { return max(min(x,maxval),minval); }
00130 vector clamp (vector x, vector minval, vector maxval) { return max(min(x,maxval),minval); }
00131 point  clamp (point x, point minval, point maxval) { return max(min(x,maxval),minval); }
00132 color  clamp (color x, color minval, color maxval) { return max(min(x,maxval),minval); }
00133 float  clamp (float x, float minval, float maxval) { return max(min(x,maxval),minval); }
00134 //normal clamp (normal x, normal minval, normal maxval) BUILTIN;
00135 //vector clamp (vector x, vector minval, vector maxval) BUILTIN;
00136 //point  clamp (point x, point minval, point maxval) BUILTIN;
00137 //color  clamp (color x, color minval, color maxval) BUILTIN;
00138 //float  clamp (float x, float minval, float maxval) BUILTIN;
00139 normal mix (normal x, normal y, normal a) { return x*(1-a) + y*a; }
00140 normal mix (normal x, normal y, float  a) { return x*(1-a) + y*a; }
00141 vector mix (vector x, vector y, vector a) { return x*(1-a) + y*a; }
00142 vector mix (vector x, vector y, float  a) { return x*(1-a) + y*a; }
00143 point  mix (point  x, point  y, point  a) { return x*(1-a) + y*a; }
00144 point  mix (point  x, point  y, float  a) { return x*(1-a) + y*a; }
00145 color  mix (color  x, color  y, color  a) { return x*(1-a) + y*a; }
00146 color  mix (color  x, color  y, float  a) { return x*(1-a) + y*a; }
00147 float  mix (float  x, float  y, float  a) { return x*(1-a) + y*a; }
00148 int isnan (float x) BUILTIN;
00149 int isinf (float x) BUILTIN;
00150 int isfinite (float x) BUILTIN;
00151 float erf (float x) BUILTIN;
00152 float erfc (float x) BUILTIN;
00153 
00154 // Vector functions
00155 
00156 vector cross (vector a, vector b) BUILTIN;
00157 float dot (vector a, vector b) BUILTIN;
00158 float length (vector v) BUILTIN;
00159 float distance (point a, point b) BUILTIN;
00160 float distance (point a, point b, point q) BUILTIN;
00161 normal normalize (normal v) BUILTIN;
00162 vector normalize (vector v) BUILTIN;
00163 vector faceforward (vector N, vector I, vector Nref) BUILTIN;
00164 vector faceforward (vector N, vector I) BUILTIN;
00165 vector reflect (vector I, vector N) { return I - 2*dot(N,I)*N; }
00166 vector refract (vector I, vector N, float eta) {
00167     float IdotN = dot (I, N);
00168     float k = 1 - eta*eta * (1 - IdotN*IdotN);
00169     return (k < 0) ? vector(0,0,0) : (eta*I - N * (eta*IdotN + sqrt(k)));
00170 }
00171 void fresnel (vector I, normal N, float eta,
00172               output float Kr, output float Kt,
00173               output vector R, output vector T)
00174 {
00175     float sqr(float x) { return x*x; }
00176     float c = dot(I, N);
00177     if (c < 0)
00178         c = -c;
00179     R = reflect(I, N);
00180     float g = 1.0 / sqr(eta) - 1.0 + c * c;
00181     if (g >= 0.0) {
00182         g = sqrt (g);
00183         float beta = g - c;
00184         float F = (c * (g+c) - 1.0) / (c * beta + 1.0);
00185         F = 0.5 * (1.0 + sqr(F));
00186         F *= sqr (beta / (g+c));
00187         Kr = F;
00188         Kt = (1.0 - Kr) * eta*eta;
00189         // OPT: the following recomputes some of the above values, but it 
00190         // gives us the same result as if the shader-writer called refract()
00191         T = refract(I, N, eta);
00192     } else {
00193         // total internal reflection
00194         Kr = 1.0;
00195         Kt = 0.0;
00196         T = vector (0,0,0);
00197     }
00198 #undef sqr
00199 }
00200 
00201 void fresnel (vector I, normal N, float eta,
00202               output float Kr, output float Kt)
00203 {
00204     vector R, T;
00205     fresnel(I, N, eta, Kr, Kt, R, T);
00206 }
00207 
00208 point rotate (point q, float angle, point a, point b) BUILTIN;
00209 
00210 normal transform (matrix Mto, normal p) BUILTIN;
00211 vector transform (matrix Mto, vector p) BUILTIN;
00212 point transform (matrix Mto, point p) BUILTIN;
00213 
00214 // Implementation of transform-with-named-space in terms of matrices:
00215 
00216 point transform (string tospace, point x)
00217 {
00218     return transform (matrix ("common", tospace), x);
00219 }
00220 
00221 point transform (string fromspace, string tospace, point x)
00222 {
00223     return transform (matrix (fromspace, tospace), x);
00224 }
00225 
00226 
00227 vector transform (string tospace, vector x)
00228 {
00229     return transform (matrix ("common", tospace), x);
00230 }
00231 
00232 vector transform (string fromspace, string tospace, vector x)
00233 {
00234     return transform (matrix (fromspace, tospace), x);
00235 }
00236 
00237 
00238 normal transform (string tospace, normal x)
00239 {
00240     return transform (matrix ("common", tospace), x);
00241 }
00242 
00243 normal transform (string fromspace, string tospace, normal x)
00244 {
00245     return transform (matrix (fromspace, tospace), x);
00246 }
00247 
00248 float transformu (string tounits, float x) BUILTIN;
00249 float transformu (string fromunits, string tounits, float x) BUILTIN;
00250 
00251 
00252 // Color functions
00253 
00254 float luminance (color c) {
00255     return dot ((vector)c, vector(0.2126, 0.7152, 0.0722));
00256 }
00257 
00258 
00259 
00260 color transformc (string to, color x)
00261 {
00262     color rgb_to_hsv (color rgb) {  // See Foley & van Dam
00263         float r = rgb[0], g = rgb[1], b = rgb[2];
00264         float mincomp = min (r, min (g, b));
00265         float maxcomp = max (r, max (g, b));
00266         float delta = maxcomp - mincomp;  // chroma
00267         float h, s, v;
00268         v = maxcomp;
00269         if (maxcomp > 0)
00270             s = delta / maxcomp;
00271         else s = 0;
00272         if (s <= 0)
00273             h = 0;
00274         else {
00275             if      (r >= maxcomp) h = (g-b) / delta;
00276             else if (g >= maxcomp) h = 2 + (b-r) / delta;
00277             else                   h = 4 + (r-g) / delta;
00278             h /= 6;
00279             if (h < 0)
00280                 h += 1;
00281         }
00282         return color (h, s, v);
00283     }
00284 
00285     color rgb_to_hsl (color rgb) {  // See Foley & van Dam
00286         // First convert rgb to hsv, then to hsl
00287         float minval = min (rgb[0], min (rgb[1], rgb[2]));
00288         color hsv = rgb_to_hsv (rgb);
00289         float maxval = hsv[2];   // v == maxval
00290         float h = hsv[0], s, l = (minval+maxval) / 2;
00291         if (minval == maxval)
00292             s = 0;  // special 'achromatic' case, hue is 0
00293         else if (l <= 0.5)
00294             s = (maxval - minval) / (maxval + minval);
00295         else
00296             s = (maxval - minval) / (2 - maxval - minval);
00297         return color (h, s, l);
00298     }
00299 
00300     color r;
00301     if (to == "rgb" || to == "RGB")
00302         r = x;
00303     else if (to == "hsv")
00304         r = rgb_to_hsv (x);
00305     else if (to == "hsl")
00306         r = rgb_to_hsl (x);
00307     else if (to == "YIQ")
00308         r = color (dot (vector(0.299,  0.587,  0.114), (vector)x),
00309                    dot (vector(0.596, -0.275, -0.321), (vector)x),
00310                    dot (vector(0.212, -0.523,  0.311), (vector)x));
00311     else if (to == "xyz")
00312         r = color (dot (vector(0.412453, 0.357580, 0.180423), (vector)x),
00313                    dot (vector(0.212671, 0.715160, 0.072169), (vector)x),
00314                    dot (vector(0.019334, 0.119193, 0.950227), (vector)x));
00315     else {
00316         error ("Unknown color space \"%s\"", to);
00317         r = x;
00318     }
00319     return r;
00320 }
00321 
00322 
00323 color transformc (string from, string to, color x)
00324 {
00325     color hsv_to_rgb (color c) { // Reference: Foley & van Dam
00326         float h = c[0], s = c[1], v = c[2];
00327         color r;
00328         if (s < 0.0001) {
00329             r = v;
00330         } else {
00331             h = 6 * (h - floor(h));  // expand to [0..6)
00332             int hi = (int)h;
00333             float f = h - hi;
00334             float p = v * (1-s);
00335             float q = v * (1-s*f);
00336             float t = v * (1-s*(1-f));
00337             if      (hi == 0) r = color (v, t, p);
00338             else if (hi == 1) r = color (q, v, p);
00339             else if (hi == 2) r = color (p, v, t);
00340             else if (hi == 3) r = color (p, q, v);
00341             else if (hi == 4) r = color (t, p, v);
00342             else              r = color (v, p, q);
00343         }
00344         return r;
00345     }
00346 
00347     color hsl_to_rgb (color c) {
00348         float h = c[0], s = c[1], l = c[2];
00349         // Easiest to convert hsl -> hsv, then hsv -> RGB (per Foley & van Dam)
00350         float v = (l <= 0.5) ? (l * (1 + s)) : (l * (1 - s) + s);
00351         color r;
00352         if (v <= 0) {
00353             r = 0;
00354         } else {
00355             float min = 2 * l - v;
00356             s = (v - min) / v;
00357             r = hsv_to_rgb (color (h, s, v));
00358         }
00359         return r;
00360     }
00361 
00362     color r;
00363     if (from == "rgb" || from == "RGB")
00364         r = x;
00365     else if (from == "hsv")
00366         r = hsv_to_rgb (x);
00367     else if (from == "hsl")
00368         r = hsl_to_rgb (x);
00369     else if (from == "YIQ")
00370         r = color (dot (vector(1,  0.9557,  0.6199), (vector)x),
00371                    dot (vector(1, -0.2716, -0.6469), (vector)x),
00372                    dot (vector(1, -1.1082,  1.7051), (vector)x));
00373     else if (from == "xyz")
00374         r = color (dot (vector( 3.240479, -1.537150, -0.498535), (vector)x),
00375                    dot (vector(-0.969256,  1.875991,  0.041556), (vector)x),
00376                    dot (vector( 0.055648, -0.204043,  1.057311), (vector)x));
00377     else {
00378         error ("Unknown color space \"%s\"", to);
00379         r = x;
00380     }
00381     return transformc (to, r);
00382 }
00383 
00384  
00385 
00386 // Matrix functions
00387 
00388 float determinant (matrix m) BUILTIN;
00389 matrix transpose (matrix m) BUILTIN;
00390 
00391 
00392 
00393 // Pattern generation
00394 
00395 float step (float edge, float x) BUILTIN;
00396 color step (color edge, color x) BUILTIN;
00397 point step (point edge, point x) BUILTIN;
00398 vector step (vector edge, vector x) BUILTIN;
00399 normal step (normal edge, normal x) BUILTIN;
00400 float smoothstep (float edge0, float edge1, float x) BUILTIN;
00401 
00402 
00403 // Derivatives and area operators
00404 
00405 
00406 // Displacement functions
00407 
00408 
00409 // String functions
00410 
00411 int strlen (string s) BUILTIN;
00412 int startswith (string s, string prefix) BUILTIN;
00413 int endswith (string s, string suffix) BUILTIN;
00414 string substr (string s, int start, int len) BUILTIN;
00415 string substr (string s, int start) { return substr (s, start, strlen(s)); }
00416 
00417 // Define concat in terms of shorter concat
00418 string concat (string a, string b, string c) {
00419     return concat(concat(a,b), c);
00420 }
00421 string concat (string a, string b, string c, string d) {
00422     return concat(concat(a,b,c), d);
00423 }
00424 string concat (string a, string b, string c, string d, string e) {
00425     return concat(concat(a,b,c,d), e);
00426 }
00427 string concat (string a, string b, string c, string d, string e, string f) {
00428     return concat(concat(a,b,c,d,e), f);
00429 }
00430 
00431 
00432 // Texture
00433 
00434 
00435 // Closures
00436 
00437 closure color diffuse(normal N) BUILTIN;
00438 closure color oren_nayar(normal N, float sigma) BUILTIN;
00439 closure color translucent(normal N) BUILTIN;
00440 closure color reflection(normal N, float eta) BUILTIN;
00441 closure color reflection(normal N) { return reflection (N, 0.0); }
00442 closure color refraction(normal N, float eta) BUILTIN;
00443 closure color dielectric(normal N, float eta) BUILTIN;
00444 closure color transparent() BUILTIN;
00445 closure color microfacet_ggx(normal N, float ag) BUILTIN;
00446 closure color microfacet_ggx_refraction(normal N, float ag, float eta) BUILTIN;
00447 closure color microfacet_beckmann(normal N, float ab) BUILTIN;
00448 closure color microfacet_beckmann_refraction(normal N, float ab, float eta) BUILTIN;
00449 closure color ward(normal N, vector T,float ax, float ay) BUILTIN;
00450 closure color ashikhmin_velvet(normal N, float sigma) BUILTIN;
00451 closure color westin_backscatter(normal N, float roughness) BUILTIN;
00452 closure color westin_sheen(normal N, float edginess) BUILTIN;
00453 closure color bssrdf_cubic(color radius) BUILTIN;
00454 closure color emission(float inner_angle, float outer_angle) BUILTIN;
00455 closure color emission(float outer_angle) BUILTIN;
00456 closure color emission() BUILTIN;
00457 closure color debug(string tag) BUILTIN;
00458 closure color background() BUILTIN;
00459 closure color holdout() BUILTIN;
00460 closure color subsurface(float eta, float g, float mfp, float albedo) BUILTIN;
00461 
00462 // Renderer state
00463 int raytype (string typename) BUILTIN;
00464 
00465 #undef BUILTIN
00466 #undef BUILTIN_DERIV
00467 #undef PERCOMP1
00468 #undef PERCOMP2
00469 #undef PERCOMP2F
00470 
00471 #endif /* CCL_STDOSL_H */
00472