CSEngine
Loading...
Searching...
No Matches
CSE::SMaterial Class Reference
Inheritance diagram for CSE::SMaterial:
CSE::SResource CSE::SObject

Public Types

enum  SMaterialMode { NORMAL = 0 , DEFERRED = 1 , DEPTH_ONLY = 2 }
 

Public Member Functions

 SMaterial (const SMaterial *material)
 
void Exterminate () override
 
void AttachElement () const
 
void InitElements (const ElementsMap &elements, SShaderGroup *shaders)
 
void SetInt (const std::string &name, int value)
 
void SetFloat (const std::string &name, float value)
 
void SetVec3 (const std::string &name, const vec3 &value)
 
void SetTexture (const std::string &name, SResource *texture)
 
short GetOrderLayer () const
 
void SetOrderLayer (int orderLayer)
 
SMaterialMode GetMode () const
 
void SetMode (SMaterialMode mode)
 
SShaderGroupGetShaders () const
 
int GetTextureCount () const
 
std::string PrintMaterial () const
 
- Public Member Functions inherited from CSE::SResource
 SResource (bool isRegister)
 
 SResource (const SResource *resource, bool isRegister)
 
void SetName (std::string name)
 
void SetAbsoluteID (std::string id)
 
std::string GetName () const
 
std::string GetAbsoluteID () const
 
AssetMgr::AssetReferenceGetAssetReference (std::string hash="") const
 
void LinkResource (AssetMgr::AssetReference *asset)
 
void LinkResource (std::string name)
 
void SetHash (std::string &hash) override
 
- Public Member Functions inherited from CSE::SObject
 SObject (bool isRegister)
 
virtual void SetUndestroyable (bool enable)
 
virtual void Destroy ()
 
virtual void __FORCE_DESTROY__ ()
 
virtual std::string GenerateMeta ()
 
std::string GetHash () const
 

Static Public Member Functions

static SMaterialGenerateMaterial (SShaderGroup *shaders)
 
- Static Public Member Functions inherited from CSE::SResource
template<class T >
static T * Create (const std::string &name)
 
template<class T >
static T * Create (const AssetMgr::AssetReference *asset)
 
template<class T >
static T * Get (std::string name)
 

Protected Member Functions

void Init (const AssetMgr::AssetReference *asset) override
 

Additional Inherited Members

- Protected Attributes inherited from CSE::SObject
std::string m_hash
 

Detailed Description

Definition at line 16 of file SMaterial.h.

Member Enumeration Documentation

◆ SMaterialMode

enum CSE::SMaterial::SMaterialMode

Definition at line 18 of file SMaterial.h.

18{ NORMAL = 0, DEFERRED = 1, DEPTH_ONLY = 2 };

Constructor & Destructor Documentation

◆ SMaterial() [1/2]

SMaterial::SMaterial ( )

Definition at line 11 of file SMaterial.cpp.

11 {
12 m_lightMgr = CORE->GetCore(LightMgr);
13}

◆ SMaterial() [2/2]

SMaterial::SMaterial ( const SMaterial material)
explicit

Definition at line 15 of file SMaterial.cpp.

15 : SResource(material, false) {
16
17 if (material == nullptr) throw -1;
18
19 m_refHash = material->m_hash;
20
21 for (const auto& element_pair : material->m_elements) {
22 const auto& element_src = element_pair.second;
23 auto element_copy = new Element;
24 element_copy->count = element_src->count;
25 element_copy->type = element_src->type;
26 element_copy->valueStr = element_src->valueStr;
27 m_elements.insert(std::pair<std::string, Element*>(element_pair.first, element_copy));
28 }
29 m_mode = material->m_mode;
30 m_shaders = material->m_shaders;
31 m_orderLayer = material->m_orderLayer;
32 m_textureLayout = material->m_textureLayout;
33
34 InitElements(m_elements, m_shaders);
35 m_lightMgr = CORE->GetCore(LightMgr);
36}

◆ ~SMaterial()

SMaterial::~SMaterial ( )
override

Definition at line 38 of file SMaterial.cpp.

38 {
39 ReleaseElements();
40}

Member Function Documentation

◆ AttachElement()

void SMaterial::AttachElement ( ) const

Definition at line 54 of file SMaterial.cpp.

54 {
55 m_textureLayout = m_lightMgr->GetShadowCount();
56
57 for (const auto& element_pair : m_elements) {
58 const auto& element = element_pair.second;
59 if (element->id < 0) continue;
60 element->attachFunc();
61 }
62}
int GetShadowCount() const
Definition LightMgr.h:56

◆ Exterminate()

void SMaterial::Exterminate ( )
overridevirtual

Implements CSE::SObject.

Definition at line 42 of file SMaterial.cpp.

42 {
43 ReleaseElements();
44}

◆ GenerateMaterial()

SMaterial * SMaterial::GenerateMaterial ( SShaderGroup shaders)
static

Definition at line 287 of file SMaterial.cpp.

287 {
288 if (shaders == nullptr) return nullptr;
289 const auto& handle = shaders->GetHandleByMode(SMaterialMode::NORMAL);
290
291 const auto& uniformList = handle->GetUniformsList();
292 auto material = new SMaterial();
293 material->m_shaders = shaders;
294
295 for (const auto& uniform_pair : uniformList) {
296 const auto& name = uniform_pair.first;
297 const auto& gl_element = uniform_pair.second;
298
299 auto element = new Element;
300 element->id = gl_element->id;
301 element->type = XMLParser::GetType(gl_element->type);
302
303 material->m_elements.insert(std::pair<std::string, Element*>(name, element));
304 }
305 material->InitElements(material->m_elements, shaders);
306
307 return material;
308}

◆ GetMode()

SMaterial::SMaterialMode SMaterial::GetMode ( ) const

