]> git.tdb.fi Git - libs/gl.git/blob - source/materials/pbrmaterial.cpp
Implement image-based lighting in PbrMaterial
[libs/gl.git] / source / materials / pbrmaterial.cpp
1 #include "framebuffer.h"
2 #include "mesh.h"
3 #include "pbrmaterial.h"
4 #include "renderer.h"
5 #include "resources.h"
6 #include "texture2d.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 const Tag PbrMaterial::texture_tags[] =
14 {
15         Tag("base_color_map"),
16         Tag("normal_map"),
17         Tag("metalness_map"),
18         Tag("roughness_map"),
19         Tag("occlusion_map"),
20         Tag("emission_map"),
21         Tag("fresnel_lookup"),
22         Tag()
23 };
24
25 PbrMaterial::PbrMaterial():
26         fresnel_lookup(get_or_create_fresnel_lookup())
27 {
28         set_base_color(0.8f);
29         set_metalness(0.0f);
30         set_roughness(0.5f);
31         set_emission(0.0f);
32 }
33
34 const Texture2D &PbrMaterial::get_or_create_fresnel_lookup()
35 {
36         Resources &resources = Resources::get_global();
37
38         static const string name = "_pbr_env_fresnel_lookup.tex2d";
39         Texture2D *fresnel_lookup = resources.find<Texture2D>(name);
40         if(fresnel_lookup)
41                 return *fresnel_lookup;
42
43         fresnel_lookup = new Texture2D;
44         fresnel_lookup->storage(RG8, 128, 128, 1);
45         resources.add(name, fresnel_lookup);
46
47         const Program &shprog = resources.get<Program>("_pbr_fresnel_lookup.glsl.shader");
48         ProgramData shdata;
49         shdata.uniform("n_samples", 1024);
50         // Not actually used here, but put it in to satisfy the interface
51         shdata.uniform("roughness", 0.0f);
52
53         const Mesh &mesh = resources.get<Mesh>("_fullscreen_quad.mesh");
54         Framebuffer fresnel_lookup_fbo;
55         fresnel_lookup_fbo.attach(COLOR_ATTACHMENT0, *fresnel_lookup);
56         Bind bind_fbo(fresnel_lookup_fbo);
57         Renderer renderer;
58         renderer.set_shader_program(&shprog, &shdata);
59         mesh.draw(renderer);
60
61         return *fresnel_lookup;
62 }
63
64 void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
65 {
66         module_name = "cooktorrance.glsl";
67         spec_values["use_base_color_map"] = (base_color.texture!=0);
68         spec_values["use_normal_map"] = (normal.texture!=0);
69         spec_values["use_metalness_map"] = (metalness.texture!=0);
70         spec_values["use_roughness_map"] = (roughness.texture!=0);
71         spec_values["use_occlusion_map"] = (occlusion.texture!=0);
72         bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
73         spec_values["use_emission"] = use_emission;
74         spec_values["use_emission_map"] = (emission.texture!=0);
75 }
76
77 #pragma GCC diagnostic push
78 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
79 void PbrMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
80 {
81         attach_texture_to(base_color.texture, texturing, tex_shdata, "base_color_map");
82         attach_texture_to(metalness.texture, texturing, tex_shdata, "metalness_map");
83         attach_texture_to(roughness.texture, texturing, tex_shdata, "roughness_map");
84         attach_texture_to(normal.texture, texturing, tex_shdata, "normal_map");
85         attach_texture_to(occlusion.texture, texturing, tex_shdata, "occlusion_map");
86         attach_texture_to(emission.texture, texturing, tex_shdata, "emission_map");
87 }
88 #pragma GCC diagnostic pop
89
90 const Texture *PbrMaterial::get_texture(Tag tag) const
91 {
92         if(tag==texture_tags[0])
93                 return base_color.texture;
94         else if(tag==texture_tags[1])
95                 return normal.texture;
96         else if(tag==texture_tags[2])
97                 return metalness.texture;
98         else if(tag==texture_tags[3])
99                 return roughness.texture;
100         else if(tag==texture_tags[4])
101                 return occlusion.texture;
102         else if(tag==texture_tags[5])
103                 return emission.texture;
104         else if(tag==texture_tags[6])
105                 return &fresnel_lookup;
106         else
107                 return 0;
108 }
109
110 void PbrMaterial::set_base_color(const Color &color)
111 {
112         base_color.value = color;
113         shdata.uniform("pbr_material.base_color", color);
114 }
115
116 void PbrMaterial::set_base_color_map(const Texture *tex)
117 {
118         base_color.texture = tex;
119 }
120
121 void PbrMaterial::set_normal_map(const Texture *tex)
122 {
123         normal.texture = tex;
124 }
125
126 void PbrMaterial::set_metalness(float value)
127 {
128         metalness.value = value;
129         shdata.uniform("pbr_material.metalness", value);
130 }
131
132 void PbrMaterial::set_metalness_map(const Texture *tex)
133 {
134         metalness.texture = tex;
135 }
136
137 void PbrMaterial::set_roughness(float value)
138 {
139         roughness.value = value;
140         shdata.uniform("pbr_material.roughness", value);
141 }
142
143 void PbrMaterial::set_roughness_map(const Texture *tex)
144 {
145         roughness.texture = tex;
146 }
147
148 void PbrMaterial::set_occlusion_map(const Texture *tex)
149 {
150         occlusion.texture = tex;
151 }
152
153 void PbrMaterial::set_emission(const Color &color)
154 {
155         emission.value = color;
156         shdata.uniform("pbr_material.emission", color);
157 }
158
159 void PbrMaterial::set_emission_map(const Texture *tex)
160 {
161         emission.texture = tex;
162 }
163
164
165 DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions;
166
167 PbrMaterial::Loader::Loader(PbrMaterial &m):
168         DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m)
169 {
170         set_actions(shared_actions);
171 }
172
173 PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c):
174         DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m, c)
175 {
176         set_actions(shared_actions);
177 }
178
179 void PbrMaterial::Loader::init_actions()
180 {
181         Material::PropertyLoader<PbrMaterial>::init_actions();
182         add_property("base_color", &PbrMaterial::set_base_color, &PbrMaterial::set_base_color_map, true);
183         add_property("normal", &PbrMaterial::set_normal_map);
184         add_property("metalness", &PbrMaterial::set_metalness, &PbrMaterial::set_metalness_map);
185         add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map);
186         add_property("occlusion", &PbrMaterial::set_occlusion_map);
187         add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false);
188 }
189
190 } // namespace GL
191 } // namespace Msp