erebus

annotate 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
rev   line source
nuclear@0 1 #include "brdf.h"
nuclear@0 2 #include "erebus_impl.h"
nuclear@0 3
nuclear@0 4 ReflAttrib::ReflAttrib()
nuclear@0 5 : value(1), color(1, 1, 1), map(0)
nuclear@0 6 {
nuclear@0 7 }
nuclear@0 8
nuclear@0 9 ReflAttrib::ReflAttrib(const Color &col, Texture *tex)
nuclear@0 10 {
nuclear@0 11 set_color(col);
nuclear@0 12 map = tex;
nuclear@0 13 }
nuclear@0 14
nuclear@0 15 void ReflAttrib::set_value(float val)
nuclear@0 16 {
nuclear@0 17 value = val;
nuclear@0 18 color = Color{val, val, val};
nuclear@0 19 }
nuclear@0 20
nuclear@0 21 void ReflAttrib::set_color(const Color &col)
nuclear@0 22 {
nuclear@0 23 color = col;
nuclear@0 24 value = color_luminance(col);
nuclear@0 25 }
nuclear@0 26
nuclear@0 27 void ReflAttrib::set_map(Texture *tex)
nuclear@0 28 {
nuclear@0 29 map = tex;
nuclear@0 30 }
nuclear@0 31
nuclear@0 32 Texture *ReflAttrib::get_map() const
nuclear@0 33 {
nuclear@0 34 return map;
nuclear@0 35 }
nuclear@0 36
nuclear@0 37 float ReflAttrib::get_value() const
nuclear@0 38 {
nuclear@0 39 return value;
nuclear@0 40 }
nuclear@0 41
nuclear@0 42 float ReflAttrib::get_value(float u, float v) const
nuclear@0 43 {
nuclear@0 44 return map ? value * color_luminance(map->lookup(u, v)) : value;
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 const Color &ReflAttrib::get_color() const
nuclear@0 48 {
nuclear@0 49 return color;
nuclear@0 50 }
nuclear@0 51
nuclear@0 52 Color ReflAttrib::get_color(float u, float v) const
nuclear@0 53 {
nuclear@0 54 return map ? color * map->lookup(u, v) : color;
nuclear@0 55 }
nuclear@0 56
nuclear@0 57 // ---- class Reflectance ----
nuclear@0 58 ReflAttrib Reflectance::def_attrib;
nuclear@0 59
nuclear@0 60 Reflectance::Reflectance()
nuclear@0 61 {
nuclear@0 62 set_default_attribs();
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 void Reflectance::set_attrib(const char *name, const Color &color, Texture *tex)
nuclear@0 66 {
nuclear@0 67 attrib[name] = ReflAttrib{color, tex};
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 ReflAttrib &Reflectance::get_attrib(const char *name)
nuclear@0 71 {
nuclear@0 72 auto it = attrib.find(name);
nuclear@0 73 if(it == attrib.end()) {
nuclear@0 74 return def_attrib;
nuclear@0 75 }
nuclear@0 76 return it->second;
nuclear@0 77 }
nuclear@0 78
nuclear@0 79 const ReflAttrib &Reflectance::get_attrib(const char *name) const
nuclear@0 80 {
nuclear@0 81 auto it = attrib.find(name);
nuclear@0 82 if(it == attrib.end()) {
nuclear@0 83 return def_attrib;
nuclear@0 84 }
nuclear@0 85 return it->second;
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 float Reflectance::get_attrib_value(const char *name) const
nuclear@0 89 {
nuclear@0 90 return get_attrib(name).get_value();
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 float Reflectance::get_attrib_value(const char *name, float u, float v) const
nuclear@0 94 {
nuclear@0 95 return get_attrib(name).get_value(u, v);
nuclear@0 96 }
nuclear@0 97
nuclear@0 98 Color Reflectance::get_attrib_color(const char *name) const
nuclear@0 99 {
nuclear@0 100 return get_attrib(name).get_color();
nuclear@0 101 }
nuclear@0 102
nuclear@0 103 Color Reflectance::get_attrib_color(const char *name, float u, float v) const
nuclear@0 104 {
nuclear@0 105 return get_attrib(name).get_color(u, v);
nuclear@0 106 }
nuclear@0 107
nuclear@0 108 float Reflectance::sample(const Vector3 &norm, const Vector3 &outdir, Vector3 *indir) const
nuclear@0 109 {
nuclear@0 110 *indir = sample_dir(norm, outdir);
nuclear@0 111 return eval(norm, outdir, *indir);
nuclear@0 112 }
nuclear@0 113
nuclear@0 114 // --- class LambertRefl ---
nuclear@0 115
nuclear@0 116 void LambertRefl::set_default_attribs()
nuclear@0 117 {
nuclear@0 118 set_attrib("color", Color(1, 1, 1));
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 Vector3 LambertRefl::sample_dir(const Vector3 &norm, const Vector3 &outdir) const
nuclear@0 122 {
nuclear@0 123 Vector3 dir = Vector3{randf(-1, 1), randf(-1, 1), randf(-1, 1)}.normalized();
nuclear@0 124 return dot_product(dir, norm) < 0.0 ? -dir : dir;
nuclear@0 125 }
nuclear@0 126
nuclear@0 127 float LambertRefl::eval(const Vector3 &norm, const Vector3 &outdir, const Vector3 &indir) const
nuclear@0 128 {
nuclear@0 129 return dot_product(norm, outdir);
nuclear@0 130 }