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