diff --git a/.hgignore b/.hgignore index 313c65b5..69afd325 100644 --- a/.hgignore +++ b/.hgignore @@ -5,6 +5,8 @@ ^doc/.*\.out$ ^doc/.*\.pdf$ ^doc/.*\.toc$ +^doc/.*\.bbl$ +^doc/.*\.blg$ ^doc/plugins_generated.tex$ # Build-related diff --git a/data/tests/test_bsdf.xml b/data/tests/test_bsdf.xml index 3baf3d2b..fdd4d760 100644 --- a/data/tests/test_bsdf.xml +++ b/data/tests/test_bsdf.xml @@ -2,9 +2,6 @@ to be tested for consistency. This is done using the testcase 'test_chisquare' --> - - - @@ -33,6 +30,9 @@ + + + diff --git a/doc/images/bsdf_plastic_default.jpg b/doc/images/bsdf_plastic_default.jpg new file mode 100644 index 00000000..bcbcdb70 Binary files /dev/null and b/doc/images/bsdf_plastic_default.jpg differ diff --git a/doc/images/bsdf_plastic_shiny.jpg b/doc/images/bsdf_plastic_shiny.jpg new file mode 100644 index 00000000..e63c515f Binary files /dev/null and b/doc/images/bsdf_plastic_shiny.jpg differ diff --git a/doc/images/bsdf_roughconductor_anisotropic_aluminium.jpg b/doc/images/bsdf_roughconductor_anisotropic_aluminium.jpg new file mode 100644 index 00000000..e35516d3 Binary files /dev/null and b/doc/images/bsdf_roughconductor_anisotropic_aluminium.jpg differ diff --git a/doc/images/bsdf_roughconductor_copper.jpg b/doc/images/bsdf_roughconductor_copper.jpg new file mode 100644 index 00000000..2472f5f7 Binary files /dev/null and b/doc/images/bsdf_roughconductor_copper.jpg differ diff --git a/doc/images/bsdf_roughdielectric_beckmann_0_0_2.jpg b/doc/images/bsdf_roughdielectric_beckmann_0_0_2.jpg new file mode 100644 index 00000000..d8f8d6da Binary files /dev/null and b/doc/images/bsdf_roughdielectric_beckmann_0_0_2.jpg differ diff --git a/doc/main.bib b/doc/main.bib index 358d7a59..39dedf5d 100644 --- a/doc/main.bib +++ b/doc/main.bib @@ -37,3 +37,12 @@ year = {2005}, publisher = {AK Peters, Ltd.} } + +@inproceedings{Oren1994Generalization, + title = {{Generalization of Lambert's reflectance model}}, + author = {Oren, M. and Nayar, S.K.}, + booktitle = {Proceedings of the 21st annual conference on Computer graphics and interactive techniques}, + pages = {239--246}, + year = {1994}, + organization={ACM} +} diff --git a/src/bsdfs/SConscript b/src/bsdfs/SConscript index 3a3c426d..8693b6d1 100644 --- a/src/bsdfs/SConscript +++ b/src/bsdfs/SConscript @@ -6,7 +6,7 @@ plugins += env.SharedLibrary('dielectric', ['dielectric.cpp']) plugins += env.SharedLibrary('conductor', ['conductor.cpp']) plugins += env.SharedLibrary('plastic', ['plastic.cpp']) -#plugins += env.SharedLibrary('roughdiffuse', ['roughdiffuse.cpp']) +plugins += env.SharedLibrary('roughdiffuse', ['roughdiffuse.cpp']) plugins += env.SharedLibrary('roughdielectric', ['roughdielectric.cpp']) plugins += env.SharedLibrary('roughconductor', ['roughconductor.cpp']) #plugins += env.SharedLibrary('roughplastic', ['roughplastic.cpp']) diff --git a/src/bsdfs/coating.cpp b/src/bsdfs/coating.cpp index 615faec6..b77da784 100644 --- a/src/bsdfs/coating.cpp +++ b/src/bsdfs/coating.cpp @@ -21,7 +21,7 @@ MTS_NAMESPACE_BEGIN -/*! \plugin{coating}{Smooth coating layer} +/*! \plugin{coating}{Smooth coating} * * \parameters{ * \parameter{intIOR}{\Float}{Interior index of refraction \default{1.5046}} diff --git a/src/bsdfs/microfacet.h b/src/bsdfs/microfacet.h index 280d9c2f..661adda6 100644 --- a/src/bsdfs/microfacet.h +++ b/src/bsdfs/microfacet.h @@ -330,12 +330,11 @@ public: const Float nDotM = Frame::cosTheta(m), nDotWo = Frame::cosTheta(wo), nDotWi = Frame::cosTheta(wi), - woDotM = dot(wo, m), - wiDotM = dot(wi, m); + woDotM = dot(wo, m); return std::min((Float) 1, std::min(std::abs(2 * nDotM * nDotWo / woDotM), - std::abs(2 * nDotM * nDotWi / wiDotM))); + std::abs(2 * nDotM * nDotWi / woDotM))); } } diff --git a/src/bsdfs/plastic.cpp b/src/bsdfs/plastic.cpp index d02150e4..3fbe6261 100644 --- a/src/bsdfs/plastic.cpp +++ b/src/bsdfs/plastic.cpp @@ -31,17 +31,34 @@ MTS_NAMESPACE_BEGIN * numerically or using a known material name. \default{\texttt{air} / 1.000277}} * \parameter{specular\showbreak Reflectance}{\Spectrum\Or\Texture}{Optional * factor used to modulate the specular component\default{1.0}} - * \lastparameter{specular\showbreak Transmittance}{\Spectrum\Or\Texture}{Optional + * \lastparameter{diffuse\showbreak Reflectance}{\Spectrum\Or\Texture}{Optional * factor used to modulate the diffuse component\default{0.5}} * } * * \renderings{ - * \medrendering{Air$\leftrightarrow$Water (IOR: 1.33) interface. - * See \lstref{dielectric-water}.}{bsdf_dielectric_water} - * \medrendering{Air$\leftrightarrow$Diamond (IOR: 2.419)}{bsdf_dielectric_diamond} - * \medrendering{Air$\leftrightarrow$Glass (IOR: 1.504) interface and absorption within. - * See \lstref{dielectric-glass}.}{bsdf_dielectric_glass} + * \rendering{A rendering with the default parameters}{bsdf_plastic_default} + * \rendering{A rendering with custom parameters (\lstref{plastic-shiny})}{bsdf_plastic_shiny} * } + * + * This plugin describes a perfectly smooth plastic-like dielectric material + * with internal scattering. The model interpolates between ideally specular + * and ideally diffuse reflection based on the Fresnel reflectance (i.e. it + * does so in a way that depends on the angle of incidence). Similar to the + * \pluginref{dielectric} plugin, IOR values can either be specified + * numerically, or based on a list of known materials (see + * \tblref{dielectric-iors} for an overview). + * + * Since it is very simple and fast, this model is often a better choice + * than the \pluginref{phong}, \pluginref{ward}, and \pluginref{roughplastic} + * plugins when rendering very smooth plastic-like materials. \vspace{4mm} + * + * \begin{xml}[caption=A shiny material whose diffuse reflectance is + * specified using sRGB, label=lst:plastic-shiny] + * + * + * + * + * \end{xml} */ class SmoothPlastic : public BSDF { public: @@ -157,8 +174,8 @@ public: fresnel(Frame::cosTheta(bRec.wi), m_extIOR, m_intIOR) : 1.0f; } else if (measure == ESolidAngle && sampleDiffuse) { return Frame::cosTheta(bRec.wo) * INV_PI * - sampleSpecular ? (1 - fresnel(Frame::cosTheta(bRec.wi), - m_extIOR, m_intIOR)) : 1.0f; + (sampleSpecular ? (1 - fresnel(Frame::cosTheta(bRec.wi), + m_extIOR, m_intIOR)) : 1.0f); } return 0.0f; @@ -170,7 +187,7 @@ public: bool sampleDiffuse = (bRec.typeMask & EDiffuseReflection) && (bRec.component == -1 || bRec.component == 1); - if ((!sampleDiffuse && !sampleSpecular) || Frame::cosTheta(bRec.wi) < 0) + if ((!sampleDiffuse && !sampleSpecular) || Frame::cosTheta(bRec.wi) <= 0) return Spectrum(0.0f); Float Fr = fresnel(Frame::cosTheta(bRec.wi), m_extIOR, m_intIOR); @@ -205,7 +222,7 @@ public: if (Fr == 1.0f) /* Total internal reflection */ return Spectrum(0.0f); - bRec.wo = squareToSphere(sample); + bRec.wo = squareToHemispherePSA(sample); return m_diffuseReflectance->getValue(bRec.its) * (1-Fr); } @@ -217,7 +234,7 @@ public: bool sampleDiffuse = (bRec.typeMask & EDiffuseReflection) && (bRec.component == -1 || bRec.component == 1); - if ((!sampleDiffuse && !sampleSpecular) || Frame::cosTheta(bRec.wi) < 0) + if ((!sampleDiffuse && !sampleSpecular) || Frame::cosTheta(bRec.wi) <= 0) return Spectrum(0.0f); Float Fr = fresnel(Frame::cosTheta(bRec.wi), m_extIOR, m_intIOR); @@ -257,7 +274,7 @@ public: if (Fr == 1.0f) /* Total internal reflection */ return Spectrum(0.0f); - bRec.wo = squareToSphere(sample); + bRec.wo = squareToHemispherePSA(sample); pdf = Frame::cosTheta(bRec.wo) * INV_PI; diff --git a/src/bsdfs/roughconductor.cpp b/src/bsdfs/roughconductor.cpp index ab41d836..e6d6ec3c 100644 --- a/src/bsdfs/roughconductor.cpp +++ b/src/bsdfs/roughconductor.cpp @@ -513,10 +513,10 @@ public: << " if ((dot(wi, m) * cosTheta(wi)) <= 0 || " << endl << " (dot(wo, m) * cosTheta(wo)) <= 0)" << endl << " return 0.0;" << endl - << " float nDotM = cosTheta(m);" << endl + << " float nDotM = cosTheta(m), tmp = 1.0 / dot(wo, m);" << endl << " return min(1.0, min(" << endl - << " abs(2 * nDotM * cosTheta(wo) / dot(wo, m))," << endl - << " abs(2 * nDotM * cosTheta(wi) / dot(wi, m))));" << endl + << " abs(2 * nDotM * cosTheta(wo) * tmp)," << endl + << " abs(2 * nDotM * cosTheta(wi) * tmp)));" << endl << "}" << endl << endl << "vec3 " << evalName << "_schlick(vec3 wi) {" << endl diff --git a/src/bsdfs/roughdiffuse.cpp b/src/bsdfs/roughdiffuse.cpp new file mode 100644 index 00000000..7fc05bd5 --- /dev/null +++ b/src/bsdfs/roughdiffuse.cpp @@ -0,0 +1,255 @@ +/* + This file is part of Mitsuba, a physically based rendering system. + + Copyright (c) 2007-2011 by Wenzel Jakob and others. + + Mitsuba is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License Version 3 + as published by the Free Software Foundation. + + Mitsuba is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include + +MTS_NAMESPACE_BEGIN + +/*!\plugin{diffuse}{Rough diffuse material} + * \order{2} + * \parameters{ + * \parameter{reflectance}{\Spectrum\Or\Texture}{ + * Specifies the reflectance / albedo of the material \linebreak(Default: 0.5) + * } + * \lastparameter{alpha}{\Spectrum\Or\Texture}{ + * Specifies the roughness of the unresolved surface microgeometry. + * This parameter is approximately equal to the \emph{root mean square} + * (RMS) slope of the microfacets.\default{0.1} + * } + * } + * + * \renderings{ + * \rendering{Homogeneous reflectance, see \lstref{diffuse-uniform}}{bsdf_diffuse_plain} + * \rendering{Textured reflectance, see \lstref{diffuse-textured}}{bsdf_diffuse_textured} + * } + * + * This reflectance model describes scattering from a rough diffuse material, + * such as plaster, sand, clay, or concrete. + * The underlying theory was developed by Oren and Nayar + * \cite{Oren1994Generalization}, who model the microscopic surface structure as + * an arrangement of unresolved planar facets with different slopes, where each facet + * is an ideal diffuse reflector. The model takes into account shadowing, + * masking, as well as interreflections between the facets. + * + * Since the original publication in 1994, this approach has been shown to + * be a very good match for many real-world materials, in particular + * compared to Lambertian scattering, which does not take surface + * roughness into account. + * + * To get an intuition about the effect of the surface roughness + * parameter $\alpha$, consider the following approximate differentiation: + * a value of $\alpha=0.001-0.01$ corresponds to a material + * with slight imperfections on an otherwise smooth surface (for such small + * values, the model will behave almost identically to \pluginref{diffuse}), $\alpha=0.1$ + * is relatively rough, and $\alpha=0.3-0.5$ is \emph{extremely} rough + * (e.g. an etched or ground finish). + * + * Note that this material is one-sided---that is, observed from the + * back side, it will be completely black. If this is undesirable, + * consider using the \pluginref{twosided} BRDF adapter plugin. + * \vspace{4mm} + * + * \begin{xml}[caption={A diffuse material, whose reflectance is specified as an sRGB color}, label=lst:diffuse-uniform] + * + * + * + * \end{xml} + */ +class RoughDiffuse : public BSDF { +public: + RoughDiffuse(const Properties &props) + : BSDF(props) { + /* For better compatibility with other models, support both + 'reflectance' and 'diffuseReflectance' as parameter names */ + m_reflectance = new ConstantSpectrumTexture(props.getSpectrum( + props.hasProperty("reflectance") ? "reflectance" + : "diffuseReflectance", Spectrum(.5f))); + + m_alpha = new ConstantFloatTexture(props.getFloat("alpha", 0.1f)); + m_components.push_back(EGlossyReflection | EFrontSide); + m_usesRayDifferentials = false; + } + + RoughDiffuse(Stream *stream, InstanceManager *manager) + : BSDF(stream, manager) { + m_reflectance = static_cast(manager->getInstance(stream)); + m_alpha = static_cast(manager->getInstance(stream)); + m_components.push_back(EGlossyReflection | EFrontSide); + m_usesRayDifferentials = m_reflectance->usesRayDifferentials(); + } + + virtual ~RoughDiffuse() { } + + void configure() { + BSDF::configure(); + + /* Verify the input parameter and fix them if necessary */ + m_reflectance = ensureEnergyConservation(m_reflectance, "reflectance", 1.0f); + } + + Spectrum eval(const BSDFQueryRecord &bRec, EMeasure measure) const { + if (!(bRec.typeMask & EGlossyReflection) || measure != ESolidAngle + || Frame::cosTheta(bRec.wi) <= 0 + || Frame::cosTheta(bRec.wo) <= 0) + return Spectrum(0.0f); + + /* Conversion from Beckmann-style RMS roughness to + Oren-Nayar-style slope-area variance. The factor + of 1/sqrt(2) was found to be a perfect fit up + to extreme roughness values (>.5), after which + the match is not as good anymore */ + const Float conversionFactor = 1 / std::sqrt((Float) 2); + + Float sigma = m_alpha->getValue(bRec.its).average() + * conversionFactor; + + Float sigma2 = sigma*sigma; + Float A = 10.f - (sigma2 / (2.0f * (sigma2 + 0.33f))); + Float B = 0.45f * sigma2 / (sigma2 + 0.09f); + + return m_reflectance->getValue(bRec.its) + * (INV_PI * Frame::cosTheta(bRec.wo)); + } + + Float pdf(const BSDFQueryRecord &bRec, EMeasure measure) const { + if (!(bRec.typeMask & EGlossyReflection) || measure != ESolidAngle + || Frame::cosTheta(bRec.wi) <= 0 + || Frame::cosTheta(bRec.wo) <= 0) + return 0.0f; + + return Frame::cosTheta(bRec.wo) * INV_PI; + } + + Spectrum sample(BSDFQueryRecord &bRec, const Point2 &sample) const { + if (!(bRec.typeMask & EGlossyReflection) || Frame::cosTheta(bRec.wi) <= 0) + return Spectrum(0.0f); + + bRec.wo = squareToHemispherePSA(sample); + bRec.sampledComponent = 0; + bRec.sampledType = EGlossyReflection; + return m_reflectance->getValue(bRec.its); + } + + Spectrum sample(BSDFQueryRecord &bRec, Float &pdf, const Point2 &sample) const { + if (!(bRec.typeMask & EGlossyReflection) || Frame::cosTheta(bRec.wi) <= 0) + return Spectrum(0.0f); + + bRec.wo = squareToHemispherePSA(sample); + bRec.sampledComponent = 0; + bRec.sampledType = EGlossyReflection; + pdf = Frame::cosTheta(bRec.wo) * INV_PI; + return m_reflectance->getValue(bRec.its) + * (INV_PI * Frame::cosTheta(bRec.wo)); + } + + void addChild(const std::string &name, ConfigurableObject *child) { + if (child->getClass()->derivesFrom(MTS_CLASS(Texture)) + && (name == "reflectance" || name == "diffuseReflectance")) { + m_reflectance = static_cast(child); + m_usesRayDifferentials |= m_reflectance->usesRayDifferentials(); + } else if (child->getClass()->derivesFrom(MTS_CLASS(Texture)) + && name == "alpha") { + m_alpha = static_cast(child); + m_usesRayDifferentials |= m_reflectance->usesRayDifferentials(); + } else { + BSDF::addChild(name, child); + } + } + + void serialize(Stream *stream, InstanceManager *manager) const { + BSDF::serialize(stream, manager); + + manager->serialize(stream, m_reflectance.get()); + manager->serialize(stream, m_alpha.get()); + } + + std::string toString() const { + std::ostringstream oss; + oss << "RoughDiffuse[" << endl + << " name = \"" << getName() << "\"," << endl + << " reflectance = " << indent(m_reflectance->toString()) << "," << endl + << " alpha = " << indent(m_alpha->toString()) << endl + << "]"; + return oss.str(); + } + + Shader *createShader(Renderer *renderer) const; + + MTS_DECLARE_CLASS() +private: + ref m_reflectance; + ref m_alpha; +}; + +// ================ Hardware shader implementation ================ + +class RoughDiffuseShader : public Shader { +public: + RoughDiffuseShader(Renderer *renderer, const Texture *reflectance, const Texture *alpha) + : Shader(renderer, EBSDFShader), m_reflectance(reflectance), m_alpha(alpha) { + m_reflectanceShader = renderer->registerShaderForResource(m_reflectance.get()); + m_alphaShader = renderer->registerShaderForResource(m_alpha.get()); + } + + bool isComplete() const { + return m_reflectanceShader.get() != NULL && + m_alphaShader.get() != NULL; + } + + void cleanup(Renderer *renderer) { + renderer->unregisterShaderForResource(m_reflectance.get()); + renderer->unregisterShaderForResource(m_alpha.get()); + } + + void putDependencies(std::vector &deps) { + deps.push_back(m_reflectanceShader.get()); + deps.push_back(m_alphaShader.get()); + } + + void generateCode(std::ostringstream &oss, + const std::string &evalName, + const std::vector &depNames) const { + oss << "vec3 " << evalName << "(vec2 uv, vec3 wi, vec3 wo) {" << endl + << " if (cosTheta(wi) < 0.0 || cosTheta(wo) < 0.0)" << endl + << " return vec3(0.0);" << endl + << " return " << depNames[0] << "(uv) * 0.31831 * cosTheta(wo);" << endl + << "}" << endl + << endl + << "vec3 " << evalName << "_diffuse(vec2 uv, vec3 wi, vec3 wo) {" << endl + << " return " << evalName << "(uv, wi, wo);" << endl + << "}" << endl; + } + + MTS_DECLARE_CLASS() +private: + ref m_reflectance; + ref m_alpha; + ref m_reflectanceShader; + ref m_alphaShader; +}; + +Shader *RoughDiffuse::createShader(Renderer *renderer) const { + return new RoughDiffuseShader(renderer, m_reflectance.get(), m_alpha.get()); +} + +MTS_IMPLEMENT_CLASS(RoughDiffuseShader, false, Shader) +MTS_IMPLEMENT_CLASS_S(RoughDiffuse, false, BSDF) +MTS_EXPORT_PLUGIN(RoughDiffuse, "Rough diffuse BRDF") +MTS_NAMESPACE_END