]> git.tdb.fi Git - libs/gl.git/commitdiff
Implement an unlit material type
authorMikko Rasa <tdb@tdb.fi>
Sun, 21 Feb 2021 21:30:55 +0000 (23:30 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 21 Feb 2021 21:30:55 +0000 (23:30 +0200)
source/materials/material.cpp
source/materials/unlitmaterial.cpp [new file with mode: 0644]
source/materials/unlitmaterial.h [new file with mode: 0644]

index 810ecd174d81fb407730b33b0488b456dcee4f86..17fa546bbf0ba9f94fb7e08688afd3a46a2d7cee 100644 (file)
@@ -6,6 +6,7 @@
 #include "resources.h"
 #include "texturing.h"
 #include "uniform.h"
+#include "unlitmaterial.h"
 
 using namespace std;
 
@@ -68,6 +69,7 @@ Material::MaterialRegistry &Material::get_material_registry()
                initialized = true;
                registry.register_type<BasicMaterial>("basic");
                registry.register_type<PbrMaterial>("pbr");
+               registry.register_type<UnlitMaterial>("unlit");
        }
        return registry;
 }
diff --git a/source/materials/unlitmaterial.cpp b/source/materials/unlitmaterial.cpp
new file mode 100644 (file)
index 0000000..5eddd66
--- /dev/null
@@ -0,0 +1,70 @@
+#include "unlitmaterial.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+UnlitMaterial::UnlitMaterial():
+       texture(0),
+       vertex_color(false)
+{
+       set_tint(Color(1.0f));
+}
+
+string UnlitMaterial::create_program_source() const
+{
+       string source = "import unlit;\n";
+       if(texture)
+               source += "const bool use_texture = true;\n";
+       if(vertex_color)
+               source += "const bool use_vertex_color = true;\n";
+       return source;
+}
+
+void UnlitMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
+{
+       attach_texture_to(texture, texturing, tex_shdata, "texture");
+}
+
+void UnlitMaterial::set_texture(const Texture *tex)
+{
+       texture = tex;
+}
+
+void UnlitMaterial::set_tint(const Color &t)
+{
+       tint = t;
+       shdata.uniform("tint", tint);
+}
+
+void UnlitMaterial::set_vertex_color(bool vc)
+{
+       vertex_color = vc;
+}
+
+
+DataFile::Loader::ActionMap UnlitMaterial::Loader::shared_actions;
+
+UnlitMaterial::Loader::Loader(UnlitMaterial &m):
+       DerivedObjectLoader<UnlitMaterial, Material::PropertyLoader<UnlitMaterial> >(m)
+{
+       set_actions(shared_actions);
+}
+
+UnlitMaterial::Loader::Loader(UnlitMaterial &m, Collection &c):
+       DerivedObjectLoader<UnlitMaterial, Material::PropertyLoader<UnlitMaterial> >(m, c)
+{
+       set_actions(shared_actions);
+}
+
+void UnlitMaterial::Loader::init_actions()
+{
+       Material::PropertyLoader<UnlitMaterial>::init_actions();
+       add("texture", &Loader::property_texture, &UnlitMaterial::set_texture);
+       add_property("tint", &UnlitMaterial::set_tint, 0, true);
+       add("vertex_color", &UnlitMaterial::vertex_color);
+}
+
+} // namespace GL
+} // namespace Msp
diff --git a/source/materials/unlitmaterial.h b/source/materials/unlitmaterial.h
new file mode 100644 (file)
index 0000000..b8b2883
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef MSP_GL_UNLITMATERIAL_H_
+#define MSP_GL_UNLITMATERIAL_H_
+
+#include "material.h"
+
+namespace Msp {
+namespace GL {
+
+class UnlitMaterial: public Material
+{
+public:
+       class Loader: public DataFile::DerivedObjectLoader<UnlitMaterial, Material::PropertyLoader<UnlitMaterial> >
+       {
+       private:
+               static ActionMap shared_actions;
+
+       public:
+               Loader(UnlitMaterial &);
+               Loader(UnlitMaterial &, Collection &);
+
+       private:
+               virtual void init_actions();
+
+               void texture(const std::string &);
+       };
+
+private:
+       const Texture *texture;
+       Color tint;
+       bool vertex_color;
+
+public:
+       UnlitMaterial();
+
+protected:
+       virtual std::string create_program_source() const;
+
+public:
+       virtual void attach_textures_to(Texturing &, ProgramData &) const;
+
+       void set_texture(const Texture *);
+       void set_tint(const Color &);
+       void set_vertex_color(bool);
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif