CSEngine
Loading...
Searching...
No Matches
SMaterial.h
1#pragma once
2
3#include <functional>
4#include <unordered_map>
5
6#include "../GLProgramHandle.h"
7#include "../STypeDef.h"
8#include "../Interface/TransformInterface.h"
9#include "../Render/GLMeshID.h"
10#include "SShaderGroup.h"
11
12namespace CSE {
13
14 class LightMgr;
15
16 class SMaterial : public SResource {
17 public:
18 enum SMaterialMode { FORWARD = 0, DEFERRED = 1, DEPTH_ONLY = 2 };
19 private:
20 struct Element {
21 int id = HANDLE_NULL;
22 SType type = SType::UNKNOWN;
23 std::vector<std::string> valueStr;
24 int count = 1;
25 std::string raw;
26
27 std::function<void()> attachFunc = nullptr;
28 };
29
30 typedef std::unordered_map<std::string, Element*> ElementsMap;
31 public:
32 RESOURCE_DEFINE_CONSTRUCTOR(SMaterial);
33
34 explicit SMaterial(const SMaterial* material);
35
36 ~SMaterial() override;
37
38 void Exterminate() override;
39
40 int AttachElement(int textureLayout) const;
41
42 SMaterial::Element* GetElement(const std::string& key) const;
43
44 const ElementsMap& GetElements() const {
45 return m_elements;
46 }
47
48 void InitElements(const ElementsMap& elements, SShaderGroup* shaders);
49
50 void SetInt(const std::string& name, int value);
51
52 void SetFloat(const std::string& name, float value);
53
54 void SetVec3(const std::string& name, const vec3& value);
55
56 void SetTexture(const std::string& name, SResource* texture);
57
58 void SetRawData(const std::string& name, std::vector<std::string> raw);
59
60 short GetOrderLayer() const;
61
62 void SetOrderLayer(int orderLayer);
63
64 SMaterialMode GetMode() const;
65
66 void SetMode(SMaterialMode mode);
67
68 SShaderGroup* GetShaders() const;
69
70 int GetTextureCount() const;
71
72 std::string PrintMaterial() const;
73
74 static SMaterial* GenerateMaterial(SShaderGroup* shaders);
75
76 void SetValue(std::string name_str, Arguments value) override;
77
78 std::string PrintValue() const override;
79
80 protected:
81 void Init(const AssetMgr::AssetReference* asset) override;
82
83 private:
84 void ReleaseElements();
85
86 void SetBindFuncByType(Element* element);
87
88 static void SetIntFunc(Element* element, int value);
89 static void SetFloatFunc(Element* element, float value);
90 static void SetBoolFunc(Element* element, bool value);
91
92 static void SetMat4Func(Element* element, mat4 value);
93 static void SetMat3Func(Element* element, mat3 value);
94 static void SetMat2Func(Element* element, mat2 value);
95 static void SetVec4Func(Element* element, vec4 value);
96 static void SetVec3Func(Element* element, vec3 value);
97 static void SetVec2Func(Element* element, vec2 value);
98
99 void SetTextureFunc(Element* element, SResource* texture);
100
101 private:
102 SShaderGroup* m_shaders = nullptr;
103 short m_orderLayer = 5000;
104 //std::vector<Element*> m_elements;
105 ElementsMap m_elements;
106 mutable int m_textureLayout = 0;
107 int m_textureCount = 0;
108 SMaterialMode m_mode = FORWARD;
109
110 LightMgr* m_lightMgr = nullptr;
111
112 std::string m_refHash;
113 };
114}
void SetValue(std::string name_str, Arguments value) override