]> git.tdb.fi Git - libs/gl.git/blobdiff - source/materials/pbrmaterial.cpp
Rearrange soucre files into subdirectories
[libs/gl.git] / source / materials / pbrmaterial.cpp
diff --git a/source/materials/pbrmaterial.cpp b/source/materials/pbrmaterial.cpp
new file mode 100644 (file)
index 0000000..bc7104c
--- /dev/null
@@ -0,0 +1,138 @@
+#include "pbrmaterial.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+PbrMaterial::PbrMaterial():
+       receive_shadows(false)
+{
+       set_base_color(0.8f);
+       set_metalness(0.0f);
+       set_roughness(0.5f);
+       set_emission(0.0f);
+}
+
+string PbrMaterial::create_program_source() const
+{
+       string source = "import cooktorrance;\n";
+       if(base_color.texture)
+               source += "const bool use_base_color_map = true;\n";
+       if(normal.texture)
+               source += "const bool use_normal_map = true;\n";
+       if(metalness.texture)
+               source += "const bool use_metalness_map = true;\n";
+       if(roughness.texture)
+               source += "const bool use_roughness_map = true;\n";
+       if(occlusion.texture)
+               source += "const bool use_occlusion_map = true;\n";
+       if(emission.texture || emission.value.r || emission.value.g || emission.value.b)
+       {
+               source += "const bool use_emission = true;\n";
+               if(emission.texture)
+                       source += "const bool use_emission_map = true;\n";
+       }
+       if(receive_shadows)
+               source += "const bool use_shadow_map = true;\n";
+       return source;
+}
+
+void PbrMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
+{
+       attach_texture_to(base_color.texture, texturing, tex_shdata, "base_color_map");
+       attach_texture_to(metalness.texture, texturing, tex_shdata, "metalness_map");
+       attach_texture_to(roughness.texture, texturing, tex_shdata, "roughness_map");
+       attach_texture_to(normal.texture, texturing, tex_shdata, "normal_map");
+       attach_texture_to(occlusion.texture, texturing, tex_shdata, "occlusion_map");
+       attach_texture_to(emission.texture, texturing, tex_shdata, "emission_map");
+}
+
+void PbrMaterial::set_base_color(const Color &color)
+{
+       base_color.value = color;
+       shdata.uniform("pbr_material.base_color", color);
+}
+
+void PbrMaterial::set_base_color_map(const Texture *tex)
+{
+       base_color.texture = tex;
+}
+
+void PbrMaterial::set_normal_map(const Texture *tex)
+{
+       normal.texture = tex;
+}
+
+void PbrMaterial::set_metalness(float value)
+{
+       metalness.value = value;
+       shdata.uniform("pbr_material.metalness", value);
+}
+
+void PbrMaterial::set_metalness_map(const Texture *tex)
+{
+       metalness.texture = tex;
+}
+
+void PbrMaterial::set_roughness(float value)
+{
+       roughness.value = value;
+       shdata.uniform("pbr_material.roughness", value);
+}
+
+void PbrMaterial::set_roughness_map(const Texture *tex)
+{
+       roughness.texture = tex;
+}
+
+void PbrMaterial::set_occlusion_map(const Texture *tex)
+{
+       occlusion.texture = tex;
+}
+
+void PbrMaterial::set_emission(const Color &color)
+{
+       emission.value = color;
+       shdata.uniform("pbr_material.emission", color);
+}
+
+void PbrMaterial::set_emission_map(const Texture *tex)
+{
+       emission.texture = tex;
+}
+
+void PbrMaterial::set_receive_shadows(bool s)
+{
+       receive_shadows = s;
+}
+
+
+DataFile::Loader::ActionMap PbrMaterial::Loader::shared_actions;
+
+PbrMaterial::Loader::Loader(PbrMaterial &m):
+       DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m)
+{
+       set_actions(shared_actions);
+}
+
+PbrMaterial::Loader::Loader(PbrMaterial &m, Collection &c):
+       DerivedObjectLoader<PbrMaterial, Material::PropertyLoader<PbrMaterial> >(m, c)
+{
+       set_actions(shared_actions);
+}
+
+void PbrMaterial::Loader::init_actions()
+{
+       Material::PropertyLoader<PbrMaterial>::init_actions();
+       add_property("base_color", &PbrMaterial::set_base_color, &PbrMaterial::set_base_color_map, true);
+       add_property("normal", &PbrMaterial::set_normal_map);
+       add_property("metalness", &PbrMaterial::set_metalness, &PbrMaterial::set_metalness_map);
+       add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map);
+       add_property("occlusion", &PbrMaterial::set_occlusion_map);
+       add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false);
+       add("receive_shadows", &PbrMaterial::receive_shadows);
+}
+
+} // namespace GL
+} // namespace Msp