aboutsummaryrefslogtreecommitdiff
path: root/source/Material.hpp
diff options
context:
space:
mode:
authorhnOsmium0001 <[email protected]>2022-04-15 20:30:39 -0700
committerhnOsmium0001 <[email protected]>2022-04-15 20:30:39 -0700
commit509201784d6525fc26345e55a56ab81e4a7616b3 (patch)
treebcd68f247937324d06480b58a284b47e1c6bb2b8 /source/Material.hpp
parent989f90ebe2c37e8a517691a35d7e0d827fbe7006 (diff)
Work on Material system
Diffstat (limited to 'source/Material.hpp')
-rw-r--r--source/Material.hpp61
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);
};