@@ -78,6 +78,11 @@ void Framebuffer::Unbind()
7878 glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0 );
7979}
8080
81+ void Framebuffer::Unbind (GLuint defaultFramebufferObject)
82+ {
83+ glBindFramebuffer (GL_DRAW_FRAMEBUFFER, defaultFramebufferObject);
84+ }
85+
8186bool Framebuffer::SetSize (int width, int height)
8287{
8388 if (width == 0 || height == 0 ||
@@ -86,6 +91,9 @@ bool Framebuffer::SetSize(int width, int height)
8691 return false ;
8792 }
8893
94+ GLint previousFramebuffer{};
95+ glGetIntegerv (GL_FRAMEBUFFER_BINDING, &previousFramebuffer);
96+
8997 m_width = width;
9098 m_height = height;
9199
@@ -98,7 +106,7 @@ bool Framebuffer::SetSize(int width, int height)
98106 glFramebufferTexture2D (GL_FRAMEBUFFER, texture.first , GL_TEXTURE_2D, texture.second ->Texture ()->TextureID (), 0 );
99107 }
100108 }
101- glBindFramebuffer (GL_FRAMEBUFFER, 0 );
109+ glBindFramebuffer (GL_FRAMEBUFFER, previousFramebuffer );
102110
103111 return true ;
104112}
@@ -202,6 +210,9 @@ void Framebuffer::CreateColorAttachment(int framebufferIndex, int attachmentInde
202210 return ;
203211 }
204212
213+ GLint previousFramebuffer{};
214+ glGetIntegerv (GL_FRAMEBUFFER_BINDING, &previousFramebuffer);
215+
205216 auto textureAttachment = std::make_shared<TextureAttachment>(internalFormat, format, type, m_width, m_height);
206217 const auto texture = textureAttachment->Texture ();
207218 m_attachments.at (framebufferIndex).insert ({GL_COLOR_ATTACHMENT0 + attachmentIndex, std::move (textureAttachment)});
@@ -212,7 +223,7 @@ void Framebuffer::CreateColorAttachment(int framebufferIndex, int attachmentInde
212223 glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachmentIndex, GL_TEXTURE_2D, texture->TextureID (), 0 );
213224 }
214225 UpdateDrawBuffers (framebufferIndex);
215- glBindFramebuffer (GL_FRAMEBUFFER, 0 );
226+ glBindFramebuffer (GL_FRAMEBUFFER, previousFramebuffer );
216227}
217228
218229void Framebuffer::RemoveColorAttachment (int framebufferIndex, int attachmentIndex)
@@ -243,6 +254,9 @@ void Framebuffer::CreateDepthAttachment(int framebufferIndex)
243254 return ;
244255 }
245256
257+ GLint previousFramebuffer{};
258+ glGetIntegerv (GL_FRAMEBUFFER_BINDING, &previousFramebuffer);
259+
246260 auto textureAttachment = std::make_shared<TextureAttachment>(TextureAttachment::AttachmentType::Depth, m_width, m_height);
247261 const auto texture = textureAttachment->Texture ();
248262 m_attachments.at (framebufferIndex).insert ({GL_DEPTH_ATTACHMENT, std::move (textureAttachment)});
@@ -253,7 +267,7 @@ void Framebuffer::CreateDepthAttachment(int framebufferIndex)
253267 glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture->TextureID (), 0 );
254268 }
255269 UpdateDrawBuffers (framebufferIndex);
256- glBindFramebuffer (GL_FRAMEBUFFER, 0 );
270+ glBindFramebuffer (GL_FRAMEBUFFER, previousFramebuffer );
257271}
258272
259273void Framebuffer::RemoveDepthAttachment (int framebufferIndex)
@@ -268,6 +282,9 @@ void Framebuffer::CreateStencilAttachment(int framebufferIndex)
268282 return ;
269283 }
270284
285+ GLint previousFramebuffer{};
286+ glGetIntegerv (GL_FRAMEBUFFER_BINDING, &previousFramebuffer);
287+
271288 auto textureAttachment = std::make_shared<TextureAttachment>(TextureAttachment::AttachmentType::Stencil, m_width, m_height);
272289 const auto texture = textureAttachment->Texture ();
273290 m_attachments.at (framebufferIndex).insert ({GL_STENCIL_ATTACHMENT, std::move (textureAttachment)});
@@ -278,7 +295,7 @@ void Framebuffer::CreateStencilAttachment(int framebufferIndex)
278295 glFramebufferTexture2D (GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texture->TextureID (), 0 );
279296 }
280297 UpdateDrawBuffers (framebufferIndex);
281- glBindFramebuffer (GL_FRAMEBUFFER, 0 );
298+ glBindFramebuffer (GL_FRAMEBUFFER, previousFramebuffer );
282299}
283300
284301void Framebuffer::RemoveStencilAttachment (int framebufferIndex)
@@ -293,6 +310,9 @@ void Framebuffer::CreateDepthStencilAttachment(int framebufferIndex)
293310 return ;
294311 }
295312
313+ GLint previousFramebuffer{};
314+ glGetIntegerv (GL_FRAMEBUFFER_BINDING, &previousFramebuffer);
315+
296316 auto textureAttachment = std::make_shared<TextureAttachment>(TextureAttachment::AttachmentType::DepthStencil, m_width, m_height);
297317 const auto texture = textureAttachment->Texture ();
298318 m_attachments.at (framebufferIndex).insert ({GL_DEPTH_STENCIL_ATTACHMENT, std::move (textureAttachment)});
@@ -303,7 +323,7 @@ void Framebuffer::CreateDepthStencilAttachment(int framebufferIndex)
303323 glFramebufferTexture2D (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texture->TextureID (), 0 );
304324 }
305325 UpdateDrawBuffers (framebufferIndex);
306- glBindFramebuffer (GL_FRAMEBUFFER, 0 );
326+ glBindFramebuffer (GL_FRAMEBUFFER, previousFramebuffer );
307327}
308328
309329void Framebuffer::RemoveDepthStencilAttachment (int framebufferIndex)
0 commit comments