]> git.tdb.fi Git - libs/gl.git/blobdiff - source/framebuffer.cpp
Complete rewrite of extension handling
[libs/gl.git] / source / framebuffer.cpp
index 3ac742bd209fd0e89c05d723c082524b4799c02c..43b0f94b82e4c882327ef686491453a1bc7cf263 100644 (file)
@@ -1,4 +1,4 @@
-#include "extension.h"
+#include "error.h"
 #include "ext_framebuffer_blit.h"
 #include "ext_framebuffer_object.h"
 #include "framebuffer.h"
@@ -16,7 +16,7 @@ Framebuffer::Framebuffer(unsigned i):
        dirty(0)
 {
        if(id)
-               throw InvalidParameterValue("System framebuffer must have id 0");
+               throw invalid_argument("System framebuffer must have id 0");
 
        int viewport[4];
        glGetIntegerv(GL_VIEWPORT, viewport);
@@ -29,15 +29,15 @@ Framebuffer::Framebuffer():
        height(0),
        dirty(0)
 {
-       static RequireExtension _ext("GL_EXT_framebuffer_object");
+       static Require _req(EXT_framebuffer_object);
 
-       glGenFramebuffersEXT(1, &id);
+       glGenFramebuffers(1, &id);
 }
 
 Framebuffer::~Framebuffer()
 {
        if(id)
-               glDeleteFramebuffersEXT(1, &id);
+               glDeleteFramebuffers(1, &id);
        if(current()==this)
                unbind();
 }
@@ -52,15 +52,20 @@ void Framebuffer::update_attachment(unsigned mask) const
                        const Attachment &attch = attachments[i];
                        if(mask&(1<<i))
                        {
-                               if(attch.type==GL_RENDERBUFFER_EXT)
-                                       glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, GL_RENDERBUFFER_EXT, attch.rbuf->get_id());
+                               if(attch.type==GL_RENDERBUFFER)
+                                       glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
                                else if(attch.type==GL_TEXTURE_2D)
                                {
                                        static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
-                                       glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
+                                       glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
+                               }
+                               else if(attch.type==GL_TEXTURE_CUBE_MAP)
+                               {
+                                       static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
+                                       glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
                                }
                                else
-                                       glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0);
+                                       glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
                        }
 
                        if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
@@ -78,7 +83,7 @@ void Framebuffer::check_size()
        for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
                if(i->type)
                {
-                       if(i->type==GL_RENDERBUFFER_EXT)
+                       if(i->type==GL_RENDERBUFFER)
                        {
                                width = i->rbuf->get_width();
                                height = i->rbuf->get_height();
@@ -89,16 +94,30 @@ void Framebuffer::check_size()
                                width = tex->get_width();
                                height = tex->get_height();
                        }
+                       else if(i->type==GL_TEXTURE_CUBE_MAP)
+                       {
+                               width = static_cast<TextureCube *>(i->tex)->get_size();
+                               height = width;
+                       }
                        if(current()==this)
                                glViewport(0, 0, width, height);
                        break;
                }
 }
 
+unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
+{
+       for(unsigned i=0; i<attachments.size(); ++i)
+               if(attachments[i].attachment==attch)
+                       return i;
+       attachments.push_back(Attachment(attch));
+       return attachments.size()-1;
+}
+
 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
 {
        if(!id)
-               throw InvalidState("Can't attach to system framebuffer");
+               throw invalid_operation("Framebuffer::attach");
 
        unsigned i = get_attachment_index(attch);
        attachments[i].set(rbuf);
@@ -109,10 +128,21 @@ void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
 {
        if(!id)
-               throw InvalidState("Can't attach to system framebuffer");
+               throw invalid_operation("Framebuffer::attach");
+
+       unsigned i = get_attachment_index(attch);
+       attachments[i].set(tex, 0, level);
+       update_attachment(1<<i);
+       check_size();
+}
+
+void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
+{
+       if(!id)
+               throw invalid_operation("Framebuffer::attach");
 
        unsigned i = get_attachment_index(attch);
-       attachments[i].set(tex, level);
+       attachments[i].set(tex, face, level);
        update_attachment(1<<i);
        check_size();
 }
@@ -120,7 +150,7 @@ void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned l
 void Framebuffer::detach(FramebufferAttachment attch)
 {
        if(!id)
-               throw InvalidState("Can't detach from system framebuffer");
+               throw invalid_operation("Framebuffer::detach");
 
        unsigned i = get_attachment_index(attch);
        attachments[i].clear();
@@ -131,7 +161,7 @@ void Framebuffer::detach(FramebufferAttachment attch)
 FramebufferStatus Framebuffer::check_status() const
 {
        Bind _bind(this, true);
-       return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
+       return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
 }
 
 void Framebuffer::clear(BufferBits bits)
@@ -142,12 +172,12 @@ void Framebuffer::clear(BufferBits bits)
 
 void Framebuffer::blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1, int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter)
 {
-       static RequireExtension _ext("GL_EXT_framebuffer_blit");
+       static Require _req(EXT_framebuffer_blit);
 
        const Framebuffer *old = current();
        if(set_current(this))
        {
-               glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, id);
+               glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
                if(dirty)
                {
                        update_attachment(dirty);
@@ -155,12 +185,12 @@ void Framebuffer::blit_from(const Framebuffer &other, int sx0, int sy0, int sx1,
                }
        }
        if(old!=&other)
-               glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, other.id);
+               glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
 
-       glBlitFramebufferEXT(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
+       glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
 
        set_current(old);
-       glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (old ? old->id : 0));
+       glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
 }
 
 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
@@ -177,7 +207,7 @@ void Framebuffer::bind() const
 {
        if(set_current(this))
        {
-               glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
+               glBindFramebuffer(GL_FRAMEBUFFER, id);
                if(dirty)
                {
                        update_attachment(dirty);
@@ -206,15 +236,6 @@ Framebuffer &Framebuffer::system()
        return sys_framebuf;
 }
 
-unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
-{
-       for(unsigned i=0; i<attachments.size(); ++i)
-               if(attachments[i].attachment==attch)
-                       return i;
-       attachments.push_back(Attachment(attch));
-       return attachments.size()-1;
-}
-
 
 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
        attachment(a),
@@ -224,15 +245,16 @@ Framebuffer::Attachment::Attachment(FramebufferAttachment a):
 
 void Framebuffer::Attachment::set(Renderbuffer &r)
 {
-       type = GL_RENDERBUFFER_EXT;
+       type = GL_RENDERBUFFER;
        rbuf = &r;
        level = 0;
 }
 
-void Framebuffer::Attachment::set(Texture &t, unsigned l)
+void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
 {
        type = t.get_target();
        tex = &t;
+       cube_face = f;
        level = l;
 }