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