erebus
view liberebus/src/material.cc @ 4:93894c232d65
more changes across the board
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 29 Apr 2014 07:38:40 +0300 |
parents | |
children | e2d9bf168a41 |
line source
1 #include "material.h"
3 MatAttrib::MatAttrib()
4 : value(1), color(1, 1, 1), map(0)
5 {
6 }
8 MatAttrib::MatAttrib(const Color &col, Texture *tex)
9 {
10 set_color(col);
11 map = tex;
12 }
14 void MatAttrib::set_value(float val)
15 {
16 value = val;
17 color = Color{val, val, val};
18 }
20 void MatAttrib::set_color(const Color &col)
21 {
22 color = col;
23 value = color_luminance(col);
24 }
26 void MatAttrib::set_map(Texture *tex)
27 {
28 map = tex;
29 }
31 Texture *MatAttrib::get_map() const
32 {
33 return map;
34 }
36 float MatAttrib::get_value() const
37 {
38 return value;
39 }
41 float MatAttrib::get_value(float u, float v) const
42 {
43 return map ? value * color_luminance(map->lookup(u, v)) : value;
44 }
46 const Color &MatAttrib::get_color() const
47 {
48 return color;
49 }
51 Color MatAttrib::get_color(float u, float v) const
52 {
53 return map ? color * map->lookup(u, v) : color;
54 }
57 // --- class Material ---
59 MatAttrib Material::def_attrib;
61 Material::Material()
62 {
63 }
65 void Material::set_attrib(const char *name, const Color &color, Texture *tex)
66 {
67 attrib[name] = MatAttrib{color, tex};
68 }
70 MatAttrib &Material::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 MatAttrib &Material::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 Material::get_attrib_value(const char *name) const
89 {
90 return get_attrib(name).get_value();
91 }
93 float Material::get_attrib_value(const char *name, float u, float v) const
94 {
95 return get_attrib(name).get_value(u, v);
96 }
98 Color Material::get_attrib_color(const char *name) const
99 {
100 return get_attrib(name).get_color();
101 }
103 Color Material::get_attrib_color(const char *name, float u, float v) const
104 {
105 return get_attrib(name).get_color(u, v);
106 }