2024-08-01 23:32:51 +00:00
|
|
|
#include "FrameBuffer.h"
|
|
|
|
|
2024-08-22 15:55:30 +00:00
|
|
|
FrameBuffer::FrameBuffer()
|
2024-08-01 23:32:51 +00:00
|
|
|
{
|
|
|
|
glGenFramebuffers(1, &m_FBO);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
|
|
|
|
|
2024-08-22 15:55:30 +00:00
|
|
|
m_Texture = new Texture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::AttachTexture(int width, int height)
|
|
|
|
{
|
|
|
|
Texture::ToImage(width, height, nullptr);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture->GetID(), 0);
|
2024-08-01 23:32:51 +00:00
|
|
|
|
|
|
|
glGenRenderbuffers(1, &m_RBO);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, m_RBO);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_RBO);
|
|
|
|
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
FrameBuffer::~FrameBuffer()
|
|
|
|
{
|
2024-08-22 15:55:30 +00:00
|
|
|
Shutdown();
|
2024-08-01 23:32:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 15:55:30 +00:00
|
|
|
Texture* FrameBuffer::GetFrameTexture() const
|
2024-08-01 23:32:51 +00:00
|
|
|
{
|
|
|
|
return m_Texture;
|
|
|
|
}
|
|
|
|
|
2024-08-22 15:55:30 +00:00
|
|
|
FrameBuffer FrameBuffer::Create()
|
2024-08-01 23:32:51 +00:00
|
|
|
{
|
2024-08-22 15:55:30 +00:00
|
|
|
return FrameBuffer{};
|
2024-08-01 23:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::Shutdown() const
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, &m_FBO);
|
|
|
|
glDeleteBuffers(1, &m_RBO);
|
2024-08-22 15:55:30 +00:00
|
|
|
m_Texture->Shutdown();
|
2024-08-01 23:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::RescaleFrameBuffer(int width, int height) const
|
|
|
|
{
|
2024-08-22 15:55:30 +00:00
|
|
|
m_Texture->Bind();
|
|
|
|
|
2024-08-01 23:32:51 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2024-08-22 15:55:30 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture->GetID(), 0);
|
2024-08-01 23:32:51 +00:00
|
|
|
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, m_RBO);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_RBO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::Bind() const
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrameBuffer::Unbind()
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
}
|