aboutsummaryrefslogtreecommitdiff
path: root/ProjectBrussel/Game/Material.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'ProjectBrussel/Game/Material.hpp')
-rw-r--r--ProjectBrussel/Game/Material.hpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/ProjectBrussel/Game/Material.hpp b/ProjectBrussel/Game/Material.hpp
new file mode 100644
index 0000000..f1cd7dd
--- /dev/null
+++ b/ProjectBrussel/Game/Material.hpp
@@ -0,0 +1,125 @@
+#pragma once
+
+#include "Ires.hpp"
+#include "RcPtr.hpp"
+#include "Shader.hpp"
+#include "Texture.hpp"
+
+#include <glad/glad.h>
+#include <robin_hood.h>
+#include <cstddef>
+#include <cstdint>
+#include <glm/glm.hpp>
+#include <memory>
+#include <span>
+#include <string_view>
+#include <vector>
+
+// Forward declarations
+class Material;
+class IresMaterial;
+
+class Material : public RefCounted {
+ friend class IresMaterial;
+
+public:
+ // NOTE: specialize between scalar vs matrix vs vector to save memory
+
+ enum UniformType : uint16_t {
+ UT_Scalar,
+ UT_Vector,
+ UT_Matrix,
+ };
+
+ struct UniformIndex {
+ UniformType type;
+ uint16_t index;
+ };
+
+ struct ScalarUniform {
+ union {
+ float floatValue;
+ int32_t intValue;
+ uint32_t uintValue;
+ };
+ GLenum actualType;
+ /* Transient */ int infoUniformIndex;
+ /* Transient */ GLint location;
+ };
+
+ struct VectorUniform {
+ float value[4];
+ int actualLength;
+ /* Transient */ int infoUniformIndex;
+ /* Transient */ GLint location;
+ };
+
+ struct MatrixUniform {
+ float value[16];
+ int actualWidth;
+ int actualHeight;
+ /* Transient */ int infoUniformIndex;
+ /* Transient */ GLint location;
+ };
+
+ struct TextureUniform {
+ RcPtr<Texture> value;
+ /* Transient */ int infoUniformIndex;
+ /* Transient */ GLint location;
+ };
+
+ IresMaterial* mIres = nullptr;
+ RcPtr<Shader> mShader;
+ std::vector<ScalarUniform> mBoundScalars;
+ std::vector<VectorUniform> mBoundVectors;
+ std::vector<MatrixUniform> mBoundMatrices;
+ std::vector<TextureUniform> mBoundTextures;
+
+public:
+ Material();
+
+ void SetFloat(const char* name, float value);
+ void SetInt(const char* name, int32_t value);
+ void SetUInt(const char* name, uint32_t value);
+
+ /// Instanciated for length == 1, 2, 3, 4
+ template <int length>
+ void SetVector(const char* name, const glm::vec<length, float>& vec);
+
+ /// Instanciated for sizes (2,2) (3,3) (4,4) (2,3) (3,2) (2,4) (4,2) (3,4) (4,3)
+ template <int width, int height>
+ void SetMatrix(const char* name, const glm::mat<width, height, float>& mat);
+
+ void SetTexture(const char* name, Texture* texture);
+
+ std::span<const VectorUniform> GetVectors() const;
+ std::span<const MatrixUniform> GetMatrices() const;
+ std::span<const TextureUniform> GetTextures() const;
+ Shader* GetShader() const;
+ void SetShader(Shader* shader);
+
+ IresMaterial* GetIres() const { return mIres; }
+
+ bool IsValid() const;
+
+ void UseUniforms() const;
+};
+
+// Initialized in main()
+inline RcPtr<Material> gDefaultMaterial;
+
+class IresMaterial : public IresObject {
+private:
+ RcPtr<Material> mInstance;
+
+public:
+ IresMaterial();
+
+ Material* GetInstance() const;
+ void InvalidateInstance();
+
+ void ShowEditor(IEditor& editor) override;
+
+ void Write(IresWritingContext& ctx, rapidjson::Value& value, rapidjson::Document& root) const override;
+ void Read(IresLoadingContext& ctx, const rapidjson::Value& value) override;
+};