CSEngine
Loading...
Searching...
No Matches
CSE::SFrameBuffer Class Reference

#include <SFrameBuffer.h>

Inheritance diagram for CSE::SFrameBuffer:
CSE::SResource CSE::SObject CSE::VariableBinder CSE::ReflectionObject

Public Types

enum  BufferType { RENDER = 0 , DEPTH = 1 , STENCIL = 2 }
 
enum  BufferDimension { PLANE = 0 , CUBE = 1 }
 
enum  BufferStatus { NONE = 0 , COLOR_ONLY = 1 , DEPTH_ONLY = 2 , MULTI = 3 }
 
enum  BlitType { IN_ORDER = 0 , REVERSE = 1 }
 

Public Member Functions

 RESOURCE_DEFINE_CONSTRUCTOR (SFrameBuffer)
 
void GenerateFramebuffer (BufferDimension dimension, int width, int height)
 
unsigned int GenerateRenderbuffer (BufferType type, int internalFormat)
 
STextureGenerateTexturebuffer (BufferType type, int channel, int level=0)
 
void RasterizeFramebuffer ()
 
void AttachCubeBuffer (int index, int level=0) const
 
void AttachFrameBuffer (int target=GL_FRAMEBUFFER) const
 
void DetachFrameBuffer () const
 
void ResizeFrameBuffer (int width, int height)
 
void Exterminate () override
 
void PostFrameBuffer (GLProgramHandle *handle, const CameraBase &camera)
 
STextureBlitCopiedFrameBuffer () const
 
int GetWidth () const
 
int GetHeight () const
 
const ivec2GetSize () const
 
BufferStatus GetBufferStatus () const
 
STextureGetTexture (int index) const
 
STextureGetTexture (const char *id) const
 
unsigned int GetRenderbufferID (int index) const
 
STextureGetMainColorTexture () const
 
STextureGetDepthTexture () const
 
void SetValue (std::string name_str, Arguments value) override
 
std::string PrintValue () const override
 
- Public Member Functions inherited from CSE::SResource
 SResource (std::string classType)
 
 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 ()
 
std::string GetHash () const
 
- Public Member Functions inherited from CSE::VariableBinder
- Public Member Functions inherited from CSE::ReflectionObject
 ReflectionObject (std::string type)
 
void SetClassType (std::string type)
 
const char * GetClassType () const
 
bool IsSameClass (const char *classType) const
 

Protected Member Functions

void Init (const AssetMgr::AssetReference *asset) override
 
- Protected Member Functions inherited from CSE::SResource
- Protected Member Functions inherited from CSE::VariableBinder

Additional Inherited Members

- Static Public Member Functions inherited from CSE::SResource
template<class T >
static T * Create (const std::string &name)
 
static SResourceCreate (const std::string &name, const std::string &classType)
 
template<class T >
static T * Create (const AssetMgr::AssetReference *asset)
 
static SResourceCreate (const AssetMgr::AssetReference *asset, const std::string &classType)
 
template<class T >
static T * Get (std::string name)
 
static SResourceGet (std::string &name)
 
- Static Public Member Functions inherited from CSE::ReflectionObject
static ReflectionObjectNewObject (const std::string &name)
 
- Protected Types inherited from CSE::VariableBinder
typedef std::vector< std::string > Arguments
 
- Protected Attributes inherited from CSE::SObject
std::string m_hash
 
- Protected Attributes inherited from CSE::ReflectionObject
std::string m_class
 

Detailed Description

See also
To use the SFrameBuffer class, you need to follow the steps below:
  1. Generate a framebuffer.
    m_frameBuffer->GenerateFramebuffer(SFrameBuffer::PLANE);
  2. Generate buffers.
    m_frameBuffer->GenerateTexturebuffer(SFrameBuffer::DEPTH, width, height, GL_DEPTH_COMPONENT);
  3. Rasterize the framebuffer.
    m_frameBuffer->RasterizeFramebuffer();

Definition at line 33 of file SFrameBuffer.h.

Member Enumeration Documentation

◆ BlitType

enum CSE::SFrameBuffer::BlitType

Definition at line 44 of file SFrameBuffer.h.

44 {
45 IN_ORDER = 0, REVERSE = 1,
46 };

◆ BufferDimension

enum CSE::SFrameBuffer::BufferDimension

Definition at line 38 of file SFrameBuffer.h.

38 {
39 PLANE = 0, CUBE = 1,
40 };

