]> 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 229c7f0a094c01ce64bfc43563a62882b6606164..cf3761d0abb2d26d639ace3ac1a16c8ea9534cb9 100644 (file)
@@ -34,33 +34,36 @@ 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;
+       get_or_create_attachment(attch) = rbuf;
        check_size();
 }
 
 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;
+       get_or_create_attachment(attch) = tex;
        check_size();
 }
 
 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)
@@ -114,25 +105,24 @@ void Framebuffer::check_size()
 {
        if(!attachments.empty())
        {
-               const Attachment &attch=attachments.front();
+               const Attachment &attch = attachments.front();
                if(attch.type==GL_RENDERBUFFER_EXT)
                {
-                       width=attch.rbuf->get_width();
-                       height=attch.rbuf->get_height();
+                       width = attch.rbuf->get_width();
+                       height = attch.rbuf->get_height();
                }
                else if(attch.type==GL_TEXTURE_2D)
                {
-                       Texture2D *tex=static_cast<Texture2D *>(attch.tex);
-                       width=tex->get_width();
-                       height=tex->get_height();
+                       Texture2D *tex = static_cast<Texture2D *>(attch.tex);
+                       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 };
+int Framebuffer::sys_viewport[4] = { 0, 0, 1, 1 };
 
 
 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
@@ -142,15 +132,15 @@ Framebuffer::Attachment::Attachment(FramebufferAttachment a):
 
 Framebuffer::Attachment &Framebuffer::Attachment::operator=(Renderbuffer &r)
 {
-       type=GL_RENDERBUFFER_EXT;
-       rbuf=&r;
+       type = GL_RENDERBUFFER_EXT;
+       rbuf = &r;
        return *this;
 }
 
 Framebuffer::Attachment &Framebuffer::Attachment::operator=(Texture &t)
 {
-       type=t.get_target();
-       tex=&t;
+       type = t.get_target();
+       tex = &t;
        return *this;
 }