]> git.tdb.fi Git - libs/gl.git/blob - source/materials/basicmaterial.cpp
Use specialization constants in the builtin material shaders
[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 BasicMaterial::BasicMaterial():
9         receive_shadows(false)
10 {
11         set_diffuse(Color(1.0f));
12         set_specular(Color(0.0f));
13         set_emission(Color(0.0f));
14         set_shininess(50.0f);
15         set_reflectivity(0.0f);
16 }
17
18 void BasicMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
19 {
20         module_name = "phong.glsl";
21         spec_values["use_diffuse_map"] = (diffuse.texture!=0);
22         bool use_specular = (specular.texture || specular.value.r || specular.value.g || specular.value.b);
23         spec_values["use_specular"] = use_specular;
24         spec_values["use_specular_map"] = (specular.texture!=0);
25         spec_values["use_shininess_map"] = (use_specular && shininess.texture!=0);
26         spec_values["use_normal_map"] = (normal.texture!=0);
27         bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
28         spec_values["use_emission"] = use_emission;
29         spec_values["use_emission_map"] = (emission.texture!=0);
30         spec_values["use_reflectivity"] = (reflectivity.value!=0 || reflectivity.texture!=0);
31         spec_values["use_reflectivity_map"] = (reflectivity.texture!=0);
32         spec_values["use_shadow_map"] = receive_shadows;
33 }
34
35 void BasicMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
36 {
37         attach_texture_to(diffuse.texture, texturing, tex_shdata, "diffuse_map");
38         attach_texture_to(specular.texture, texturing, tex_shdata, "specular_map");
39         attach_texture_to(normal.texture, texturing, tex_shdata, "normal_map");
40         attach_texture_to(emission.texture, texturing, tex_shdata, "emission_map");
41         attach_texture_to(shininess.texture, texturing, tex_shdata, "shininess_map");
42         attach_texture_to(reflectivity.texture, texturing, tex_shdata, "reflectivity_map");
43 }
44
45 void BasicMaterial::set_diffuse(const Color &color)
46 {
47         diffuse.value = color;
48         shdata.uniform("basic_material.diffuse", color);
49 }
50
51 void BasicMaterial::set_diffuse_map(const Texture *tex)
52 {
53         diffuse.texture = tex;
54 }
55
56 void BasicMaterial::set_specular(const Color &color)
57 {
58         specular.value = color;
59         shdata.uniform("basic_material.specular", color);
60 }
61
62 void BasicMaterial::set_specular_map(const Texture *tex)
63 {
64         specular.texture = tex;
65 }
66
67 void BasicMaterial::set_normal_map(const Texture *tex)
68 {
69         normal.texture = tex;
70 }
71
72 void BasicMaterial::set_emission(const Color &color)
73 {
74         emission.value = color;
75         shdata.uniform("basic_material.emission", color);
76 }
77
78 void BasicMaterial::set_emission_map(const Texture *tex)
79 {
80         emission.texture = tex;
81 }
82
83 void BasicMaterial::set_shininess(float value)
84 {
85         shininess.value = value;
86         shdata.uniform("basic_material.shininess", value);
87 }
88
89 void BasicMaterial::set_shininess_map(const Texture *tex)
90 {
91         shininess.texture = tex;
92 }
93
94 void BasicMaterial::set_reflectivity(float value)
95 {
96         reflectivity.value = value;
97         shdata.uniform("basic_material.reflectivity", value);
98 }
99
100 void BasicMaterial::set_reflectivity_map(const Texture *tex)
101 {
102         reflectivity.texture = tex;
103 }
104
105 void BasicMaterial::set_receive_shadows(bool s)
106 {
107         receive_shadows = s;
108 }
109
110
111 DataFile::Loader::ActionMap BasicMaterial::Loader::shared_actions;
112
113 BasicMaterial::Loader::Loader(BasicMaterial &m):
114         DerivedObjectLoader<BasicMaterial, Material::PropertyLoader<BasicMaterial> >(m)
115 {
116         set_actions(shared_actions);
117 }
118
119 BasicMaterial::Loader::Loader(BasicMaterial &m, Collection &c):
120         DerivedObjectLoader<BasicMaterial, Material::PropertyLoader<BasicMaterial> >(m, c)
121 {
122         set_actions(shared_actions);
123 }
124
125 void BasicMaterial::Loader::init_actions()
126 {
127         Material::PropertyLoader<BasicMaterial>::init_actions();
128         add_property("diffuse", &BasicMaterial::set_diffuse, &BasicMaterial::set_diffuse_map, true);
129         add_property("specular", &BasicMaterial::set_specular, &BasicMaterial::set_specular_map, false);
130         add_property("normal", &BasicMaterial::set_normal_map);
131         add_property("emission", &BasicMaterial::set_emission, &BasicMaterial::set_emission_map, false);
132         add_property("shininess", &BasicMaterial::set_shininess, &BasicMaterial::set_shininess_map);
133         add_property("reflectivity", &BasicMaterial::set_reflectivity, &BasicMaterial::set_reflectivity_map);
134         add("receive_shadows", &BasicMaterial::receive_shadows);
135 }
136
137 } // namespace GL
138 } // namespace Msp