◆ BufferStatus

enum CSE::SFrameBuffer::BufferStatus

Definition at line 41 of file SFrameBuffer.h.

41 {
42 NONE = 0, COLOR_ONLY = 1, DEPTH_ONLY = 2, MULTI = 3,
43 };

◆ BufferType

enum CSE::SFrameBuffer::BufferType

Definition at line 35 of file SFrameBuffer.h.

35 {
36 RENDER = 0, DEPTH = 1, STENCIL = 2,
37 };

Member Function Documentation

◆ AttachCubeBuffer()

void SFrameBuffer::AttachCubeBuffer ( int index,
int level = 0 ) const

Definition at line 188 of file SFrameBuffer.cpp.

188 {
189 if (m_dimension != CUBE) return;
190
191 for (const auto& buffer: m_buffers) {
192 const auto& texture = buffer->texture;
193 if (texture == nullptr) continue;
194 glFramebufferTexture2D(GL_FRAMEBUFFER, GenerateAttachmentType(buffer->type, false),
195 GL_TEXTURE_CUBE_MAP_POSITIVE_X + index, texture->GetTextureID(), level);
196 }
197}

◆ AttachFrameBuffer()

void SFrameBuffer::AttachFrameBuffer ( int target = GL_FRAMEBUFFER) const

Definition at line 199 of file SFrameBuffer.cpp.

199 {
200 glViewport(0, 0, m_size->x, m_size->y);
201 glBindFramebuffer(target, m_fbo);
202}

◆ BlitCopiedFrameBuffer()

STexture * SFrameBuffer::BlitCopiedFrameBuffer ( ) const

Definition at line 272 of file SFrameBuffer.cpp.

272 {
273 if (m_postObject.copyFbo < 0) {
274 m_postObject.copyBuffer = new STexture(static_cast<STexture::Type>(m_dimension));
275 m_postObject.copyBuffer->InitTexture(m_size->x, m_size->y, GL_RGB, GenerateInternalFormat(GL_RGB),
276 GenerateInternalType(GL_RGB));
277 m_postObject.copyTexId = m_postObject.copyBuffer->GetTextureID();
278
279 unsigned int copy_fbo;
280 glGenFramebuffers(1, &copy_fbo);
281 glBindFramebuffer(GL_FRAMEBUFFER, copy_fbo);
282 m_postObject.copyFbo = copy_fbo;
283 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_postObject.copyTexId, 0);
284 }
285
286 AttachFrameBuffer(GL_READ_FRAMEBUFFER);
287 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_postObject.copyFbo);
288 glBlitFramebuffer(0, 0, m_size->x, m_size->y, 0, 0, m_size->x, m_size->y, GL_COLOR_BUFFER_BIT, GL_NEAREST);
289 return m_postObject.copyBuffer;
290}

◆ DetachFrameBuffer()

void SFrameBuffer::DetachFrameBuffer ( ) const

Definition at line 204 of file SFrameBuffer.cpp.

204 {
205 glBindFramebuffer(GL_FRAMEBUFFER, 0);
206}

◆ Exterminate()

void SFrameBuffer::Exterminate ( )
overridevirtual

Implements CSE::SObject.

Definition at line 37 of file SFrameBuffer.cpp.

37 {
38 glDeleteFramebuffers(1, &m_fbo);
39
40 for (const auto& buffer: m_buffers) {
41 ReleaseBufferObject(buffer);
42 }
43 m_buffers.clear();
44 m_depthBuffer = m_mainColorBuffer = nullptr;
45 SAFE_DELETE(m_size);
46}

◆ GenerateFramebuffer()

void SFrameBuffer::GenerateFramebuffer ( BufferDimension dimension,
int width,
int height )

This is the first function called when creating a framebuffer. You specify the dimensions of the framebuffer through that function.

Parameters
dimensionSets the dimensions of the framebuffer. The default is SFrameBuffer::PLANE.
widthWrite the width of the render buffer.
heightWrites the height of the render buffer.

Definition at line 104 of file SFrameBuffer.cpp.

104 {
105 m_dimension = dimension;
106 m_size->x = width;
107 m_size->y = height;
108
109 glGenFramebuffers(1, &m_fbo);
110 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
111}

◆ GenerateRenderbuffer()

unsigned int SFrameBuffer::GenerateRenderbuffer ( BufferType type,
int internalFormat )

Creates a render buffer in a non-texture format.

