]> git.tdb.fi Git - libs/gl.git/blob - source/material.cpp
Move the reflectivity parameter to material
[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::update_parameter(int mask) const
19 {
20         if(cur_obj!=this)
21                 return;
22
23         if(mask&AMBIENT)
24                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r);
25         if(mask&DIFFUSE)
26                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r);
27         if(mask&SPECULAR)
28                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r);
29         if(mask&EMISSION)
30                 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &emission.r);
31         if(mask&SHININESS)
32                 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
33 }
34
35 void Material::set_ambient(const Color &a)
36 {
37         ambient = a;
38         shdata.uniform("material.ambient", ambient);
39         update_parameter(AMBIENT);
40 }
41
42 void Material::set_diffuse(const Color &d)
43 {
44         diffuse = d;
45         shdata.uniform("material.diffuse", diffuse);
46         update_parameter(DIFFUSE);
47 }
48
49 void Material::set_specular(const Color &s)
50 {
51         specular = s;
52         shdata.uniform("material.specular", specular);
53         update_parameter(SPECULAR);
54 }
55
56 void Material::set_emission(const Color &e)
57 {
58         emission = e;
59         shdata.uniform("material.emission", emission);
60         update_parameter(EMISSION);
61 }
62
63 void Material::set_shininess(float s)
64 {
65         shininess = s;
66         shdata.uniform("material.shininess", shininess);
67         update_parameter(SHININESS);
68 }
69
70 void Material::set_reflectivity(float r)
71 {
72         reflectivity = r;
73         shdata.uniform("reflectivity", reflectivity);
74 }
75
76 void Material::bind() const
77 {
78         if(set_current(this))
79                 update_parameter(-1);
80 }
81
82
83 Material::Loader::Loader(Material &m):
84         DataFile::CollectionObjectLoader<Material>(m, 0)
85 {
86         init();
87 }
88
89 Material::Loader::Loader(Material &m, Collection &c):
90         DataFile::CollectionObjectLoader<Material>(m, &c)
91 {
92         init();
93 }
94
95 void Material::Loader::init()
96 {
97         if(Resources *res = dynamic_cast<Resources *>(coll))
98                 srgb = res->get_srgb_conversion();
99         else
100                 srgb = false;
101
102         add("ambient",   &Loader::ambient);
103         add("diffuse",   &Loader::diffuse);
104         add("specular",  &Loader::specular);
105         add("emission",  &Loader::emission);
106         add("shininess", &Loader::shininess);
107         add("reflectivity", &Loader::reflectivity);
108 }
109
110 Color Material::Loader::make_color(float r, float g, float b, float a)
111 {
112         Color c(r, g, b, a);
113         if(srgb)
114                 c = c.to_linear();
115         return c;
116 }
117
118 void Material::Loader::ambient(float r, float g, float b, float a)
119 {
120         obj.set_ambient(make_color(r, g, b, a));
121 }
122
123 void Material::Loader::diffuse(float r, float g, float b, float a)
124 {
125         obj.set_diffuse(make_color(r, g, b, a));
126 }
127
128 void Material::Loader::specular(float r, float g, float b, float a)
129 {
130         obj.set_specular(make_color(r, g, b, a));
131 }
132
133 void Material::Loader::emission(float r, float g, float b, float a)
134 {
135         obj.set_emission(make_color(r, g, b, a));
136 }
137
138 void Material::Loader::shininess(float s)
139 {
140         obj.set_shininess(s);
141 }
142
143 void Material::Loader::reflectivity(float r)
144 {
145         obj.set_reflectivity(r);
146 }
147
148 } // namespace GL
149 } // namespace Msp