]> git.tdb.fi Git - libs/gl.git/blob - source/material.cpp
Remove support for legacy OpenGL features
[libs/gl.git] / source / material.cpp
1 #include "gl.h"
2 #include "material.h"
3 #include "resources.h"
4
5 namespace Msp {
6 namespace GL {
7
8 Material::Material()
9 {
10         set_ambient(0.2);
11         set_diffuse(0.8);
12         set_specular(0);
13         set_emission(0);
14         set_shininess(0);
15         set_reflectivity(0);
16 }
17
18 void Material::set_ambient(const Color &a)
19 {
20         ambient = a;
21         shdata.uniform("material.ambient", ambient);
22 }
23
24 void Material::set_diffuse(const Color &d)
25 {
26         diffuse = d;
27         shdata.uniform("material.diffuse", diffuse);
28 }
29
30 void Material::set_specular(const Color &s)
31 {
32         specular = s;
33         shdata.uniform("material.specular", specular);
34 }
35
36 void Material::set_emission(const Color &e)
37 {
38         emission = e;
39         shdata.uniform("material.emission", emission);
40 }
41
42 void Material::set_shininess(float s)
43 {
44         shininess = s;
45         shdata.uniform("material.shininess", shininess);
46 }
47
48 void Material::set_reflectivity(float r)
49 {
50         reflectivity = r;
51         shdata.uniform("reflectivity", reflectivity);
52 }
53
54
55 Material::Loader::Loader(Material &m):
56         DataFile::CollectionObjectLoader<Material>(m, 0)
57 {
58         init();
59 }
60
61 Material::Loader::Loader(Material &m, Collection &c):
62         DataFile::CollectionObjectLoader<Material>(m, &c)
63 {
64         init();
65 }
66
67 void Material::Loader::init()
68 {
69         if(Resources *res = dynamic_cast<Resources *>(coll))
70                 srgb = res->get_srgb_conversion();
71         else
72                 srgb = false;
73
74         add("ambient",   &Loader::ambient);
75         add("diffuse",   &Loader::diffuse);
76         add("specular",  &Loader::specular);
77         add("emission",  &Loader::emission);
78         add("shininess", &Loader::shininess);
79         add("reflectivity", &Loader::reflectivity);
80 }
81
82 Color Material::Loader::make_color(float r, float g, float b, float a)
83 {
84         Color c(r, g, b, a);
85         if(srgb)
86                 c = c.to_linear();
87         return c;
88 }
89
90 void Material::Loader::ambient(float r, float g, float b, float a)
91 {
92         obj.set_ambient(make_color(r, g, b, a));
93 }
94
95 void Material::Loader::diffuse(float r, float g, float b, float a)
96 {
97         obj.set_diffuse(make_color(r, g, b, a));
98 }
99
100 void Material::Loader::specular(float r, float g, float b, float a)
101 {
102         obj.set_specular(make_color(r, g, b, a));
103 }
104
105 void Material::Loader::emission(float r, float g, float b, float a)
106 {
107         obj.set_emission(make_color(r, g, b, a));
108 }
109
110 void Material::Loader::shininess(float s)
111 {
112         obj.set_shininess(s);
113 }
114
115 void Material::Loader::reflectivity(float r)
116 {
117         obj.set_reflectivity(r);
118 }
119
120 } // namespace GL
121 } // namespace Msp