CSEngine
Loading...
Searching...
No Matches
RenderMgr.cpp
1#include "RenderMgr.h"
2#include "../LightMgr.h"
3#include "../EngineCore.h"
4#include "../../Util/Render/SFrameBuffer.h"
5#include "../../Util/Render/SEnvironmentMgr.h"
6#include "../../Util/Render/SGBuffer.h"
7#include "../CameraMgr.h"
8#include "../../Component/RenderComponent.h"
9#include "../../Util/Settings.h"
10#include "../../Util/GLProgramHandle.h"
11
12#include "ForwardRenderGroup.h"
13#include "DeferredRenderGroup.h"
14#include "DepthOnlyRenderGroup.h"
15
16using namespace CSE;
17
18CameraMgr* cameraMgr = nullptr;
19LightMgr* lightMgr = nullptr;
20
21RenderMgr::RenderMgr() = default;
22
23RenderMgr::~RenderMgr() {
25}
26
28 cameraMgr = CORE->GetCore(CameraMgr);
29 lightMgr = CORE->GetCore(LightMgr);
30
31 m_width = SEnvironmentMgr::GetPointerWidth();
32 m_height = SEnvironmentMgr::GetPointerHeight();
33
34 InitBuffers((int)*m_width, (int)*m_height);
35
36 m_forwardRenderGroup = new ForwardRenderGroup(*this);
37 m_deferredRenderGroup = new DeferredRenderGroup(*this);
38 m_depthOnlyRenderGroup = new DepthOnlyRenderGroup(*this);
39}
40
42 if(m_deferredRenderGroup != nullptr)
43 static_cast<DeferredRenderGroup*>(m_deferredRenderGroup)->SetViewport();
44 ResizeBuffers((int)*m_width, (int)*m_height);
45}
46
47void RenderMgr::Render() const {
48 // 1. Render depth buffer for shadows.
49 RenderShadows();
50
51 // 2. Render active sub cameras.
52 RenderSubCameras();
53
54 // 3. Main Render Buffer
55 RenderMainCamera();
56}
57
58void RenderMgr::RenderShadows() const {
59 const auto& lightObjects = lightMgr->GetAll();
60 for (const auto& light : lightObjects) {
61 if(!light->IsShadow()) continue;
62 m_depthOnlyRenderGroup->RenderAll(*light);
63 }
64 lightMgr->RefreshShadowCount();
65}
66
67void RenderMgr::RenderSubCameras() const {
68 const auto& cameraObjects = cameraMgr->GetAll();
69 const auto& mainCamera = cameraMgr->GetCurrentCamera();
70
71 for (const auto& camera : cameraObjects) {
72 if(!camera->GetIsEnable() || camera == mainCamera || camera->GetFrameBuffer() == nullptr) continue;
73 ResetBuffer(*camera);
74 m_deferredRenderGroup->RenderAll(*camera); // Deferred Render
75 m_forwardRenderGroup->RenderAll(*camera); // Forward Render
76 }
77}
78
79void RenderMgr::RenderMainCamera() const {
80 const auto& mainCamera = cameraMgr->GetCurrentCamera();
81 if (mainCamera == nullptr) return;
82
83 ResetBuffer(*mainCamera);
84 m_deferredRenderGroup->RenderAll(*mainCamera); // Deferred Render
85 m_forwardRenderGroup->RenderAll(*mainCamera); // Forward Render
86
87 GetMainBuffer()->AttachFrameBuffer(GL_READ_FRAMEBUFFER);
88#ifdef IOS
89 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 1);
90#else
91 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
92#endif
93 glBlitFramebuffer(0, 0, *m_width, *m_height, 0, 0, *m_width, *m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
94}
95
97 RenderContainer::Exterminate();
98}
99
100void RenderMgr::ResetBuffer(const CameraBase& camera) const {
101 auto frameBuffer = camera.GetFrameBuffer();
102 if(frameBuffer == nullptr) {
103 frameBuffer = GetMainBuffer();
104 }
105 frameBuffer->AttachFrameBuffer();
106 camera.RenderBackground();
107}
void RefreshShadowCount(int shadowCount=-1) const
Definition LightMgr.cpp:57
void Init() override
Initialization of resources required for rendering.
Definition RenderMgr.cpp:27
void SetViewport()
Modify the framebuffer to fit the modified screen size.
Definition RenderMgr.cpp:41
RenderMgr()
Constructor for RenderMgr.
void Exterminate() override
Destroy all resources used for rendering.
Definition RenderMgr.cpp:96
void Render() const override
Rendering all stuffs.
Definition RenderMgr.cpp:47