CSEngine
Loading...
Searching...
No Matches
RenderComponent.cpp
1#include "RenderComponent.h"
2#include "../Manager/Render/RenderMgr.h"
3#include "../Manager/LightMgr.h"
4#include "../Manager/EngineCore.h"
5#include "../Util/Render/ShaderUtil.h"
6#include "../Util/Render/SMaterial.h"
7#include "../Util/Settings.h"
8#include "TransformComponent.h"
9#include "DrawableSkinnedMeshComponent.h"
10
11using namespace CSE;
12
13COMPONENT_CONSTRUCTOR(RenderComponent) {
14 m_renderMgr = CORE->GetCore(RenderMgr);
15 SetMaterial(nullptr);
16}
17
18RenderComponent::~RenderComponent() = default;
19
20void RenderComponent::Exterminate() {
21 if (material != nullptr) {
22 const auto& mode = material->GetMode();
23 if (m_renderMgr != nullptr) {
24 m_renderMgr->Remove(this, (RenderContainer::RenderGroupMode) mode);
25 m_renderMgr->Remove(this, RenderContainer::DEPTH_ONLY);
26 }
27 }
28 SAFE_DELETE(m_material_clone);
29 material = nullptr;
30}
31
32void RenderComponent::Init() {
33
34 if (!m_disableShadow) {
35 m_lightMgr = CORE->GetCore(LightMgr);
36 m_renderMgr->Register(this, RenderContainer::DEPTH_ONLY);
37 }
38
39 m_mesh = gameObject->GetComponent<DrawableStaticMeshComponent>();
40 if (m_mesh != nullptr) {
41 m_skinningMesh = dynamic_cast<DrawableSkinnedMeshComponent*>(m_mesh);
42 }
43
44 if (material == nullptr) {
45 isEnable = isRenderActive = false;
46 }
47
48 isRenderActive = isEnable;
49}
50
51void RenderComponent::Tick(float elapsedTime) {
52
53 if (m_mesh == nullptr) {
54 m_mesh = gameObject->GetComponent<DrawableStaticMeshComponent>();
55 if (m_mesh != nullptr) {
56 m_skinningMesh = dynamic_cast<DrawableSkinnedMeshComponent*>(m_mesh);
57 }
58 }
59}
60
61void
62RenderComponent::SetMatrix(const CameraMatrixStruct& cameraMatrixStruct, const GLProgramHandle* handle) {
63 ShaderUtil::BindCameraToShader(*handle, cameraMatrixStruct.camera, cameraMatrixStruct.cameraPosition,
64 cameraMatrixStruct.projection,
65 static_cast<const TransformComponent*>(gameObject->GetTransform())->GetMatrix());
66}
67
68void RenderComponent::Render(const GLProgramHandle* handle) const {
69
70 if (m_mesh == nullptr || m_material_clone == nullptr || gameObject->isPrefab()) return;
71
72 SetJointMatrix(handle);
73 ShaderUtil::BindAttributeToShader(*handle, m_mesh->GetMeshID());
74}
75
76void RenderComponent::SetIsEnable(bool is_enable) {
77 SComponent::SetIsEnable(is_enable);
78
79 isRenderActive = isEnable;
80}
81
82SComponent* RenderComponent::Clone(SGameObject* object) {
83 INIT_COMPONENT_CLONE(RenderComponent, clone);
84
85 clone->isRenderActive = isRenderActive;
86 clone->SetMaterial(material);
87
88 return clone;
89}
90
91void RenderComponent::SetJointMatrix(const GLProgramHandle* handle) const {
92 ShaderUtil::BindSkinningDataToShader(*handle, m_mesh->GetMeshID(),
93 m_skinningMesh != nullptr ?
94 m_skinningMesh->GetJointMatrix() : std::vector<mat4>());
95}
96
97SMaterial* RenderComponent::GetMaterial() const {
98 return m_material_clone;
99}
100
101void RenderComponent::SetMaterial(SMaterial* material) {
102 auto renderMgr = CORE->GetCore(RenderMgr);
103 if (this->material == nullptr)
104 this->material = SResource::Create<SMaterial>(Settings::GetDefaultDeferredMaterialId());
105 else {
106 const auto& mode = this->material->GetMode();
107 renderMgr->Remove(this, (RenderContainer::RenderGroupMode) mode);
108 this->material = material;
109 }
110
111 const auto& mode = this->material->GetMode();
112 renderMgr->Register(this, (RenderContainer::RenderGroupMode) mode);
113
114 SAFE_DELETE(m_material_clone)
115 m_material_clone = new SMaterial(this->material);
116}
117
118void RenderComponent::SetValue(std::string name_str, VariableBinder::Arguments value) {
119 if (name_str == "material") {
120 SetMaterial(SResource::Create<SMaterial>(value[0]));
121 }
122}
123
124std::string RenderComponent::PrintValue() const {
125 PRINT_START("component");
126
127 PRINT_VALUE(material, ConvertSpaceStr(material->GetHash()));
128
129 PRINT_END("component");
130}
Class for managing rendering operations.
Definition RenderMgr.h:21
static void BindAttributeToShader(const GLProgramHandle &handle, const GLMeshID &meshId)
Binds the attributes to the shader.
static void BindSkinningDataToShader(const GLProgramHandle &handle, const GLMeshID &meshId, const std::vector< mat4 > &jointMatrix)
Binds the skinning data to the shader.
static void BindCameraToShader(const GLProgramHandle &handle, const mat4 &camera, const vec3 &cameraPosition, const mat4 &projection, const mat4 &transform)
Binds the camera data to the shader.