/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #pragma once #include "interfaces/IMaterial.h" #include "utilities/Classes.h" #include "model/Texture.h" #include "gl/shader.h" #include "gl/ubo.h" // a collection of parameters for the rendering setup. // for modern opengl this translates to set of attributes for shaders struct opengl_material : public IMaterial { std::array textures = { null_handle }; std::array params; std::vector params_state; std::shared_ptr shader; std::optional opacity; std::optional selfillum; float glossiness { 10.f }; int shadow_rank { 0 }; // priority as shadow caster; higher = more likely to be skipped std::string name; glm::vec2 size { -1.f, -1.f }; // 'physical' size of bound texture, in meters // constructors opengl_material(); // methods bool deserialize(cParser &Input, bool const Loadnow); virtual void finalize(bool Loadnow) override; virtual bool update() override; virtual float get_or_guess_opacity() const override; virtual bool is_translucent() const override; virtual glm::vec2 GetSize() const override { return size; } virtual std::string GetName() const override { return name; } virtual std::optional GetSelfillum() const override { return selfillum; } virtual int GetShadowRank() const override { return shadow_rank; } virtual texture_handle GetTexture(int slot) const override { return textures[slot]; } // members static struct path_data { std::unordered_map index_map; std::vector data; } paths; bool is_good { false }; // indicates material was compiled without failure int path{ -1 }; // index to material path bool update_on_weather_change{ false }; bool update_on_season_change{ false }; private: // methods // imports member data pair from the config file bool deserialize_mapping( cParser &Input, int const Priority, bool const Loadnow ); void log_error(const std::string &str); // members // priorities for textures, shader, opacity int m_shader_priority = -1; int m_opacity_priority = -1; int m_selfillum_priority = -1; int m_glossiness_priority = -1; struct parse_info_s { struct tex_def { std::string name; int priority; }; struct param_def { glm::vec4 data; int priority; }; std::unordered_map tex_mapping; std::unordered_map param_mapping; }; std::unique_ptr parse_info; }; class material_manager { public: material_manager() { m_materials.emplace_back( opengl_material() ); } // empty bindings for null material material_handle create( std::string const &Filename, bool const Loadnow ); opengl_material const & material( material_handle const Material ) const { return m_materials[ Material ]; } opengl_material & material( material_handle const Material ) { return m_materials[ Material ]; } // material updates executed when environment changes // TODO: registerable callbacks in environment manager void on_weather_change(); void on_season_change(); private: // types typedef std::vector material_sequence; typedef std::unordered_map index_map; // methods: // checks whether specified texture is in the texture bank. returns texture id, or npos. material_handle find_in_databank( std::string const &Materialname ) const; public: // checks whether specified file exists. returns name of the located file, or empty string. static std::pair find_on_disk( std::string const &Materialname ); private: // members: material_sequence m_materials; index_map m_materialmappings; }; //---------------------------------------------------------------------------