Blender V2.61 - r43446

bsdf_ashikhmin_velvet.h

Go to the documentation of this file.
00001 /*
00002  * Adapted from Open Shading Language with this license:
00003  *
00004  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
00005  * All Rights Reserved.
00006  *
00007  * Modifications Copyright 2011, Blender Foundation.
00008  * 
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are
00011  * met:
00012  * * Redistributions of source code must retain the above copyright
00013  *   notice, this list of conditions and the following disclaimer.
00014  * * Redistributions in binary form must reproduce the above copyright
00015  *   notice, this list of conditions and the following disclaimer in the
00016  *   documentation and/or other materials provided with the distribution.
00017  * * Neither the name of Sony Pictures Imageworks nor the names of its
00018  *   contributors may be used to endorse or promote products derived from
00019  *   this software without specific prior written permission.
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00023  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00024  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 */
00032 
00033 #ifndef __BSDF_ASHIKHMIN_VELVET_H__
00034 #define __BSDF_ASHIKHMIN_VELVET_H__
00035 
00036 CCL_NAMESPACE_BEGIN
00037 
00038 typedef struct BsdfAshikhminVelvetClosure {
00039     //float3 m_N;
00040     float m_invsigma2;
00041 } BsdfAshikhminVelvetClosure;
00042 
00043 __device void bsdf_ashikhmin_velvet_setup(ShaderData *sd, ShaderClosure *sc, float sigma)
00044 {
00045     sigma = fmaxf(sigma, 0.01f);
00046 
00047     float m_invsigma2 = 1.0f/(sigma * sigma);
00048 
00049     sc->type = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
00050     sd->flag |= SD_BSDF|SD_BSDF_HAS_EVAL;
00051     sc->data0 = m_invsigma2;
00052 }
00053 
00054 __device void bsdf_ashikhmin_velvet_blur(ShaderClosure *sc, float roughness)
00055 {
00056 }
00057 
00058 __device float3 bsdf_ashikhmin_velvet_eval_reflect(const ShaderData *sd, const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
00059 {
00060     float m_invsigma2 = sc->data0;
00061     float3 m_N = sd->N;
00062 
00063     float cosNO = dot(m_N, I);
00064     float cosNI = dot(m_N, omega_in);
00065     if(cosNO > 0 && cosNI > 0) {
00066         float3 H = normalize(omega_in + I);
00067 
00068         float cosNH = dot(m_N, H);
00069         float cosHO = fabsf(dot(I, H));
00070 
00071         if(!(fabsf(cosNH) < 1.0f-1e-5f && cosHO > 1e-5f))
00072             return make_float3(0, 0, 0);
00073 
00074         float cosNHdivHO = cosNH / cosHO;
00075         cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
00076 
00077         float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
00078         float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
00079 
00080         float sinNH2 = 1 - cosNH * cosNH;
00081         float sinNH4 = sinNH2 * sinNH2;
00082         float cotangent2 = (cosNH * cosNH) / sinNH2;
00083 
00084         float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
00085         float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
00086 
00087         float out = 0.25f * (D * G) / cosNO;
00088 
00089         *pdf = 0.5f * M_1_PI_F;
00090         return make_float3(out, out, out);
00091     }
00092 
00093     return make_float3(0, 0, 0);
00094 }
00095 
00096 __device float3 bsdf_ashikhmin_velvet_eval_transmit(const ShaderData *sd, const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
00097 {
00098     return make_float3(0.0f, 0.0f, 0.0f);
00099 }
00100 
00101 __device float bsdf_ashikhmin_velvet_albedo(const ShaderData *sd, const ShaderClosure *sc, const float3 I)
00102 {
00103     return 1.0f;
00104 }
00105 
00106 __device int bsdf_ashikhmin_velvet_sample(const ShaderData *sd, const ShaderClosure *sc, float randu, float randv, float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
00107 {
00108     float m_invsigma2 = sc->data0;
00109     float3 m_N = sd->N;
00110 
00111     // we are viewing the surface from above - send a ray out with uniform
00112     // distribution over the hemisphere
00113     sample_uniform_hemisphere(m_N, randu, randv, omega_in, pdf);
00114 
00115     if(dot(sd->Ng, *omega_in) > 0) {
00116         float3 H = normalize(*omega_in + sd->I);
00117 
00118         float cosNI = dot(m_N, *omega_in);
00119         float cosNO = dot(m_N, sd->I);
00120         float cosNH = dot(m_N, H);
00121         float cosHO = fabsf(dot(sd->I, H));
00122 
00123         if(fabsf(cosNO) > 1e-5f && fabsf(cosNH) < 1.0f-1e-5f && cosHO > 1e-5f) {
00124             float cosNHdivHO = cosNH / cosHO;
00125             cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
00126 
00127             float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
00128             float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
00129 
00130             float sinNH2 = 1 - cosNH * cosNH;
00131             float sinNH4 = sinNH2 * sinNH2;
00132             float cotangent2 =  (cosNH * cosNH) / sinNH2;
00133 
00134             float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
00135             float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
00136 
00137             float power = 0.25f * (D * G) / cosNO;
00138 
00139             *eval = make_float3(power, power, power);
00140 
00141 #ifdef __RAY_DIFFERENTIALS__
00142             // TODO: find a better approximation for the retroreflective bounce
00143             *domega_in_dx = (2 * dot(m_N, sd->dI.dx)) * m_N - sd->dI.dx;
00144             *domega_in_dy = (2 * dot(m_N, sd->dI.dy)) * m_N - sd->dI.dy;
00145             *domega_in_dx *= 125.0f;
00146             *domega_in_dy *= 125.0f;
00147 #endif
00148         }
00149         else
00150             *pdf = 0.0f;
00151     }
00152     else
00153         *pdf = 0.0f;
00154 
00155     return LABEL_REFLECT|LABEL_DIFFUSE;
00156 }
00157 
00158 CCL_NAMESPACE_END
00159 
00160 #endif /* __BSDF_ASHIKHMIN_VELVET_H__ */
00161