]> git.tdb.fi Git - libs/gl.git/blob - source/materials/basicmaterial.cpp
30202a568f62a7964700f7f765cc3af84c3350f6
[libs/gl.git] / source / materials / basicmaterial.cpp
1 #include "basicmaterial.h"
2
3 using namespace std;
4
5 namespace Msp {
6 namespace GL {
7
8 const Tag BasicMaterial::texture_tags[] =
9 {
10         Tag("diffuse_map"),
11         Tag("specular_map"),
12         Tag("shininess_map"),
13         Tag("normal_map"),
14         Tag("emission_map"),
15         Tag("reflectivity_map"),
16         Tag()
17 };
18
19 BasicMaterial::BasicMaterial()
20 {
21         set_diffuse(Color(1.0f));
22         set_specular(Color(0.0f));
23         set_emission(Color(0.0f));
24         set_shininess(50.0f);
25         set_reflectivity(0.0f);
26 }
27
28 void BasicMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
29 {
30         module_name = "phong.glsl";
31         spec_values["use_diffuse_map"] = (diffuse.texture!=0);
32         bool use_specular = (specular.texture || specular.value.r || specular.value.g || specular.value.b);
33         spec_values["use_specular"] = use_specular;
34         spec_values["use_specular_map"] = (specular.texture!=0);
35         spec_values["use_shininess_map"] = (use_specular && shininess.texture!=0);
36         spec_values["use_normal_map"] = (normal.texture!=0);
37         bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
38         spec_values["use_emission"] = use_emission;
39         spec_values["use_emission_map"] = (emission.texture!=0);
40         spec_values["use_reflectivity"] = (reflectivity.value!=0 || reflectivity.texture!=0);
41         spec_values["use_reflectivity_map"] = (reflectivity.texture!=0);
42 }
43
44 #pragma GCC diagnostic push
45 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
46 void BasicMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
47 {
48         attach_texture_to(diffuse.texture, texturing, tex_shdata, "diffuse_map");
49         attach_texture_to(specular.texture, texturing, tex_shdata, "specular_map");
50         attach_texture_to(normal.texture, texturing, tex_shdata, "normal_map");
51         attach_texture_to(emission.texture, texturing, tex_shdata, "emission_map");
52         attach_texture_to(shininess.texture, texturing, tex_shdata, "shininess_map");
53         attach_texture_to(reflectivity.texture, texturing, tex_shdata, "reflectivity_map");
54 }
55 #pragma GCC diagnostic pop
56
57 const Texture *BasicMaterial::get_texture(Tag tag) const
58 {
59         if(tag==texture_tags[0])
60                 return diffuse.texture;
61         else if(tag==texture_tags[1])
62                 return specular.texture;
63         else if(tag==texture_tags[2])
64                 return shininess.texture;
65         else if(tag==texture_tags[3])
66                 return normal.texture;
67         else if(tag==texture_tags[4])
68                 return emission.texture;
69         else if(tag==texture_tags[5])
70                 return reflectivity.texture;
71         else
72                 return 0;
73 }
74
75 void BasicMaterial::set_diffuse(const Color &color)
76 {
77         diffuse.value = color;
78         shdata.uniform("basic_material.diffuse", color);
79 }
80
81 void BasicMaterial::set_diffuse_map(const Texture *tex)
82 {
83         diffuse.texture = tex;
84 }
85
86 void BasicMaterial::set_specular(const Color &color)
87 {
88         specular.value = color;
89         shdata.uniform("basic_material.specular", color);
90 }
91
92 void BasicMaterial::set_specular_map(const Texture *tex)
93 {
94         specular.texture = tex;
95 }
96
97 void BasicMaterial::set_normal_map(const Texture *tex)
98 {
99         normal.texture = tex;
100 }
101
102 void BasicMaterial::set_emission(const Color &color)
103 {
104         emission.value = color;
105         shdata.uniform("basic_material.emission", color);
106 }
107
108 void BasicMaterial::set_emission_map(const Texture *tex)
109 {
110         emission.texture = tex;
111 }
112
113 void BasicMaterial::set_shininess(float value)
114 {
115         shininess.value = value;
116         shdata.uniform("basic_material.shininess", value);
117 }
118
119 void BasicMaterial::set_shininess_map(const Texture *tex)
120 {
121         shininess.texture = tex;
122 }
123
124 void BasicMaterial::set_reflectivity(float value)
125 {
126         reflectivity.value = value;
127         shdata.uniform("basic_material.reflectivity", value);
128 }
129
130 void BasicMaterial::set_reflectivity_map(const Texture *tex)
131 {
132         reflectivity.texture = tex;
133 }
134
135
136 DataFile::Loader::ActionMap BasicMaterial::Loader::shared_actions;
137
138 BasicMaterial::Loader::Loader(BasicMaterial &m):
139         DerivedObjectLoader<BasicMaterial, Material::PropertyLoader<BasicMaterial> >(m)
140 {
141         set_actions(shared_actions);
142 }
143
144 BasicMaterial::Loader::Loader(BasicMaterial &m, Collection &c):
145         DerivedObjectLoader<BasicMaterial, Material::PropertyLoader<BasicMaterial> >(m, c)
146 {
147         set_actions(shared_actions);
148 }
149
150 void BasicMaterial::Loader::init_actions()
151 {
152         Material::PropertyLoader<BasicMaterial>::init_actions();
153         add_property("diffuse", &BasicMaterial::set_diffuse, &BasicMaterial::set_diffuse_map, true);
154         add_property("specular", &BasicMaterial::set_specular, &BasicMaterial::set_specular_map, false);
155         add_property("normal", &BasicMaterial::set_normal_map);
156         add_property("emission", &BasicMaterial::set_emission, &BasicMaterial::set_emission_map, false);
157         add_property("shininess", &BasicMaterial::set_shininess, &BasicMaterial::set_shininess_map);
158         add_property("reflectivity", &BasicMaterial::set_reflectivity, &BasicMaterial::set_reflectivity_map);
159 }
160
161 } // namespace GL
162 } // namespace Msp