erebus
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/liberebus/src/material.cc Tue Apr 29 07:38:40 2014 +0300 1.3 @@ -0,0 +1,107 @@ 1.4 +#include "material.h" 1.5 + 1.6 +MatAttrib::MatAttrib() 1.7 + : value(1), color(1, 1, 1), map(0) 1.8 +{ 1.9 +} 1.10 + 1.11 +MatAttrib::MatAttrib(const Color &col, Texture *tex) 1.12 +{ 1.13 + set_color(col); 1.14 + map = tex; 1.15 +} 1.16 + 1.17 +void MatAttrib::set_value(float val) 1.18 +{ 1.19 + value = val; 1.20 + color = Color{val, val, val}; 1.21 +} 1.22 + 1.23 +void MatAttrib::set_color(const Color &col) 1.24 +{ 1.25 + color = col; 1.26 + value = color_luminance(col); 1.27 +} 1.28 + 1.29 +void MatAttrib::set_map(Texture *tex) 1.30 +{ 1.31 + map = tex; 1.32 +} 1.33 + 1.34 +Texture *MatAttrib::get_map() const 1.35 +{ 1.36 + return map; 1.37 +} 1.38 + 1.39 +float MatAttrib::get_value() const 1.40 +{ 1.41 + return value; 1.42 +} 1.43 + 1.44 +float MatAttrib::get_value(float u, float v) const 1.45 +{ 1.46 + return map ? value * color_luminance(map->lookup(u, v)) : value; 1.47 +} 1.48 + 1.49 +const Color &MatAttrib::get_color() const 1.50 +{ 1.51 + return color; 1.52 +} 1.53 + 1.54 +Color MatAttrib::get_color(float u, float v) const 1.55 +{ 1.56 + return map ? color * map->lookup(u, v) : color; 1.57 +} 1.58 + 1.59 + 1.60 +// --- class Material --- 1.61 + 1.62 +MatAttrib Material::def_attrib; 1.63 + 1.64 +Material::Material() 1.65 +{ 1.66 +} 1.67 + 1.68 +void Material::set_attrib(const char *name, const Color &color, Texture *tex) 1.69 +{ 1.70 + attrib[name] = MatAttrib{color, tex}; 1.71 +} 1.72 + 1.73 +MatAttrib &Material::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 MatAttrib &Material::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 Material::get_attrib_value(const char *name) const 1.92 +{ 1.93 + return get_attrib(name).get_value(); 1.94 +} 1.95 + 1.96 +float Material::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 Material::get_attrib_color(const char *name) const 1.102 +{ 1.103 + return get_attrib(name).get_color(); 1.104 +} 1.105 + 1.106 +Color Material::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 +