Blender V2.61 - r43446

bsdf_diffuse.cpp

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 #include <OpenImageIO/fmath.h>
00034 
00035 #include <OSL/genclosure.h>
00036 
00037 #include "osl_closures.h"
00038 
00039 CCL_NAMESPACE_BEGIN
00040 
00041 using namespace OSL;
00042 
00043 class DiffuseClosure : public BSDFClosure {
00044 public:
00045     Vec3 m_N;
00046 
00047     DiffuseClosure() : BSDFClosure(Labels::DIFFUSE) { }
00048 
00049     void setup() {};
00050 
00051     bool mergeable (const ClosurePrimitive *other) const {
00052         const DiffuseClosure *comp = (const DiffuseClosure *)other;
00053         return m_N == comp->m_N && BSDFClosure::mergeable(other);
00054     }
00055 
00056     size_t memsize () const { return sizeof(*this); }
00057 
00058     const char *name () const { return "diffuse"; }
00059 
00060     void print_on (std::ostream &out) const
00061     {
00062         out << name() << " ((" << m_N[0] << ", " << m_N[1] << ", " << m_N[2] << "))";
00063     }
00064 
00065     float albedo (const Vec3 &omega_out) const
00066     {
00067         return 1.0f;
00068     }
00069 
00070     Color3 eval_reflect (const Vec3 &omega_out, const Vec3 &omega_in, float& pdf) const
00071     {
00072         float cos_pi = std::max(m_N.dot(omega_in),0.0f) * (float) M_1_PI;
00073         pdf = cos_pi;
00074         return Color3 (cos_pi, cos_pi, cos_pi);
00075     }
00076 
00077     Color3 eval_transmit (const Vec3 &omega_out, const Vec3 &omega_in, float& pdf) const
00078     {
00079         return Color3 (0, 0, 0);
00080     }
00081 
00082     ustring sample (const Vec3 &Ng,
00083                  const Vec3 &omega_out, const Vec3 &domega_out_dx, const Vec3 &domega_out_dy,
00084                  float randu, float randv,
00085                  Vec3 &omega_in, Vec3 &domega_in_dx, Vec3 &domega_in_dy,
00086                  float &pdf, Color3 &eval) const
00087     {
00088         // we are viewing the surface from the right side - send a ray out with cosine
00089         // distribution over the hemisphere
00090         sample_cos_hemisphere (m_N, omega_out, randu, randv, omega_in, pdf);
00091         if (Ng.dot(omega_in) > 0) {
00092             eval.setValue(pdf, pdf, pdf);
00093             // TODO: find a better approximation for the diffuse bounce
00094             domega_in_dx = (2 * m_N.dot(domega_out_dx)) * m_N - domega_out_dx;
00095             domega_in_dy = (2 * m_N.dot(domega_out_dy)) * m_N - domega_out_dy;
00096             domega_in_dx *= 125;
00097             domega_in_dy *= 125;
00098         } else
00099             pdf = 0;
00100         return Labels::REFLECT;
00101     }
00102 };
00103 
00104 
00105 
00106 class TranslucentClosure : public BSDFClosure {
00107 public:
00108     Vec3 m_N;
00109 
00110     TranslucentClosure() : BSDFClosure(Labels::DIFFUSE, Back) { }
00111 
00112     void setup() {};
00113 
00114     bool mergeable (const ClosurePrimitive *other) const {
00115         const TranslucentClosure *comp = (const TranslucentClosure *)other;
00116         return m_N == comp->m_N && BSDFClosure::mergeable(other);
00117     }
00118 
00119     size_t memsize () const { return sizeof(*this); }
00120 
00121     const char *name () const { return "translucent"; }
00122 
00123     void print_on (std::ostream &out) const
00124     {
00125         out << name() << " ((" << m_N[0] << ", " << m_N[1] << ", " << m_N[2] << "))";
00126     }
00127 
00128     Color3 eval_reflect (const Vec3 &omega_out, const Vec3 &omega_in, float& pdf) const
00129     {
00130         return Color3 (0, 0, 0);
00131     }
00132 
00133     float albedo (const Vec3 &omega_out) const
00134     {
00135         return 1.0f;
00136     }
00137 
00138     Color3 eval_transmit (const Vec3 &omega_out, const Vec3 &omega_in, float& pdf) const
00139     {
00140         float cos_pi = std::max(-m_N.dot(omega_in), 0.0f) * (float) M_1_PI;
00141         pdf = cos_pi;
00142         return Color3 (cos_pi, cos_pi, cos_pi);
00143     }
00144 
00145     ustring sample (const Vec3 &Ng,
00146                  const Vec3 &omega_out, const Vec3 &domega_out_dx, const Vec3 &domega_out_dy,
00147                  float randu, float randv,
00148                  Vec3 &omega_in, Vec3 &domega_in_dx, Vec3 &domega_in_dy,
00149                  float &pdf, Color3 &eval) const
00150     {
00151         // we are viewing the surface from the right side - send a ray out with cosine
00152         // distribution over the hemisphere
00153         sample_cos_hemisphere (-m_N, omega_out, randu, randv, omega_in, pdf);
00154         if (Ng.dot(omega_in) < 0) {
00155             eval.setValue(pdf, pdf, pdf);
00156             // TODO: find a better approximation for the diffuse bounce
00157             domega_in_dx = (2 * m_N.dot(domega_out_dx)) * m_N - domega_out_dx;
00158             domega_in_dy = (2 * m_N.dot(domega_out_dy)) * m_N - domega_out_dy;
00159             domega_in_dx *= -125;
00160             domega_in_dy *= -125;
00161         } else
00162             pdf = 0;
00163         return Labels::TRANSMIT;
00164     }
00165 };
00166 
00167 ClosureParam bsdf_diffuse_params[] = {
00168     CLOSURE_VECTOR_PARAM   (DiffuseClosure, m_N),
00169     CLOSURE_STRING_KEYPARAM("label"),
00170     CLOSURE_FINISH_PARAM   (DiffuseClosure) };
00171 
00172 ClosureParam bsdf_translucent_params[] = {
00173     CLOSURE_VECTOR_PARAM   (TranslucentClosure, m_N),
00174     CLOSURE_STRING_KEYPARAM("label"),
00175     CLOSURE_FINISH_PARAM   (TranslucentClosure) };
00176 
00177 CLOSURE_PREPARE(bsdf_diffuse_prepare, DiffuseClosure)
00178 CLOSURE_PREPARE(bsdf_translucent_prepare, TranslucentClosure)
00179 
00180 CCL_NAMESPACE_END
00181