erebus

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