00001 /*************************************************************************** 00002 * Copyright (C) 1998-2009 by authors (see AUTHORS.txt ) * 00003 * * 00004 * This file is part of LuxRender. * 00005 * * 00006 * Lux Renderer is free software; you can redistribute it and/or modify * 00007 * it under the terms of the GNU General Public License as published by * 00008 * the Free Software Foundation; either version 3 of the License, or * 00009 * (at your option) any later version. * 00010 * * 00011 * Lux Renderer is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00014 * GNU General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU General Public License * 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 00018 * * 00019 * This project is based on PBRT ; see http://www.pbrt.org * 00020 * Lux Renderer website : http://www.luxrender.net * 00021 ***************************************************************************/ 00022 00023 // beckmann.cpp* 00024 #include "beckmann.h" 00025 #include "bxdf.h" 00026 00027 using namespace lux; 00028 00029 Beckmann::Beckmann(float rms) { 00030 r = rms; 00031 } 00032 00033 float Beckmann::D(const Vector &wh) const { 00034 float costhetah = CosTheta(wh); 00035 float theta = acos(costhetah); 00036 float tanthetah = tan(theta); 00037 00038 float dfac = tanthetah / r; 00039 00040 return exp(-(dfac * dfac)) / (r * r * powf(costhetah, 4.0)); 00041 } 00042 00043 void Beckmann::Sample_f(const Vector &wo, Vector *wi, float u1, float u2, float *pdf) const { 00044 // Compute sampled half-angle vector $\wh$ for Beckmann distribution 00045 // Adapted from B. Walter et al, Microfacet Models for Refraction, Eurographics Symposium on Rendering, 2007, page 7 00046 00047 float theta = atan (sqrt (-(r * r) * log(1.0 - u1))); 00048 float costheta = cos (theta); 00049 float sintheta = sqrtf(max(0.f, 1.f - costheta*costheta)); 00050 float phi = u2 * 2.f * M_PI; 00051 00052 Vector H = SphericalDirection(sintheta, costheta, phi); 00053 00054 if (!SameHemisphere(wo, H)) 00055 H.z *= -1.f; 00056 00057 // Compute incident direction by reflecting about $\wh$ 00058 *wi = -wo + 2.f * Dot(wo, H) * H; 00059 00060 // Compute PDF for \wi from Beckmann distribution - note that the inverse of the integral over 00061 // the Beckmann distribution is not available in closed form, so this is not really correct 00062 // (see Kelemen and Szirmay-Kalos / Microfacet Based BRDF Model, Eurographics 2001) 00063 00064 float conversion_factor = 1.0 / (4.f * Dot(wo, H)); 00065 float beckmann_pdf = conversion_factor * D(H); 00066 00067 *pdf = beckmann_pdf; 00068 } 00069 00070 // NB: See note above! 00071 float Beckmann::Pdf(const Vector &wo, const Vector &wi) const { 00072 Vector H = Normalize(wo + wi); 00073 float conversion_factor = 1.0 / 4.f * Dot(wo, H); 00074 float beckmann_pdf = conversion_factor * D(H); 00075 00076 return beckmann_pdf; 00077 } 00078