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