Parameters
typeSpecifies the type of buffer to create. The default is RENDER.
internalFormatSets the internal format for the render buffer. Format details can be found at this link.
Returns
Returns the ID value of the created render buffer. If creation fails, 0 is returned.

Definition at line 113 of file SFrameBuffer.cpp.

113 {
114 if (m_dimension == CUBE) return 0;
115
116 auto buffer = new BufferObject();
117 buffer->type = type;
118 buffer->format = internalFormat;
119
120 glGenRenderbuffers(1, &buffer->renderbufferId);
121 glBindRenderbuffer(GL_RENDERBUFFER, buffer->renderbufferId);
122 glRenderbufferStorage(GL_RENDERBUFFER, GenerateInternalFormat(internalFormat), m_size->x, m_size->y);
123
124 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
125 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GenerateAttachmentType(type, false), GL_RENDERBUFFER,
126 buffer->renderbufferId);
127
128 if (type == RENDER) {
129 if (m_mainColorBuffer == nullptr)
130 m_mainColorBuffer = buffer;
131 } else if (m_depthBuffer == nullptr) {
132 m_depthBuffer = buffer;
133 }
134 m_buffers.push_back(buffer);
135 return buffer->renderbufferId;
136}

◆ GenerateTexturebuffer()

STexture * SFrameBuffer::GenerateTexturebuffer ( BufferType type,
int channel,
int level = 0 )

Creates a render buffer in texture format.

Parameters
typeSpecifies the type of buffer to create. The default is RENDER.
channelSets the channel format for the render buffer. Format details can be found at this link.
levelSets the texture mipmap level. Default is 0.
Returns
Returns the STexture of the created render texture buffer. If creation fails, nullptr is returned.

Definition at line 138 of file SFrameBuffer.cpp.

138 {
139 auto buffer = new BufferObject();
140 buffer->type = type;
141 buffer->format = channel;
142 buffer->level = (short) level;
143 buffer->texture = new STexture(static_cast<STexture::Type>(m_dimension));
144 buffer->texture->InitTexture(m_size->x, m_size->y, channel, GenerateInternalFormat(channel),
145 GenerateInternalType(channel));
146 unsigned int texId = buffer->texture->GetTextureID();
147// if(level > 0)
148
149 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
150
151 if (m_dimension == PLANE) {
152 glFramebufferTexture2D(GL_FRAMEBUFFER, GenerateAttachmentType(type), GL_TEXTURE_2D, texId, level);
153 } else {
154 // TODO: 큐브맵 텍스쳐 바인딩 설정 필요
155 GenerateAttachmentType(buffer->type, false);
156 }
157
158 if (type == RENDER) {
159 if (m_mainColorBuffer == nullptr)
160 m_mainColorBuffer = buffer;
161 } else if (m_depthBuffer == nullptr) {
162 m_depthBuffer = buffer;
163 }
164 m_buffers.push_back(buffer);
165 return buffer->texture;
166}

Referenced by RasterizeFramebuffer().

◆ GetBufferStatus()

SFrameBuffer::BufferStatus SFrameBuffer::GetBufferStatus ( ) const

Definition at line 359 of file SFrameBuffer.cpp.

359 {
360 return m_bufferStatus;
361}

◆ GetDepthTexture()

STexture * CSE::SFrameBuffer::GetDepthTexture ( ) const
inline

Get the texture assigned to the framebuffer as depth.

Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 160 of file SFrameBuffer.h.

160 {
161 if (m_depthBuffer == nullptr) return nullptr;
162 return m_depthBuffer->texture;
163 }

◆ GetHeight()

int SFrameBuffer::GetHeight ( ) const

Definition at line 296 of file SFrameBuffer.cpp.

296 {
297 return m_size->y;
298}

◆ GetMainColorTexture()

STexture * CSE::SFrameBuffer::GetMainColorTexture ( ) const
inline

Get the texture assigned to the framebuffer as GL_COLOR_ATTACHMENT0.

Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 151 of file SFrameBuffer.h.

151 {
152 if (m_mainColorBuffer == nullptr) return nullptr;
153 return m_mainColorBuffer->texture;
154 }

◆ GetRenderbufferID()

unsigned int SFrameBuffer::GetRenderbufferID ( int index) const

Get renderbuffer ID that exist in the framebuffer.

Parameters
indexbuffer index in framebuffer
Returns
if the corresponding index is an invalid buffer ID, 0 is returned.

Definition at line 381 of file SFrameBuffer.cpp.

