CSEngine
Loading...
Searching...
No Matches
SGBuffer.cpp
1#include "SGBuffer.h"
2#include "STexture.h"
3#include "SMaterial.h"
4#include "SFrameBuffer.h"
5#include "ShaderUtil.h"
6#include "../GLProgramHandle.h"
7#include "../../Manager/EngineCore.h"
8#include "RenderInterfaces.h"
9
10using namespace CSE;
11
12ResMgr* resMgr = nullptr;
13
14
15SGBuffer::SGBuffer() {
16 resMgr = CORE->GetCore(ResMgr);
17}
18
19SGBuffer::~SGBuffer() = default;
20
21void SGBuffer::GenerateGBuffer(int width, int height) {
22 if(m_geometryFrameBuffer != nullptr) return;
23
24 m_width = width;
25 m_height = height;
26
27 m_geometryFrameBuffer = new SFrameBuffer();
28 m_geometryFrameBuffer->SetName(m_geometryFrameBuffer->GetName() + " - GBuffer");
29 m_geometryFrameBuffer->GenerateFramebuffer(SFrameBuffer::PLANE, width, height);
30
31 m_firstTexture = m_geometryFrameBuffer->GenerateTexturebuffer(SFrameBuffer::RENDER, GL_RGBA);
32 m_secondTexture = m_geometryFrameBuffer->GenerateTexturebuffer(SFrameBuffer::RENDER, GL_RGBA);
33 m_depthTexture = m_geometryFrameBuffer->GenerateTexturebuffer(SFrameBuffer::DEPTH, GL_DEPTH_COMPONENT);
34 m_geometryFrameBuffer->RasterizeFramebuffer();
35
36// if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
37// std::cout << "Framebuffer not complete!" << std::endl;
38}
39
40void SGBuffer::AttachGeometryFrameBuffer() const {
41 if(m_geometryFrameBuffer == nullptr) return;
42 m_geometryFrameBuffer->AttachFrameBuffer();
43}
44
45void SGBuffer::AttachGeometryFrameBuffer(int target) const {
46 if(m_geometryFrameBuffer == nullptr) return;
47
48 m_geometryFrameBuffer->AttachFrameBuffer(target);
49}
50
51void SGBuffer::ResizeGBuffer(int width, int height) {
52 ReleaseGBuffer();
53 GenerateGBuffer(width, height);
54}
55
56void SGBuffer::ReleaseGBuffer() {
57 if(m_geometryFrameBuffer != nullptr) {
58 resMgr->Remove(m_geometryFrameBuffer);
59 SAFE_DELETE(m_geometryFrameBuffer);
60 }
61}
62
63void SGBuffer::BindLightPass(GLProgramHandle* lightPassHandle) {
64 if(lightPassHandle == nullptr || m_lightPassHandle != nullptr) return;
65
66 m_lightPassHandle = lightPassHandle;
67 m_firstTextureId = m_lightPassHandle->UniformLocation("geo.normal.mr")->id;
68 m_secondTextureId = m_lightPassHandle->UniformLocation("geo.albedo.a")->id;
69 m_depthTextureId = m_lightPassHandle->UniformLocation("geo.depth")->id;
70}
71
72void SGBuffer::AttachLightPass() const {
73 glUseProgram(m_lightPassHandle->Program);
74}
75
76void SGBuffer::AttachLightPassTexture(int textureLayout) const {
77 m_firstTexture->Bind(m_firstTextureId, textureLayout);
78 m_secondTexture->Bind(m_secondTextureId, textureLayout + 1);
79 m_depthTexture->Bind(m_depthTextureId, textureLayout + 2);
80}
81
82void SGBuffer::RenderLightPass() const {
83 ShaderUtil::BindAttributeToPlane();
84}
85
86const std::vector<SIRender*>& SGBuffer::GetRendersLayer() const {
87 return m_rendersLayer;
88}
89
90void SGBuffer::PushBackToLayer(SIRender* render) {
91 m_rendersLayer.push_back(render);
92}
93
94void SGBuffer::RemoveToLayer(SIRender* render) {
95 m_rendersLayer.erase(std::remove(m_rendersLayer.begin(), m_rendersLayer.end(), render), m_rendersLayer.end());
96}
97
98int SGBuffer::GetWidth() const {
99 return m_width;
100}
101
102int SGBuffer::GetHeight() const {
103 return m_height;
104}
105
106GLProgramHandle* SGBuffer::GetLightPassHandle() const {
107 return m_lightPassHandle;
108}
STexture * GenerateTexturebuffer(BufferType type, int channel, int level=0)
void GenerateFramebuffer(BufferDimension dimension, int width, int height)