// Cloth Method Definitions
BSDF *Cloth::GetBSDF(MemoryArena &arena, const SpectrumWavelengths &sw,
	const Intersection &isect, const DifferentialGeometry &dgs) const
{
	// Determine cloth coordinates and yarn at current point
	Point uv;
	float umax, scale = specularNormalization;
	const Yarn *yarn = Pattern->GetYarn(dgs.u, dgs.v, &uv, &umax, &scale);

	// Allocate _BSDF_
	MultiBSDF<2> *bsdf = ARENA_ALLOC(arena, MultiBSDF<2>)(dgs, isect.dg.nn,
		isect.exterior, isect.interior);

	bsdf->Add(ARENA_ALLOC(arena, Lambertian)
		(Kds.at(yarn->index)->Evaluate(sw, dgs).Clamp(0.f, 1.f)));

	bsdf->Add(ARENA_ALLOC(arena, Irawan)
		(Kss.at(yarn->index)->Evaluate(sw, dgs).Clamp(0.f, 1.f),
		uv, umax, yarn, Pattern.get(), scale));

	// Add ptr to CompositingParams structure
	bsdf->SetCompositingParams(&compParams);

	return bsdf;
}
// Glossy Method Definitions
BSDF *GlossyTranslucent::GetBSDF(MemoryArena &arena, const SpectrumWavelengths &sw,
	const Intersection &isect, const DifferentialGeometry &dgs) const
{
	// Allocate _BSDF_
	// NOTE - lordcrc - changed clamping to 0..1 to avoid >1 reflection
	SWCSpectrum d(Kd->Evaluate(sw, dgs).Clamp(0.f, 1.f));
	SWCSpectrum t(Kt->Evaluate(sw, dgs).Clamp(0.f, 1.f));
	
	SWCSpectrum s(Ks->Evaluate(sw, dgs));
	float i = index->Evaluate(sw, dgs);
	if (i > 0.f) {
		const float ti = (i - 1.f) / (i + 1.f);
		s *= ti * ti;
	}
	s = s.Clamp(0.f, 1.f);
	
	SWCSpectrum a(Ka->Evaluate(sw, dgs).Clamp(0.f, 1.f));
	float ld = depth->Evaluate(sw, dgs);

	SWCSpectrum bs(Ks_bf->Evaluate(sw, dgs));
	float bi = index_bf->Evaluate(sw, dgs);
	if (bi > 0.f) {
		const float bti = (bi - 1.f) / (bi + 1.f);
		bs *= bti * bti;
	}
	bs = bs.Clamp(0.f, 1.f);

	SWCSpectrum ba(Ka_bf->Evaluate(sw, dgs).Clamp(0.f, 1.f));
	float bld = depth_bf->Evaluate(sw, dgs);

	// Clamp roughness values to avoid artifacts with too small values
	const float u = Clamp(nu->Evaluate(sw, dgs), 6e-3f, 1.f);
	const float v = Clamp(nv->Evaluate(sw, dgs), 6e-3f, 1.f);
	const float u2 = u * u;
	const float v2 = v * v;

	const float roughness = u * v;
	const float anisotropy = u2 < v2 ? 1.f - u2 / v2 : v2 / u2 - 1.f;
	
	// Clamp roughness values to avoid artifacts with too small values
	const float bu = Clamp(nu_bf->Evaluate(sw, dgs), 6e-3f, 1.f);
	const float bv = Clamp(nv_bf->Evaluate(sw, dgs), 6e-3f, 1.f);
	const float bu2 = bu * bu;
	const float bv2 = bv * bv;

	const float broughness = bu * bv;
	const float banisotropy = bu2 < bv2 ? 1.f - bu2 / bv2 : bv2 / bu2 - 1.f;
	
	MultiBSDF<2> *bsdf = ARENA_ALLOC(arena, MultiBSDF<2>)(dgs, isect.dg.nn,
		isect.exterior, isect.interior);
	
	// add the BRDF
	bsdf->Add(ARENA_ALLOC(arena, SchlickDoubleSidedBRDF)(d, s, bs, a, ba,
		ld, bld, roughness, broughness, anisotropy, banisotropy,
		multibounce, multibounce_bf));
	
	// add the BTDF
	bsdf->Add(ARENA_ALLOC(arena, SchlickTranslucentBTDF)(d, t, s, bs, a, ba,
		ld, bld));

	// Add ptr to CompositingParams structure
	bsdf->SetCompositingParams(&compParams);

	return bsdf;
}