Definition at line 275 of file SMaterial.cpp.

275 {
276 return m_mode;
277}

◆ GetOrderLayer()

short SMaterial::GetOrderLayer ( ) const

Definition at line 263 of file SMaterial.cpp.

263 {
264 return m_orderLayer;
265}

◆ GetShaders()

SShaderGroup * SMaterial::GetShaders ( ) const

Definition at line 271 of file SMaterial.cpp.

271 {
272 return m_shaders;
273}

◆ GetTextureCount()

int SMaterial::GetTextureCount ( ) const

Definition at line 283 of file SMaterial.cpp.

283 {
284 return m_textureLayout;
285}

◆ Init()

void SMaterial::Init ( const AssetMgr::AssetReference asset)
overrideprotectedvirtual

Implements CSE::SResource.

Definition at line 106 of file SMaterial.cpp.

106 {
107 const XNode* root;
108
109 try {
110 root = XFILE(asset->name_path.c_str()).getRoot();
111 }
112 catch (int e) {
113 return;
114 }
115
116 XNode cse_mat = root->getChild("CSEMAT");
117 XNode shader_node = cse_mat.getChild("shader");
118
119 auto var_nodes = shader_node.children;
120 auto shader_file_id = shader_node.getAttribute("id").value;
121 auto shaderGroup = Create<SShaderGroup>(shader_file_id);
122 if (shaderGroup == nullptr) return;
123 m_shaders = shaderGroup;
124
125 if (shader_node.hasAttribute("deferred")) {
126 auto get_deferred = std::stoi(shader_node.getAttribute("deferred").value);
127 if (get_deferred == 1) {
128 m_mode = DEFERRED;
129 }
130 }
131
132 for (const auto& node : var_nodes) {
133
134 auto element_value = node.value.toStringVector();
135 auto element_type = node.getAttribute("type").value;
136 SType type = XMLParser::GetType(element_type);
137 auto element_name = node.getAttribute("name").value;
138 auto element_count = node.getAttribute("count").value;
139
140 auto element = new Element;
141 element->type = type;
142 element->count = std::stoi(element_count);
143 element->valueStr = element_value;
144 m_elements.insert(std::pair<std::string, Element*>(element_name, element));
145 }
146 SAFE_DELETE(root);
147}
Definition XML.h:77
Definition XML.h:43

◆ InitElements()

void SMaterial::InitElements ( const ElementsMap &  elements,
SShaderGroup shaders 
)

Definition at line 64 of file SMaterial.cpp.

64 {
65 const auto& handle = shaders->GetHandleByMode(m_mode);
66 for (const auto& element_pair : elements) {
67 const auto& element_name = element_pair.first.c_str();
68 const auto& element = element_pair.second;
69 if (element->attachFunc != nullptr) continue;
70
71 const auto& handleElement = handle->UniformLocation(element_name);
72 if (handleElement == nullptr) continue;
73
74 element->id = handleElement->id;
75 SetBindFuncByType(element);
76 }
77// std::string str = PrintMaterial();
78// std::string path = GetAssetReference(m_refHash)->path;
79// SaveTxtFile(path, str);
80}

◆ PrintMaterial()

std::string SMaterial::PrintMaterial ( ) const

Definition at line 310 of file SMaterial.cpp.

310 {
311 std::string result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
312 "<CSEMAT version=\"1.0.0\">\n";
313
314 if(m_mode == DEFERRED) {
315 result += "<shader id=\"" + m_shaders->GetHash() + "\" deferred=\"1\">\n";
316 }
317 else {
318 result += "<shader id=\"" + m_shaders->GetHash() + "\">\n";
319 }
320
321 for (const auto& var : m_elements) {
322 const auto& element = var.second;
323 if(element->raw.empty()) continue;
324
325 result += "<var name=\"" + var.first
326 + "\" type=\"" + XMLParser::ToString(element->type)
327 + "\" count=\"1\">"
328 + element->raw + "</var>\n";
329 }
330 result += "</shader>\n</CSEMAT>";
331 return result;
332}

◆ SetFloat()

void SMaterial::SetFloat ( const std::string &  name,
float  value 
)

Definition at line 88 of file SMaterial.cpp.

88 {
89 auto find_iter = m_elements.find(name);
90 if (find_iter == m_elements.end()) return;
91 SetFloatFunc(find_iter->second, value);
92}

◆ SetInt()

void SMaterial::SetInt ( const std::string &  name,
int  value 
)

Definition at line 82 of file SMaterial.cpp.

82 {
83 auto find_iter = m_elements.find(name);
84 if (find_iter == m_elements.end()) return;
85 SetIntFunc(find_iter->second, value);
86}

◆ SetMode()

void SMaterial::SetMode ( SMaterialMode  mode)

Definition at line 279 of file SMaterial.cpp.

279 {
280 m_mode = mode;
281}

◆ SetOrderLayer()

void SMaterial::SetOrderLayer ( int  orderLayer)

Definition at line 267 of file SMaterial.cpp.

267 {
268 m_orderLayer = orderLayer;
269}

◆ SetTexture()

void SMaterial::SetTexture ( const std::string &  name,
SResource texture 
)

Definition at line 100 of file SMaterial.cpp.

100 {
101 auto find_iter = m_elements.find(name);
102 if (find_iter == m_elements.end()) return;
103 SetTextureFunc(find_iter->second, texture);
104}

◆ SetVec3()

void SMaterial::SetVec3 ( const std::string &  name,
const vec3 value 
)

Definition at line 94 of file SMaterial.cpp.

94 {
95 auto find_iter = m_elements.find(name);
96 if (find_iter == m_elements.end()) return;
97 SetVec3Func(find_iter->second, value);
98}

The documentation for this class was generated from the following files: