]> git.tdb.fi Git - libs/gl.git/blob - source/material.cpp
Homogenise binding semantics
[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::set_ambient(const Color &a)
16 {
17         ambient = a;
18 }
19
20 void Material::set_diffuse(const Color &d)
21 {
22         diffuse = d;
23 }
24
25 void Material::set_specular(const Color &s)
26 {
27         specular = s;
28 }
29
30 void Material::set_emission(const Color &e)
31 {
32         emission = e;
33 }
34
35 void Material::set_shininess(float s)
36 {
37         shininess = s;
38 }
39
40 void Material::bind() const
41 {
42         if(set_current(this))
43         {
44                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r);
45                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r);
46                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r);
47                 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &emission.r);
48                 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
49         }
50 }
51
52
53 Material::Loader::Loader(Material &m):
54         DataFile::ObjectLoader<Material>(m)
55 {
56         add("ambient",   &Loader::ambient);
57         add("diffuse",   &Loader::diffuse);
58         add("specular",  &Loader::specular);
59         add("emission",  &Loader::emission);
60         add("shininess", &Material::shininess);
61 }
62
63 void Material::Loader::ambient(float r, float g, float b, float a)
64 {
65         obj.ambient = GL::Color(r, g, b, a);
66 }
67
68 void Material::Loader::diffuse(float r, float g, float b, float a)
69 {
70         obj.diffuse = GL::Color(r, g, b, a);
71 }
72
73 void Material::Loader::specular(float r, float g, float b, float a)
74 {
75         obj.specular = GL::Color(r, g, b, a);
76 }
77
78 void Material::Loader::emission(float r, float g, float b, float a)
79 {
80         obj.emission = GL::Color(r, g, b, a);
81 }
82
83 } // namespace GL
84 } // namespace Msp