From: Mikko Rasa Date: Sun, 21 Feb 2021 21:30:55 +0000 (+0200) Subject: Implement an unlit material type X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=2f198e3;p=libs%2Fgl.git Implement an unlit material type --- diff --git a/source/materials/material.cpp b/source/materials/material.cpp index 810ecd17..17fa546b 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -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("basic"); registry.register_type("pbr"); + registry.register_type("unlit"); } return registry; } diff --git a/source/materials/unlitmaterial.cpp b/source/materials/unlitmaterial.cpp new file mode 100644 index 00000000..5eddd669 --- /dev/null +++ b/source/materials/unlitmaterial.cpp @@ -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 >(m) +{ + set_actions(shared_actions); +} + +UnlitMaterial::Loader::Loader(UnlitMaterial &m, Collection &c): + DerivedObjectLoader >(m, c) +{ + set_actions(shared_actions); +} + +void UnlitMaterial::Loader::init_actions() +{ + Material::PropertyLoader::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 index 00000000..b8b28835 --- /dev/null +++ b/source/materials/unlitmaterial.h @@ -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 > + { + 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