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