From afcac59c7d04f4337d6b04ebed8cac7e871ccc50 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 15 Apr 2022 20:30:39 -0700 Subject: Changeset: 7 Work on Material system --- source/Material.hpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 8 deletions(-) (limited to 'source/Material.hpp') diff --git a/source/Material.hpp b/source/Material.hpp index 6290a25..6b431be 100644 --- a/source/Material.hpp +++ b/source/Material.hpp @@ -1,5 +1,6 @@ #pragma once +#include "EditorAttachment.hpp" #include "RcPtr.hpp" #include "Shader.hpp" #include "Texture.hpp" @@ -7,15 +8,18 @@ #include #include #include +#include #include #include #include #include #include -// TODO support multiple sizes of vectors and matrices +class MaterialManager; class Material : public RefCounted { -public: +public: // NOTE: public for internal helpers and editor + // NOTE: specialize between scalar vs matrix vs vector to save memory + struct ScalarUniform { union { float floatValue; @@ -23,27 +27,33 @@ public: uint32_t uintValue; }; GLenum actualType; - GLint location; + /* Saves 'name' */ int infoUniformIndex; + /* Transient */ GLint location; }; struct VectorUniform { float value[4]; int actualLength; - GLint location; + /* Saves 'name' */ int infoUniformIndex; + /* Transient */ GLint location; }; struct MatrixUniform { float value[16]; int actualWidth; int actualHeight; - GLint location; + /* Saves 'name' */ int infoUniformIndex; + /* Transient */ GLint location; }; struct TextureUniform { RcPtr value; - GLint location; + /* Saves 'name' */ int infoUniformIndex; + /* Transient */ GLint location; }; + std::string mName; + std::unique_ptr mEditorAttachment; RcPtr mShader; std::vector mBoundScalars; std::vector mBoundVectors; @@ -51,7 +61,9 @@ public: std::vector mBoundTextures; public: - Material(Shader* shader); + // NOTE: constructs invalid object + Material() = default; + Material(Shader* shader, std::string name = ""); void SetFloat(const char* name, float value); void SetInt(const char* name, int32_t value); @@ -70,7 +82,40 @@ public: std::span GetVectors() const; std::span GetMatrices() const; std::span GetTextures() const; - const Shader& GetShader() const; + Shader* GetShader() const; + void SetShader(Shader* shader); + const std::string& GetName() const; + + bool IsAnnoymous() const; + bool IsValid() const; void UseUniforms() const; + + EditorAttachment* GetEditorAttachment() const { return mEditorAttachment.get(); } + void SetEditorAttachment(EditorAttachment* attachment) { mEditorAttachment.reset(attachment); } + + void GetDesignatedPath(char* buffer, int bufferSize); + std::filesystem::path GetDesignatedPath(); + + bool SaveToFile(const std::filesystem::path& filePath) const; + bool LoadFromFile(const std::filesystem::path& filePath); +}; + +class MaterialManager { +public: + static inline MaterialManager* instance = nullptr; + +private: + absl::flat_hash_map> mMaterials; + +public: + void DiscoverMaterials(); + + std::pair SaveMaterial(Material* mat); + void DeleteMaterial(Material* mat, bool onDisk = false); + Material* LoadMaterial(std::string_view name); + bool RenameMaterial(Material* mat, std::string newName); + + const auto& GetMaterials() const { return mMaterials; } + Material* FindMaterial(std::string_view name); }; -- cgit v1.2.3-70-g09d2