erebus

diff liberebus/src/brdf.cc @ 0:4abdce1361b9

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 Apr 2014 16:02:47 +0300
parents
children 474a0244f57d
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/liberebus/src/brdf.cc	Sun Apr 27 16:02:47 2014 +0300
     1.3 @@ -0,0 +1,130 @@
     1.4 +#include "brdf.h"
     1.5 +#include "erebus_impl.h"
     1.6 +
     1.7 +ReflAttrib::ReflAttrib()
     1.8 +	: value(1), color(1, 1, 1), map(0)
     1.9 +{
    1.10 +}
    1.11 +
    1.12 +ReflAttrib::ReflAttrib(const Color &col, Texture *tex)
    1.13 +{
    1.14 +	set_color(col);
    1.15 +	map = tex;
    1.16 +}
    1.17 +
    1.18 +void ReflAttrib::set_value(float val)
    1.19 +{
    1.20 +	value = val;
    1.21 +	color = Color{val, val, val};
    1.22 +}
    1.23 +
    1.24 +void ReflAttrib::set_color(const Color &col)
    1.25 +{
    1.26 +	color = col;
    1.27 +	value = color_luminance(col);
    1.28 +}
    1.29 +
    1.30 +void ReflAttrib::set_map(Texture *tex)
    1.31 +{
    1.32 +	map = tex;
    1.33 +}
    1.34 +
    1.35 +Texture *ReflAttrib::get_map() const
    1.36 +{
    1.37 +	return map;
    1.38 +}
    1.39 +
    1.40 +float ReflAttrib::get_value() const
    1.41 +{
    1.42 +	return value;
    1.43 +}
    1.44 +
    1.45 +float ReflAttrib::get_value(float u, float v) const
    1.46 +{
    1.47 +	return map ? value * color_luminance(map->lookup(u, v)) : value;
    1.48 +}
    1.49 +
    1.50 +const Color &ReflAttrib::get_color() const
    1.51 +{
    1.52 +	return color;
    1.53 +}
    1.54 +
    1.55 +Color ReflAttrib::get_color(float u, float v) const
    1.56 +{
    1.57 +	return map ? color * map->lookup(u, v) : color;
    1.58 +}
    1.59 +
    1.60 +// ---- class Reflectance ----
    1.61 +ReflAttrib Reflectance::def_attrib;
    1.62 +
    1.63 +Reflectance::Reflectance()
    1.64 +{
    1.65 +	set_default_attribs();
    1.66 +}
    1.67 +
    1.68 +void Reflectance::set_attrib(const char *name, const Color &color, Texture *tex)
    1.69 +{
    1.70 +	attrib[name] = ReflAttrib{color, tex};
    1.71 +}
    1.72 +
    1.73 +ReflAttrib &Reflectance::get_attrib(const char *name)
    1.74 +{
    1.75 +	auto it = attrib.find(name);
    1.76 +	if(it == attrib.end()) {
    1.77 +		return def_attrib;
    1.78 +	}
    1.79 +	return it->second;
    1.80 +}
    1.81 +
    1.82 +const ReflAttrib &Reflectance::get_attrib(const char *name) const
    1.83 +{
    1.84 +	auto it = attrib.find(name);
    1.85 +	if(it == attrib.end()) {
    1.86 +		return def_attrib;
    1.87 +	}
    1.88 +	return it->second;
    1.89 +}
    1.90 +
    1.91 +float Reflectance::get_attrib_value(const char *name) const
    1.92 +{
    1.93 +	return get_attrib(name).get_value();
    1.94 +}
    1.95 +
    1.96 +float Reflectance::get_attrib_value(const char *name, float u, float v) const
    1.97 +{
    1.98 +	return get_attrib(name).get_value(u, v);
    1.99 +}
   1.100 +
   1.101 +Color Reflectance::get_attrib_color(const char *name) const
   1.102 +{
   1.103 +	return get_attrib(name).get_color();
   1.104 +}
   1.105 +
   1.106 +Color Reflectance::get_attrib_color(const char *name, float u, float v) const
   1.107 +{
   1.108 +	return get_attrib(name).get_color(u, v);
   1.109 +}
   1.110 +
   1.111 +float Reflectance::sample(const Vector3 &norm, const Vector3 &outdir, Vector3 *indir) const
   1.112 +{
   1.113 +	*indir = sample_dir(norm, outdir);
   1.114 +	return eval(norm, outdir, *indir);
   1.115 +}
   1.116 +
   1.117 +// --- class LambertRefl ---
   1.118 +
   1.119 +void LambertRefl::set_default_attribs()
   1.120 +{
   1.121 +	set_attrib("color", Color(1, 1, 1));
   1.122 +}
   1.123 +
   1.124 +Vector3 LambertRefl::sample_dir(const Vector3 &norm, const Vector3 &outdir) const
   1.125 +{
   1.126 +	Vector3 dir = Vector3{randf(-1, 1), randf(-1, 1), randf(-1, 1)}.normalized();
   1.127 +	return dot_product(dir, norm) < 0.0 ? -dir : dir;
   1.128 +}
   1.129 +
   1.130 +float LambertRefl::eval(const Vector3 &norm, const Vector3 &outdir, const Vector3 &indir) const
   1.131 +{
   1.132 +	return dot_product(norm, outdir);
   1.133 +}
   1.134 \ No newline at end of file