00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "plastic.h"
00025 #include "bxdf.h"
00026 #include "lambertian.h"
00027 #include "fresneldielectric.h"
00028 #include "microfacet.h"
00029 #include "blinn.h"
00030 #include "anisotropic.h"
00031 #include "paramset.h"
00032
00033 using namespace lux;
00034
00035
00036 BSDF *Plastic::GetBSDF(const DifferentialGeometry &dgGeom,
00037 const DifferentialGeometry &dgShading, float) const {
00038
00039 DifferentialGeometry dgs;
00040 if (bumpMap)
00041 Bump(bumpMap, dgGeom, dgShading, &dgs);
00042 else
00043 dgs = dgShading;
00044 BSDF *bsdf = BSDF_ALLOC( BSDF)(dgs, dgGeom.nn);
00045
00046 Spectrum kd(Kd->Evaluate(dgs).Clamp(0.f, 1.f));
00047
00048 Spectrum ks(Ks->Evaluate(dgs).Clamp(0.f, 1.f));
00049
00050 Spectrum sum(kd + ks);
00051 float sumMax = 0.f;
00052 for (int i = 0; i < COLOR_SAMPLES; ++i)
00053 sumMax = max<float>(sumMax, sum.c[i]);
00054 if (sumMax > 1.f) {
00055 kd /= sumMax;
00056 ks /= sumMax;
00057 }
00058 BxDF *diff = BSDF_ALLOC( Lambertian)(SWCSpectrum(kd));
00059 Fresnel *fresnel =
00060 BSDF_ALLOC( FresnelDielectric)(1.5f, 1.f);
00061
00062 float u = nu->Evaluate(dgs);
00063 float v = nv->Evaluate(dgs);
00064
00065 MicrofacetDistribution *md;
00066 if(u == v)
00067 md = BSDF_ALLOC( Blinn)(1.f / u);
00068 else
00069 md = BSDF_ALLOC( Anisotropic)(1.f/u, 1.f/v);
00070
00071 BxDF *spec = BSDF_ALLOC( Microfacet)(SWCSpectrum(ks), fresnel, md);
00072 bsdf->Add(diff);
00073 bsdf->Add(spec);
00074 return bsdf;
00075 }
00076
00077 Material* Plastic::CreateMaterial(const Transform &xform,
00078 const TextureParams &mp) {
00079 boost::shared_ptr<Texture<Spectrum> > Kd = mp.GetSpectrumTexture("Kd", Spectrum(1.f));
00080 boost::shared_ptr<Texture<Spectrum> > Ks = mp.GetSpectrumTexture("Ks", Spectrum(1.f));
00081 boost::shared_ptr<Texture<float> > uroughness = mp.GetFloatTexture("uroughness", .1f);
00082 boost::shared_ptr<Texture<float> > vroughness = mp.GetFloatTexture("vroughness", .1f);
00083 boost::shared_ptr<Texture<float> > bumpMap = mp.GetFloatTexture("bumpmap", 0.f);
00084 return new Plastic(Kd, Ks, uroughness, vroughness, bumpMap);
00085 }