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 { NORMAL = 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 SMaterial();
33
34 explicit SMaterial(const SMaterial* material);
35
36 ~SMaterial() override;
37
38 void Exterminate() override;
39
40 void AttachElement() const;
41
42 void InitElements(const ElementsMap& elements, SShaderGroup* shaders);
43
44 void SetInt(const std::string& name, int value);
45
46 void SetFloat(const std::string& name, float value);
47
48 void SetVec3(const std::string& name, const vec3& value);
49
50 void SetTexture(const std::string& name, SResource* texture);
51
52 short GetOrderLayer() const;
53
54 void SetOrderLayer(int orderLayer);
55
56 SMaterialMode GetMode() const;
57
58 void SetMode(SMaterialMode mode);
59
60 SShaderGroup* GetShaders() const;
61
62 int GetTextureCount() const;
63
64 std::string PrintMaterial() const;
65
66 static SMaterial* GenerateMaterial(SShaderGroup* shaders);
67
68 protected:
69 void Init(const AssetMgr::AssetReference* asset) override;
70
71 private:
72 void ReleaseElements();
73
74 void SetBindFuncByType(Element* element);
75
76 static void SetIntFunc(Element* element, int value);
77 static void SetFloatFunc(Element* element, float value);
78 static void SetBoolFunc(Element* element, bool value);
79
80 static void SetMat4Func(Element* element, mat4 value);
81 static void SetMat3Func(Element* element, mat3 value);
82 static void SetMat2Func(Element* element, mat2 value);
83 static void SetVec4Func(Element* element, vec4 value);
84 static void SetVec3Func(Element* element, vec3 value);
85 static void SetVec2Func(Element* element, vec2 value);
86
87 void SetTextureFunc(Element* element, SResource* texture);
88
89 private:
90 SShaderGroup* m_shaders = nullptr;
91 short m_orderLayer = 5000;
92 //std::vector<Element*> m_elements;
93 ElementsMap m_elements;
94 mutable int m_textureLayout = 0;
95 int m_textureCount = 0;
96 SMaterialMode m_mode = NORMAL;
97
98 LightMgr* m_lightMgr = nullptr;
99
100 std::string m_refHash;
101 };
102}