381 {
382 if (index < 0 || index > m_buffers.size() - 1) return 0;
383
384 auto id = m_buffers[index]->renderbufferId;
385 return id;
386}

◆ GetSize()

const ivec2 & CSE::SFrameBuffer::GetSize ( ) const
inline

Definition at line 120 of file SFrameBuffer.h.

120 {
121 return *m_size;
122 }

◆ GetTexture() [1/2]

STexture * SFrameBuffer::GetTexture ( const char * id) const

Get textures that exist in the framebuffer.

Parameters
idtexture id in framebuffer
Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 370 of file SFrameBuffer.cpp.

370 {
371 for (const auto& buffer: m_buffers) {
372 const auto& texture = buffer->texture;
373 if (texture == nullptr) continue;
374
375 std::string bufferId = texture->GetHash();
376 if (bufferId == id) return texture;
377 }
378 return nullptr;
379}

◆ GetTexture() [2/2]

STexture * SFrameBuffer::GetTexture ( int index) const

Get textures that exist in the framebuffer.

Parameters
indexbuffer index in framebuffer
Returns
if the corresponding index is an invalid texture type, nullptr is returned.

Definition at line 363 of file SFrameBuffer.cpp.

363 {
364 if (index < 0 || index > m_buffers.size() - 1) return nullptr;
365
366 auto texture = m_buffers[index]->texture;
367 return texture;
368}

◆ GetWidth()

int SFrameBuffer::GetWidth ( ) const

Definition at line 292 of file SFrameBuffer.cpp.

292 {
293 return m_size->x;
294}

◆ Init()

void SFrameBuffer::Init ( const AssetMgr::AssetReference * asset)
overrideprotectedvirtual

Implements CSE::SResource.

Definition at line 49 of file SFrameBuffer.cpp.

49 {
50 const XNode* root = nullptr;
51
52 try {
53 root = XFILE(asset->name_path.c_str()).getRoot();
54 }
55 catch (int e) {
56 SAFE_DELETE(root);
57 return;
58 }
59
60 try {
61 const XNode& cse_framebuffer = root->getChild("CSEFRAMEBUFFER");
62 const XNode& info = cse_framebuffer.getChild("info");
63
64 auto width_str = info.getAttribute("width").value;
65 auto height_str = info.getAttribute("height").value;
66 auto dimension_str = info.getAttribute("dimension").value;
67
68 m_size->x = std::stoi(width_str);
69 m_size->y = std::stoi(height_str);
70 auto dimension = static_cast<BufferDimension>(std::stoi(dimension_str));
71 GenerateFramebuffer(dimension, m_size->x, m_size->y);
72
73 const XNode& buffers = cse_framebuffer.getChild("buffers");
74 int index = 0;
75 for (const auto& buffer_node: buffers.children) {
76 auto type_str = buffer_node.getAttribute("type").value;
77 auto format_str = buffer_node.getAttribute("format").value;
78 auto isTexture_str = buffer_node.getAttribute("isTexture").value;
79
80 auto type = static_cast<BufferType>(std::stoi(type_str));
81 auto format = std::stoi(format_str);
82 auto isTexture = std::stoi(isTexture_str) == 1;
83
84 if (isTexture) {
85 const auto& texture = GenerateTexturebuffer(type, format);
86 const auto& textureName = "?Texture" + std::to_string(index);
87 texture->SetName(GetName() + textureName);
88 texture->SetAbsoluteID(GetHash() + textureName);
89 } else {
90 GenerateRenderbuffer(type, format);
91 }
92 ++index;
93 }
95 }
96 catch (int e) {
97 SAFE_DELETE(root);
98 auto resMgr = CORE->GetCore(ResMgr);
99 resMgr->Remove(this);
100 }
101 SAFE_DELETE(root);
102}
STexture * GenerateTexturebuffer(BufferType type, int channel, int level=0)
unsigned int GenerateRenderbuffer(BufferType type, int internalFormat)
void GenerateFramebuffer(BufferDimension dimension, int width, int height)
Definition XML.h:77
Definition XML.h:43

◆ PostFrameBuffer()

void SFrameBuffer::PostFrameBuffer ( GLProgramHandle * handle,
const CameraBase & camera )

Definition at line 234 of file SFrameBuffer.cpp.

