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