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