]> git.tdb.fi Git - libs/gl.git/blobdiff - source/framebuffer.cpp
Rework Bind and enable it to restore the old binding
[libs/gl.git] / source / framebuffer.cpp
index 6941c0342d7963208324df4c39c19812442aa6b1..cf3761d0abb2d26d639ace3ac1a16c8ea9534cb9 100644 (file)
@@ -34,17 +34,20 @@ Framebuffer::~Framebuffer()
 
 void Framebuffer::bind() const
 {
-       if(!cur_fbo)
-               get(GL_VIEWPORT, sys_viewport);
-       glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
-       cur_fbo = this;
-       if(width && height)
-               viewport(0, 0, width, height);
+       const Framebuffer *old = current();
+       if(set_current(this))
+       {
+               if(!old)
+                       get(GL_VIEWPORT, sys_viewport);
+               glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
+               if(width && height)
+                       viewport(0, 0, width, height);
+       }
 }
 
 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
 {
-       maybe_bind();
+       bind();
        glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, rbuf.get_id());
        get_or_create_attachment(attch) = rbuf;
        check_size();
@@ -52,7 +55,7 @@ void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
 
 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, int level)
 {
-       maybe_bind();
+       bind();
        glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, tex.get_target(), tex.get_id(), level);
        get_or_create_attachment(attch) = tex;
        check_size();
@@ -60,7 +63,7 @@ void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, int level)
 
 void Framebuffer::detach(FramebufferAttachment attch)
 {
-       maybe_bind();
+       bind();
        for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
                if(i->attachment==attch)
                {
@@ -76,31 +79,19 @@ void Framebuffer::detach(FramebufferAttachment attch)
 
 FramebufferStatus Framebuffer::check_status() const
 {
-       maybe_bind();
+       bind();
        return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
 }
 
-const Framebuffer *Framebuffer::current()
-{
-       return cur_fbo;
-}
-
 void Framebuffer::unbind()
 {
-       if(cur_fbo)
+       if(set_current(0))
        {
                glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
-               cur_fbo = 0;
                viewport(sys_viewport[0], sys_viewport[1], sys_viewport[2], sys_viewport[3]);
        }
 }
 
-void Framebuffer::maybe_bind() const
-{
-       if(cur_fbo!=this)
-               bind();
-}
-
 Framebuffer::Attachment &Framebuffer::get_or_create_attachment(FramebufferAttachment attch)
 {
        for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
@@ -126,12 +117,11 @@ void Framebuffer::check_size()
                        width = tex->get_width();
                        height = tex->get_height();
                }
-               if(cur_fbo==this)
+               if(current()==this)
                        viewport(0, 0, width, height);
        }
 }
 
-const Framebuffer *Framebuffer::cur_fbo = 0;
 int Framebuffer::sys_viewport[4] = { 0, 0, 1, 1 };