diff options
author | rtk0c <[email protected]> | 2022-04-15 20:30:39 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-15 20:30:39 -0700 |
commit | afcac59c7d04f4337d6b04ebed8cac7e871ccc50 (patch) | |
tree | 8c32b90b4a0ab762a68f228dc8cc4e7f52fc5bd7 /source/Material.hpp | |
parent | f2a1481123ac23aeb4937df5f61c57e0e4f1ff52 (diff) |
Changeset: 7 Work on Material system
Diffstat (limited to 'source/Material.hpp')
-rw-r--r-- | source/Material.hpp | 61 |
1 files changed, 53 insertions, 8 deletions
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 <glad/glad.h> #include <cstddef> #include <cstdint> +#include <filesystem> #include <glm/glm.hpp> #include <memory> #include <span> #include <string_view> #include <vector> -// 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<Texture> value; - GLint location; + /* Saves 'name' */ int infoUniformIndex; + /* Transient */ GLint location; }; + std::string mName; + std::unique_ptr<EditorAttachment> mEditorAttachment; RcPtr<Shader> mShader; std::vector<ScalarUniform> mBoundScalars; std::vector<VectorUniform> mBoundVectors; @@ -51,7 +61,9 @@ public: std::vector<TextureUniform> 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<const VectorUniform> GetVectors() const; std::span<const MatrixUniform> GetMatrices() const; std::span<const TextureUniform> 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<std::string_view, RcPtr<Material>> mMaterials; + +public: + void DiscoverMaterials(); + + std::pair<Material*, bool> 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); }; |