erebus

annotate liberebus/src/brdf.cc @ 3:a932848de652

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