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