]> git.tdb.fi Git - libs/gl.git/blob - source/material.cpp
Better state tracking for bound objects
[libs/gl.git] / source / material.cpp
1 #include "gl.h"
2 #include "material.h"
3
4 namespace Msp {
5 namespace GL {
6
7 Material::Material():
8         ambient(0.2),
9         diffuse(0.8),
10         specular(0),
11         emission(0),
12         shininess(0)
13 { }
14
15 void Material::update_parameter(int mask) const
16 {
17         if(cur_obj!=this)
18                 return;
19
20         if(mask&AMBIENT)
21                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r);
22         if(mask&DIFFUSE)
23                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r);
24         if(mask&SPECULAR)
25                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r);
26         if(mask&EMISSION)
27                 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &emission.r);
28         if(mask&SHININESS)
29                 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
30 }
31
32 void Material::set_ambient(const Color &a)
33 {
34         ambient = a;
35         update_parameter(AMBIENT);
36 }
37
38 void Material::set_diffuse(const Color &d)
39 {
40         diffuse = d;
41         update_parameter(DIFFUSE);
42 }
43
44 void Material::set_specular(const Color &s)
45 {
46         specular = s;
47         update_parameter(SPECULAR);
48 }
49
50 void Material::set_emission(const Color &e)
51 {
52         emission = e;
53         update_parameter(EMISSION);
54 }
55
56 void Material::set_shininess(float s)
57 {
58         shininess = s;
59         update_parameter(SHININESS);
60 }
61
62 void Material::bind() const
63 {
64         if(set_current(this))
65                 update_parameter(-1);
66 }
67
68
69 Material::Loader::Loader(Material &m):
70         DataFile::ObjectLoader<Material>(m)
71 {
72         add("ambient",   &Loader::ambient);
73         add("diffuse",   &Loader::diffuse);
74         add("specular",  &Loader::specular);
75         add("emission",  &Loader::emission);
76         add("shininess", &Material::shininess);
77 }
78
79 void Material::Loader::ambient(float r, float g, float b, float a)
80 {
81         obj.ambient = GL::Color(r, g, b, a);
82 }
83
84 void Material::Loader::diffuse(float r, float g, float b, float a)
85 {
86         obj.diffuse = GL::Color(r, g, b, a);
87 }
88
89 void Material::Loader::specular(float r, float g, float b, float a)
90 {
91         obj.specular = GL::Color(r, g, b, a);
92 }
93
94 void Material::Loader::emission(float r, float g, float b, float a)
95 {
96         obj.emission = GL::Color(r, g, b, a);
97 }
98
99 } // namespace GL
100 } // namespace Msp