CSEngine
Loading...
Searching...
No Matches
DeferredRenderGroup.cpp
1#include "DeferredRenderGroup.h"
2#include "RenderMgr.h"
3#include "../../Util/Render/CameraBase.h"
4#include "../../Util/Render/SMaterial.h"
5#include "../../Util/Render/SGBuffer.h"
6#include "../../Util/Render/SFrameBuffer.h"
7#include "../../Util/Render/ShaderUtil.h"
8#include "../../Util/Settings.h"
9#include "../LightMgr.h"
10#include "../../Component/CameraComponent.h"
11#include "../EngineCore.h"
12
13using namespace CSE;
14
16 m_width = renderMgr.GetWidth();
17 m_height = renderMgr.GetHeight();
18
19 m_lightMgr = CORE->GetCore(LightMgr);
20}
21
22DeferredRenderGroup::~DeferredRenderGroup() = default;
23
25 const auto& material = object->GetMaterialReference();
26 const auto& shaders = material->GetShaders();
27 if (shaders == nullptr) return;
28 auto* handle = const_cast<GLProgramHandle*>(shaders->GetHandle("deferred"));
29
30 auto gbuffer = m_gbufferLayer[shaders];
31 if (!gbuffer) {
32 gbuffer = new SGBuffer();
33 gbuffer->GenerateGBuffer(*m_width, *m_height);
34 m_gbufferLayer[shaders] = gbuffer;
35 }
36 gbuffer->PushBackToLayer(object);
37 gbuffer->BindLightPass(handle);
38}
39
41 const auto& material = object->GetMaterialReference();
42 const auto& programLayer = m_gbufferLayer;
43 auto it = std::find_if(programLayer.begin(), programLayer.end(), [&material](const auto& elem) {
44 return elem.first == material->GetShaders();
45 });
46 if (it != programLayer.end()) {
47 it->second->RemoveToLayer(object);
48 }
49}
50
51void DeferredRenderGroup::RenderAll(const CameraBase& camera) const {
52 for (const auto& gbuffer_pair : m_gbufferLayer) {
53 const auto& shaders = gbuffer_pair.first;
54 const auto& gbuffer = gbuffer_pair.second;
55 RenderGbuffer(camera, *gbuffer, *shaders);
56 }
57}
58
59void
61 const SShaderGroup& shaders) const {
62 const auto cameraMatrix = camera.GetCameraMatrixStruct();
63 const auto& frameBuffer = camera.GetFrameBuffer();
64 const bool hasFrameBuffer = frameBuffer != nullptr;
65 const auto& lightPassHandle = gbuffer.GetLightPassHandle();
66
67 const int bufferWidth = hasFrameBuffer ? frameBuffer->GetWidth() : (int) *m_width;
68 const int bufferHeight = hasFrameBuffer ? frameBuffer->GetHeight() : (int) *m_height;
69
73 gbuffer.AttachGeometryFrameBuffer();
74 glViewport(0, 0, *m_width, *m_height);
75 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
76 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77 const auto& renderLayer = gbuffer.GetRendersLayer();
78 const auto& handle = shaders.GetGeometryHandle();
79 glUseProgram(handle->Program);
80
81 for (const auto& render : renderLayer) {
82 if (render == nullptr) continue;
83 if (!render->isRenderActive) continue;
84
85 const auto& material = render->GetMaterial();
86 material->AttachElement();
87 render->SetMatrix(cameraMatrix, handle);
88 render->Render(handle);
89 }
90
94 const auto& deferredBuffer =
95 frameBuffer == nullptr ? m_renderMgr->GetMainBuffer() : frameBuffer;
96 deferredBuffer->AttachFrameBuffer();
97 gbuffer.AttachLightPass();
98 //Attach Light
99 m_lightMgr->AttachLightToShader(lightPassHandle);
100 m_lightMgr->AttachLightMapToShader(lightPassHandle, m_lightMgr->GetShadowCount());
101 const auto layoutBegin = m_lightMgr->GetShadowCount() + m_lightMgr->GetLightMapCount();
102 gbuffer.AttachLightPassTexture(layoutBegin);
103 BindSourceBuffer(*deferredBuffer, *lightPassHandle, layoutBegin + 3);
104
105 ShaderUtil::BindCameraToShader(*lightPassHandle, cameraMatrix.camera, cameraMatrix.cameraPosition,
106 cameraMatrix.projection, cameraMatrix.camera);
107
108 gbuffer.RenderLightPass();
109
113// if (frameBuffer == nullptr) {
114// m_renderMgr->GetMainBuffer()->BlitFrameBuffer(*deferredBuffer);
115// } else {
116// frameBuffer->BlitFrameBuffer(*deferredBuffer);
117// }
118// gbuffer.AttachGeometryFrameBuffer(GL_READ_FRAMEBUFFER);
119// if(frameBuffer == nullptr) {
120// GetMainBuffer()->AttachFrameBuffer(GL_DRAW_FRAMEBUFFER);
121// }
122// else {
123// frameBuffer->AttachFrameBuffer(GL_DRAW_FRAMEBUFFER);
124// }
125//
126// glBlitFramebuffer(0, 0, *m_width, *m_height, 0, 0, bufferWidth, bufferHeight, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
127}
128
130 for (const auto& gbufferPair : m_gbufferLayer) {
131 const auto& gbuffer = gbufferPair.second;
132 gbuffer->ResizeGBuffer((int) *m_width, (int) *m_height);
133 }
134}
135
137 for (auto gbuffer_pair : m_gbufferLayer) {
138 auto gbuffer = gbuffer_pair.second;
139 SAFE_DELETE(gbuffer);
140 }
141 m_gbufferLayer.clear();
142}
void RenderGbuffer(const CameraBase &camera, const SGBuffer &gbuffer, const SShaderGroup &shaders) const
void RegisterObject(SIRender *object) override
DeferredRenderGroup(const RenderMgr &renderMgr)
void RemoveObjects(SIRender *object) override
void RenderAll(const CameraBase &camera) const override
int GetShadowCount() const
Definition LightMgr.h:56
void AttachLightMapToShader(const GLProgramHandle *handle, int textureLayout) const
Definition LightMgr.cpp:47
void AttachLightToShader(const GLProgramHandle *handle) const
Definition LightMgr.cpp:15
int GetLightMapCount() const
Definition LightMgr.h:64
Class for managing rendering operations.
Definition RenderMgr.h:21
unsigned int * GetWidth() const
Get the width of the screen.
Definition RenderMgr.h:50
unsigned int * GetHeight() const
Get the height of the screen.
Definition RenderMgr.h:59
static void BindSourceBuffer(const SFrameBuffer &buffer, const GLProgramHandle &handle, int layout)
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.