nuclear@0: #include "brdf.h" nuclear@0: #include "erebus_impl.h" nuclear@0: nuclear@0: ReflAttrib::ReflAttrib() nuclear@0: : value(1), color(1, 1, 1), map(0) nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: ReflAttrib::ReflAttrib(const Color &col, Texture *tex) nuclear@0: { nuclear@0: set_color(col); nuclear@0: map = tex; nuclear@0: } nuclear@0: nuclear@0: void ReflAttrib::set_value(float val) nuclear@0: { nuclear@0: value = val; nuclear@0: color = Color{val, val, val}; nuclear@0: } nuclear@0: nuclear@0: void ReflAttrib::set_color(const Color &col) nuclear@0: { nuclear@0: color = col; nuclear@0: value = color_luminance(col); nuclear@0: } nuclear@0: nuclear@0: void ReflAttrib::set_map(Texture *tex) nuclear@0: { nuclear@0: map = tex; nuclear@0: } nuclear@0: nuclear@0: Texture *ReflAttrib::get_map() const nuclear@0: { nuclear@0: return map; nuclear@0: } nuclear@0: nuclear@0: float ReflAttrib::get_value() const nuclear@0: { nuclear@0: return value; nuclear@0: } nuclear@0: nuclear@0: float ReflAttrib::get_value(float u, float v) const nuclear@0: { nuclear@0: return map ? value * color_luminance(map->lookup(u, v)) : value; nuclear@0: } nuclear@0: nuclear@0: const Color &ReflAttrib::get_color() const nuclear@0: { nuclear@0: return color; nuclear@0: } nuclear@0: nuclear@0: Color ReflAttrib::get_color(float u, float v) const nuclear@0: { nuclear@0: return map ? color * map->lookup(u, v) : color; nuclear@0: } nuclear@0: nuclear@0: // ---- class Reflectance ---- nuclear@0: ReflAttrib Reflectance::def_attrib; nuclear@0: nuclear@0: Reflectance::Reflectance() nuclear@0: { nuclear@0: set_default_attribs(); nuclear@0: } nuclear@0: nuclear@0: void Reflectance::set_attrib(const char *name, const Color &color, Texture *tex) nuclear@0: { nuclear@0: attrib[name] = ReflAttrib{color, tex}; nuclear@0: } nuclear@0: nuclear@0: ReflAttrib &Reflectance::get_attrib(const char *name) nuclear@0: { nuclear@0: auto it = attrib.find(name); nuclear@0: if(it == attrib.end()) { nuclear@0: return def_attrib; nuclear@0: } nuclear@0: return it->second; nuclear@0: } nuclear@0: nuclear@0: const ReflAttrib &Reflectance::get_attrib(const char *name) const nuclear@0: { nuclear@0: auto it = attrib.find(name); nuclear@0: if(it == attrib.end()) { nuclear@0: return def_attrib; nuclear@0: } nuclear@0: return it->second; nuclear@0: } nuclear@0: nuclear@0: float Reflectance::get_attrib_value(const char *name) const nuclear@0: { nuclear@0: return get_attrib(name).get_value(); nuclear@0: } nuclear@0: nuclear@0: float Reflectance::get_attrib_value(const char *name, float u, float v) const nuclear@0: { nuclear@0: return get_attrib(name).get_value(u, v); nuclear@0: } nuclear@0: nuclear@0: Color Reflectance::get_attrib_color(const char *name) const nuclear@0: { nuclear@0: return get_attrib(name).get_color(); nuclear@0: } nuclear@0: nuclear@0: Color Reflectance::get_attrib_color(const char *name, float u, float v) const nuclear@0: { nuclear@0: return get_attrib(name).get_color(u, v); nuclear@0: } nuclear@0: nuclear@0: float Reflectance::sample(const Vector3 &norm, const Vector3 &outdir, Vector3 *indir) const nuclear@0: { nuclear@0: *indir = sample_dir(norm, outdir); nuclear@0: return eval(norm, outdir, *indir); nuclear@0: } nuclear@0: nuclear@0: // --- class LambertRefl --- nuclear@0: nuclear@0: void LambertRefl::set_default_attribs() nuclear@0: { nuclear@0: set_attrib("color", Color(1, 1, 1)); nuclear@0: } nuclear@0: nuclear@0: Vector3 LambertRefl::sample_dir(const Vector3 &norm, const Vector3 &outdir) const nuclear@0: { nuclear@0: Vector3 dir = Vector3{randf(-1, 1), randf(-1, 1), randf(-1, 1)}.normalized(); nuclear@0: return dot_product(dir, norm) < 0.0 ? -dir : dir; nuclear@0: } nuclear@0: nuclear@0: float LambertRefl::eval(const Vector3 &norm, const Vector3 &outdir, const Vector3 &indir) const nuclear@0: { nuclear@0: return dot_product(norm, outdir); nuclear@0: }