Elevate Engine 1
Loading...
Searching...
No Matches
OpenGLFrameBuffer.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "OpenGLFrameBuffer.h"
3
8
9// TODO : ADD GLCHECK TO EACH LINES
10
12{
13 EE_CORE_ASSERT(tex->GetWidth() > 0 && tex->GetHeight() > 0, "Framebuffer dimensions must be positive");
14
15 // Creating and binding
16 glGenFramebuffers(1, &m_frameBufferId);
17 Bind();
18
19 m_textureId = static_cast<GLuint>(reinterpret_cast<intptr_t>(tex->GetNativeHandle()));
20
21 // Bind the texture with the Frame Buffer
22 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_textureId, 0);
23 // render buffer
24 glGenRenderbuffers(1, &m_renderBufferId);
25 glBindRenderbuffer(GL_RENDERBUFFER, m_renderBufferId);
26 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, tex->GetWidth(), tex->GetHeight());
27 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_renderBufferId);
28
29 CheckCompleteness();
30 Unbind();
31}
32
34{
35 glBindFramebuffer(GL_FRAMEBUFFER, m_frameBufferId);
36}
37
39{
40 glBindFramebuffer(GL_FRAMEBUFFER, 0);
41}
42
43void Elevate::OpenGLFrameBuffer::BlitFramebufferToScreen(uint32_t screenWidth, uint32_t screenHeight) const
44{
45 GLCheck(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_frameBufferId)); // Read from this framebuffer
46 GLCheck(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); // Draw to the main framebuffer
47
48 // TODO GIVE A CHOICE TO THE USER FOR THE FILTERING
49 glBlitFramebuffer(
50 0, 0, m_texture->GetWidth(), m_texture->GetHeight(),
51 0, 0, screenWidth, screenHeight,
52 GL_COLOR_BUFFER_BIT, GL_NEAREST
53 );
54}
55
56void Elevate::OpenGLFrameBuffer::Rescale(uint32_t width, uint32_t height)
57{
58 if (width == m_texture->GetWidth() && height == m_texture->GetHeight()) return;
59 else if (width <= 0 || height <= 0) return;
60
61 Bind();
62
63 Renderer::BindTexture(m_texture);
64
65 TextureMetadata meta = m_texture->GetMetadata();
66 meta.Width = width;
67 meta.Height = height;
68
69 m_texture->SetData(nullptr, meta);
70
71 // Attacher la texture redimensionn�e au framebuffer
72 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_textureId, 0);
73
74 // Redimensionner le renderbuffer pour le depth/stencil
75 glBindRenderbuffer(GL_RENDERBUFFER, m_renderBufferId);
76 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
77 glBindRenderbuffer(GL_RENDERBUFFER, 0); // Unbind renderbuffer
78
79 CheckCompleteness();
80 Unbind();
81}
82
83bool Elevate::OpenGLFrameBuffer::CheckCompleteness() const
84{
85 uint32_t status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
86 if (status != GL_FRAMEBUFFER_COMPLETE) {
87 const char* msg = GetFramebufferStatusString(status);
88 EE_CORE_ERROR("Framebuffer error: {}", msg);
89 return false;
90 }
91 return true;
92}
93
94const char* Elevate::OpenGLFrameBuffer::GetFramebufferStatusString(uint32_t status) const
95{
96 switch (status)
97 {
98 case GL_FRAMEBUFFER_COMPLETE: return "COMPLETE";
99 case GL_FRAMEBUFFER_UNDEFINED: return "UNDEFINED";
100 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: return "INCOMPLETE_ATTACHMENT";
101 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: return "MISSING_ATTACHMENT";
102 case GL_FRAMEBUFFER_UNSUPPORTED: return "UNSUPPORTED_FORMAT";
103 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: return "INCOMPLETE_MULTISAMPLE";
104 default: return "UNKNOWN_ERROR";
105 }
106}
107
#define GLCheck(x)
Definition GLDebug.h:11
void Rescale(uint32_t width, uint32_t height) override
void Unbind() const override
void BlitFramebufferToScreen(uint32_t screenWidth, uint32_t screenHeight) const override
static void BindTexture(const std::shared_ptr< Texture > &texture, uint8_t slot=0)
Definition Renderer.cpp:160
std::shared_ptr< Texture > TexturePtr
Definition Texture.h:13