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