234 {
235 if (m_mainColorBuffer == nullptr || m_depthBuffer == nullptr) {
236 Exterminate();
237 GenerateFramebuffer(PLANE, m_size->x, m_size->y);
238 GenerateTexturebuffer(RENDER, GL_RGB);
239 GenerateTexturebuffer(DEPTH, GL_DEPTH_COMPONENT);
241 }
242
243 if (m_postObject.handle == nullptr) {
244 m_postObject.handle = handle;
245 m_postObject.color = m_postObject.handle->UniformLocation("post.color")->id;
246 m_postObject.depth = m_postObject.handle->UniformLocation("post.depth")->id;
247 }
248
249 // Blit for copy buffer
250 BlitCopiedFrameBuffer();
251
252 const auto& colorTexture = m_postObject.copyBuffer;
253 const auto& depthTexture = m_depthBuffer->texture;
254 const float sizeRaw[2] = { static_cast<float>(m_size->x), static_cast<float>(m_size->y) };
255 const auto& uniforms = m_postObject.handle->Uniforms;
256
257 AttachFrameBuffer();
258 glClear(GL_COLOR_BUFFER_BIT);
259 glViewport(0, 0, m_size->x, m_size->y);
260 glUseProgram(m_postObject.handle->Program);
261 colorTexture->Bind(m_postObject.color, 0);
262 depthTexture->Bind(m_postObject.depth, 1);
263 const auto& cameraStruct = camera.GetCameraMatrixStruct();
264 ShaderUtil::BindCameraToShader(*m_postObject.handle, cameraStruct.camera, cameraStruct.cameraPosition,
265 cameraStruct.projection, mat4::Identity());
266 if (uniforms.SourceBufferSize >= 0)
267 glUniform2fv(uniforms.SourceBufferSize, 1, sizeRaw);
268
269 ShaderUtil::BindAttributeToPlane();
270}
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.

◆ PrintValue()

std::string SFrameBuffer::PrintValue ( ) const
overridevirtual

Implements CSE::VariableBinder.

Definition at line 391 of file SFrameBuffer.cpp.

391 {
392 return {};
393}

◆ RasterizeFramebuffer()

void SFrameBuffer::RasterizeFramebuffer ( )

Create all the render buffers to allocate to the framebuffer and proceed with rasterization. You can use the framebuffer after calling the function.

Definition at line 168 of file SFrameBuffer.cpp.

168 {
169 glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
170 m_buffers.reserve(m_buffers.size());
171
172 // tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
173 if (m_colorAttachmentSize > 1) {
174 std::vector<unsigned int> attachments;
175 attachments.reserve(m_colorAttachmentSize);
176 for (int i = 0; i < m_colorAttachmentSize; ++i) {
177 attachments.push_back(GL_COLOR_ATTACHMENT0 + i);
178 }
179 glDrawBuffers(m_colorAttachmentSize, &attachments[0]);
180 }
181
182 if (m_bufferStatus == COLOR_ONLY) {
183 GenerateTexturebuffer(SFrameBuffer::DEPTH, GL_DEPTH_COMPONENT);
184 m_bufferStatus = MULTI;
185 }
186}

References GenerateTexturebuffer().

◆ ResizeFrameBuffer()

void SFrameBuffer::ResizeFrameBuffer ( int width,
int height )

Definition at line 208 of file SFrameBuffer.cpp.

208 {
209 std::vector<BufferObject*> origBufferVector(m_buffers);
210
211 m_buffers.clear();
212 m_buffers.reserve(origBufferVector.size());
213 m_colorAttachmentSize = 0;
214 glDeleteFramebuffers(1, &m_fbo);
215 GenerateFramebuffer(m_dimension, width, height);
216 m_depthBuffer = m_mainColorBuffer = nullptr;
217
218 for (const auto& buffer: origBufferVector) {
219 auto type = buffer->type;
220 auto format = buffer->format;
221 auto level = buffer->level;
222 auto isTexture = buffer->texture != nullptr;
223
224 ReleaseBufferObject(buffer);
225
226 if (isTexture)
227 GenerateTexturebuffer(type, format, level);
228 else
229 GenerateRenderbuffer(type, format);
230 }
232}

◆ SetValue()

void SFrameBuffer::SetValue ( std::string name_str,
Arguments value )
overridevirtual

Used to get serialized values. All classes that inherit from VariableBinder will unconditionally call this function first during initialization.

Parameters
name_strName of values
valueSerialized values

Implements CSE::VariableBinder.

Definition at line 388 of file SFrameBuffer.cpp.

388 {
389}

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