]> git.tdb.fi Git - libs/gl.git/blob - source/materials/pbrmaterial.cpp
Use specialization constants in the builtin material shaders
[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 PbrMaterial::PbrMaterial():
9         receive_shadows(false)
10 {
11         set_base_color(0.8f);
12         set_metalness(0.0f);
13         set_roughness(0.5f);
14         set_emission(0.0f);
15 }
16
17 void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
18 {
19         module_name = "cooktorrance.glsl";
20         spec_values["use_base_color_map"] = (base_color.texture!=0);
21         spec_values["use_normal_map"] = (normal.texture!=0);
22         spec_values["use_metalness_map"] = (metalness.texture!=0);
23         spec_values["use_roughness_map"] = (roughness.texture!=0);
24         spec_values["use_occlusion_map"] = (occlusion.texture!=0);
25         bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
26         spec_values["use_emission"] = use_emission;
27         spec_values["use_emission_map"] = (emission.texture!=0);
28         spec_values["use_shadow_map"] = receive_shadows;
29 }
30
31 void PbrMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
32 {
33         attach_texture_to(base_color.texture, texturing, tex_shdata, "base_color_map");
34         attach_texture_to(metalness.texture, texturing, tex_shdata, "metalness_map");
35         attach_texture_to(roughness.texture, texturing, tex_shdata, "roughness_map");
36         attach_texture_to(normal.texture, texturing, tex_shdata, "normal_map");
37         attach_texture_to(occlusion.texture, texturing, tex_shdata, "occlusion_map");
38         attach_texture_to(emission.texture, texturing, tex_shdata, "emission_map");
39 }
40
41 void PbrMaterial::set_base_color(const Color &color)
42 {
43         base_color.value = color;
44         shdata.uniform("pbr_material.base_color", color);
45 }
46
47 void PbrMaterial::set_base_color_map(const Texture *tex)
48 {
49         base_color.texture = tex;
50 }
51
52 void PbrMaterial::set_normal_map(const Texture *tex)
53 {
54         normal.texture = tex;
55 }
56
57 void PbrMaterial::set_metalness(float value)
58 {
59         metalness.value = value;
60         shdata.uniform("pbr_material.metalness", value);
61 }
62
63 void PbrMaterial::set_metalness_map(const Texture *tex)
64 {
65         metalness.texture = tex;
66 }
67
68 void PbrMaterial::set_roughness(float value)
69 {
70         roughness.value = value;
71         shdata.uniform("pbr_material.roughness", value);
72 }
73
74 void PbrMaterial::set_roughness_map(const Texture *tex)
75 {
76         roughness.texture = tex;
77 }
78
79 void PbrMaterial::set_occlusion_map(const Texture *tex)
80 {
81         occlusion.texture = tex;
82 }
83
84 void PbrMaterial::set_emission(const Color &color)
85 {
86         emission.value = color;
87         shdata.uniform("pbr_material.emission", color);
88 }
89
90 void PbrMaterial::set_emission_map(const Texture *tex)
91 {
92         emission.texture = tex;
93 }
94
95 void PbrMaterial::set_receive_shadows(bool s)
96 {
97         receive_shadows = s;
98 }
99
100
101 DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions;
102
103 PbrMaterial::Loader::Loader(PbrMaterial &m):
104         DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m)
105 {
106         set_actions(shared_actions);
107 }
108
109 PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c):
110         DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m, c)
111 {
112         set_actions(shared_actions);
113 }
114
115 void PbrMaterial::Loader::init_actions()
116 {
117         Material::PropertyLoader<PbrMaterial>::init_actions();
118         add_property("base_color", &PbrMaterial::set_base_color, &PbrMaterial::set_base_color_map, true);
119         add_property("normal", &PbrMaterial::set_normal_map);
120         add_property("metalness", &PbrMaterial::set_metalness, &PbrMaterial::set_metalness_map);
121         add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map);
122         add_property("occlusion", &PbrMaterial::set_occlusion_map);
123         add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false);
124         add("receive_shadows", &PbrMaterial::receive_shadows);
125 }
126
127 } // namespace GL
128 } // namespace Msp