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