erebus

annotate liberebus/src/material.cc @ 10:506e114b7ca2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 24 May 2014 17:43:46 +0300
parents 93894c232d65
children
rev   line source
nuclear@4 1 #include "material.h"
nuclear@4 2
nuclear@4 3 MatAttrib::MatAttrib()
nuclear@8 4 : value(0), color(0, 0, 0), map(0)
nuclear@4 5 {
nuclear@4 6 }
nuclear@4 7
nuclear@4 8 MatAttrib::MatAttrib(const Color &col, Texture *tex)
nuclear@4 9 {
nuclear@4 10 set_color(col);
nuclear@4 11 map = tex;
nuclear@4 12 }
nuclear@4 13
nuclear@4 14 void MatAttrib::set_value(float val)
nuclear@4 15 {
nuclear@4 16 value = val;
nuclear@4 17 color = Color{val, val, val};
nuclear@4 18 }
nuclear@4 19
nuclear@4 20 void MatAttrib::set_color(const Color &col)
nuclear@4 21 {
nuclear@4 22 color = col;
nuclear@4 23 value = color_luminance(col);
nuclear@4 24 }
nuclear@4 25
nuclear@4 26 void MatAttrib::set_map(Texture *tex)
nuclear@4 27 {
nuclear@4 28 map = tex;
nuclear@4 29 }
nuclear@4 30
nuclear@4 31 Texture *MatAttrib::get_map() const
nuclear@4 32 {
nuclear@4 33 return map;
nuclear@4 34 }
nuclear@4 35
nuclear@4 36 float MatAttrib::get_value() const
nuclear@4 37 {
nuclear@4 38 return value;
nuclear@4 39 }
nuclear@4 40
nuclear@4 41 float MatAttrib::get_value(float u, float v) const
nuclear@4 42 {
nuclear@4 43 return map ? value * color_luminance(map->lookup(u, v)) : value;
nuclear@4 44 }
nuclear@4 45
nuclear@4 46 const Color &MatAttrib::get_color() const
nuclear@4 47 {
nuclear@4 48 return color;
nuclear@4 49 }
nuclear@4 50
nuclear@4 51 Color MatAttrib::get_color(float u, float v) const
nuclear@4 52 {
nuclear@4 53 return map ? color * map->lookup(u, v) : color;
nuclear@4 54 }
nuclear@4 55
nuclear@4 56
nuclear@4 57 // --- class Material ---
nuclear@4 58
nuclear@4 59 MatAttrib Material::def_attrib;
nuclear@4 60
nuclear@4 61 Material::Material()
nuclear@4 62 {
nuclear@4 63 }
nuclear@4 64
nuclear@4 65 void Material::set_attrib(const char *name, const Color &color, Texture *tex)
nuclear@4 66 {
nuclear@4 67 attrib[name] = MatAttrib{color, tex};
nuclear@4 68 }
nuclear@4 69
nuclear@4 70 MatAttrib &Material::get_attrib(const char *name)
nuclear@4 71 {
nuclear@4 72 auto it = attrib.find(name);
nuclear@4 73 if(it == attrib.end()) {
nuclear@4 74 return def_attrib;
nuclear@4 75 }
nuclear@4 76 return it->second;
nuclear@4 77 }
nuclear@4 78
nuclear@4 79 const MatAttrib &Material::get_attrib(const char *name) const
nuclear@4 80 {
nuclear@4 81 auto it = attrib.find(name);
nuclear@4 82 if(it == attrib.end()) {
nuclear@4 83 return def_attrib;
nuclear@4 84 }
nuclear@4 85 return it->second;
nuclear@4 86 }
nuclear@4 87
nuclear@4 88 float Material::get_attrib_value(const char *name) const
nuclear@4 89 {
nuclear@4 90 return get_attrib(name).get_value();
nuclear@4 91 }
nuclear@4 92
nuclear@4 93 float Material::get_attrib_value(const char *name, float u, float v) const
nuclear@4 94 {
nuclear@4 95 return get_attrib(name).get_value(u, v);
nuclear@4 96 }
nuclear@4 97
nuclear@4 98 Color Material::get_attrib_color(const char *name) const
nuclear@4 99 {
nuclear@4 100 return get_attrib(name).get_color();
nuclear@4 101 }
nuclear@4 102
nuclear@4 103 Color Material::get_attrib_color(const char *name, float u, float v) const
nuclear@4 104 {
nuclear@4 105 return get_attrib(name).get_color(u, v);
nuclear@4 106 }
nuclear@4 107