2025-07-05 13:28:41 +03:30
|
|
|
#include <engine/platform/graphics/opengl/framebuffers.hpp>
|
2025-07-05 11:33:43 +03:30
|
|
|
#include <glad/gl.h>
|
2021-07-29 17:12:13 +04:30
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
2021-07-15 15:46:28 +04:30
|
|
|
namespace Light {
|
|
|
|
|
2025-07-05 13:28:41 +03:30
|
|
|
glFramebuffer::glFramebuffer(const FramebufferSpecification &specification)
|
2025-07-05 14:23:01 +03:30
|
|
|
: m_specification(specification)
|
|
|
|
, m_buffer_id(NULL)
|
|
|
|
, m_color_attachment_id(NULL)
|
|
|
|
, m_depth_stencil_attachment_id(NULL)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
resize({ specification.width, specification.height });
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
glFramebuffer::~glFramebuffer()
|
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
glDeleteFramebuffers(1, &m_buffer_id);
|
|
|
|
glDeleteTextures(1, &m_color_attachment_id);
|
|
|
|
// glDeleteTextures(1, &m_depth_stencil_attachment_id);
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void glFramebuffer::bind_as_target(const glm::vec4 &clearColor)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
|
|
|
// #todo: use viewport instead of default x=0, y=0
|
2025-07-05 14:23:01 +03:30
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_buffer_id);
|
|
|
|
glViewport(0, 0, m_specification.width, m_specification.height);
|
2022-03-07 21:57:00 +03:30
|
|
|
|
|
|
|
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void glFramebuffer::bind_as_resource()
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_log(err, "NO_IMPLEMENT!");
|
2022-03-07 21:57:00 +03:30
|
|
|
}
|
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
void glFramebuffer::resize(const glm::uvec2 &size)
|
2022-03-07 21:57:00 +03:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
if (m_buffer_id)
|
2021-07-16 19:59:14 +04:30
|
|
|
{
|
2025-07-05 14:23:01 +03:30
|
|
|
glDeleteFramebuffers(1, &m_buffer_id);
|
|
|
|
glDeleteTextures(1, &m_color_attachment_id);
|
|
|
|
// glDeleteTextures(1, &m_depth_stencil_attachment_id);
|
2021-07-16 19:59:14 +04:30
|
|
|
}
|
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
m_specification.width = std::clamp(size.x, 1u, (unsigned int)GL_MAX_TEXTURE_SIZE);
|
|
|
|
m_specification.height = std::clamp(size.y, 1u, (unsigned int)GL_MAX_TEXTURE_SIZE);
|
2021-07-16 19:59:14 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
glCreateFramebuffers(1, &m_buffer_id);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_buffer_id);
|
2021-07-16 19:59:14 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
// create color attachment
|
2025-07-05 14:23:01 +03:30
|
|
|
glCreateTextures(GL_TEXTURE_2D, 1, &m_color_attachment_id);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_color_attachment_id);
|
2025-07-05 13:28:41 +03:30
|
|
|
glTexImage2D(
|
|
|
|
GL_TEXTURE_2D,
|
|
|
|
0,
|
|
|
|
GL_RGBA8,
|
2025-07-05 14:23:01 +03:30
|
|
|
m_specification.width,
|
|
|
|
m_specification.height,
|
2025-07-05 13:28:41 +03:30
|
|
|
NULL,
|
|
|
|
GL_RGBA,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
nullptr
|
|
|
|
);
|
2025-07-05 14:23:01 +03:30
|
|
|
glTextureParameteri(m_color_attachment_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTextureParameteri(m_color_attachment_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2025-07-05 13:28:41 +03:30
|
|
|
glFramebufferTexture2D(
|
|
|
|
GL_FRAMEBUFFER,
|
|
|
|
GL_COLOR_ATTACHMENT0,
|
|
|
|
GL_TEXTURE_2D,
|
2025-07-05 14:23:01 +03:30
|
|
|
m_color_attachment_id,
|
2025-07-05 13:28:41 +03:30
|
|
|
0
|
|
|
|
);
|
2021-07-16 19:59:14 +04:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
// glTextureStorage2D(m_color_attachment_id, 0, GL_RGBA8, m_specification.width,
|
|
|
|
// m_specification.height);
|
2022-03-07 21:57:00 +03:30
|
|
|
|
2025-07-05 14:23:01 +03:30
|
|
|
// glCreateTextures(GL_TEXTURE_2D, 1, &m_depth_stencil_attachment_id);
|
|
|
|
// glBindTexture(GL_TEXTURE_2D, m_depth_stencil_attachment_id);
|
|
|
|
// glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, m_specification.width,
|
|
|
|
// m_specification.height, NULL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
|
|
|
|
// // glTextureStorage2D(m_depth_stencil_attachment_id, 0, GL_DEPTH24_STENCIL8,
|
|
|
|
// m_specification.width, m_specification.height); glFramebufferTexture2D(GL_FRAMEBUFFER,
|
|
|
|
// GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, m_depth_stencil_attachment_id, 0);
|
2025-07-05 13:28:41 +03:30
|
|
|
|
2025-07-05 15:36:53 +03:30
|
|
|
lt_assert(
|
2025-07-05 13:28:41 +03:30
|
|
|
(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE),
|
|
|
|
"Framebuffer is incomplete"
|
|
|
|
);
|
2022-03-07 21:57:00 +03:30
|
|
|
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
}
|
2021-07-15 15:46:28 +04:30
|
|
|
|
2022-03-07 21:57:00 +03:30
|
|
|
} // namespace Light
|