Blender V2.61 - r43446

bsdf_diffuse.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_DIFFUSE_H__
00034 #define __BSDF_DIFFUSE_H__
00035 
00036 CCL_NAMESPACE_BEGIN
00037 
00038 /* DIFFUSE */
00039 
00040 typedef struct BsdfDiffuseClosure {
00041     //float3 m_N;
00042 } BsdfDiffuseClosure;
00043 
00044 __device void bsdf_diffuse_setup(ShaderData *sd, ShaderClosure *sc)
00045 {
00046     sc->type = CLOSURE_BSDF_DIFFUSE_ID;
00047     sd->flag |= SD_BSDF|SD_BSDF_HAS_EVAL;
00048 }
00049 
00050 __device void bsdf_diffuse_blur(ShaderClosure *sc, float roughness)
00051 {
00052 }
00053 
00054 __device float3 bsdf_diffuse_eval_reflect(const ShaderData *sd, const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
00055 {
00056     float3 m_N = sd->N;
00057 
00058     float cos_pi = fmaxf(dot(m_N, omega_in), 0.0f) * M_1_PI_F;
00059     *pdf = cos_pi;
00060     return make_float3(cos_pi, cos_pi, cos_pi);
00061 }
00062 
00063 __device float3 bsdf_diffuse_eval_transmit(const ShaderData *sd, const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
00064 {
00065     return make_float3(0.0f, 0.0f, 0.0f);
00066 }
00067 
00068 __device float bsdf_diffuse_albedo(const ShaderData *sd, const ShaderClosure *sc, const float3 I)
00069 {
00070     return 1.0f;
00071 }
00072 
00073 __device int bsdf_diffuse_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)
00074 {
00075     float3 m_N = sd->N;
00076 
00077     // distribution over the hemisphere
00078     sample_cos_hemisphere(m_N, randu, randv, omega_in, pdf);
00079 
00080     if(dot(sd->Ng, *omega_in) > 0.0f) {
00081         *eval = make_float3(*pdf, *pdf, *pdf);
00082 #ifdef __RAY_DIFFERENTIALS__
00083         // TODO: find a better approximation for the diffuse bounce
00084         *domega_in_dx = (2 * dot(m_N, sd->dI.dx)) * m_N - sd->dI.dx;
00085         *domega_in_dy = (2 * dot(m_N, sd->dI.dy)) * m_N - sd->dI.dy;
00086         *domega_in_dx *= 125.0f;
00087         *domega_in_dy *= 125.0f;
00088 #endif
00089     }
00090     else
00091         *pdf = 0.0f;
00092     
00093     return LABEL_REFLECT|LABEL_DIFFUSE;
00094 }
00095 
00096 /* TRANSLUCENT */
00097 
00098 typedef struct BsdfTranslucentClosure {
00099     //float3 m_N;
00100 } BsdfTranslucentClosure;
00101 
00102 __device void bsdf_translucent_setup(ShaderData *sd, ShaderClosure *sc)
00103 {
00104     sc->type = CLOSURE_BSDF_TRANSLUCENT_ID;
00105     sd->flag |= SD_BSDF|SD_BSDF_HAS_EVAL;
00106 }
00107 
00108 __device void bsdf_translucent_blur(ShaderClosure *sc, float roughness)
00109 {
00110 }
00111 
00112 __device float3 bsdf_translucent_eval_reflect(const ShaderData *sd, const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
00113 {
00114     return make_float3(0.0f, 0.0f, 0.0f);
00115 }
00116 
00117 __device float3 bsdf_translucent_eval_transmit(const ShaderData *sd, const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
00118 {
00119     float3 m_N = sd->N;
00120 
00121     float cos_pi = fmaxf(-dot(m_N, omega_in), 0.0f) * M_1_PI_F;
00122     *pdf = cos_pi;
00123     return make_float3 (cos_pi, cos_pi, cos_pi);
00124 }
00125 
00126 __device float bsdf_translucent_albedo(const ShaderData *sd, const ShaderClosure *sc, const float3 I)
00127 {
00128     return 1.0f;
00129 }
00130 
00131 __device int bsdf_translucent_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)
00132 {
00133     float3 m_N = sd->N;
00134 
00135     // we are viewing the surface from the right side - send a ray out with cosine
00136     // distribution over the hemisphere
00137     sample_cos_hemisphere (-m_N, randu, randv, omega_in, pdf);
00138     if(dot(sd->Ng, *omega_in) < 0) {
00139         *eval = make_float3(*pdf, *pdf, *pdf);
00140 #ifdef __RAY_DIFFERENTIALS__
00141         // TODO: find a better approximation for the diffuse bounce
00142         *domega_in_dx = (2 * dot(m_N, sd->dI.dx)) * m_N - sd->dI.dx;
00143         *domega_in_dy = (2 * dot(m_N, sd->dI.dy)) * m_N - sd->dI.dy;
00144         *domega_in_dx *= -125.0f;
00145         *domega_in_dy *= -125.0f;
00146 #endif
00147     } else
00148         *pdf = 0;
00149 
00150     return LABEL_TRANSMIT|LABEL_DIFFUSE;
00151 }
00152 
00153 CCL_NAMESPACE_END
00154 
00155 #endif /* __BSDF_DIFFUSE_H__ */
00156