X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fframebuffer.cpp;h=8dbd2d0ee93bd20940b26326c1d2aaeb0c3f25a1;hb=3663bed18358a2399b2a8a8f7779d85e0ed81bd0;hp=d9d873bb7aa855de4491057a412178b964444f0e;hpb=a361efc05fcad11b2918f3cd7abdebe794b131d8;p=libs%2Fgl.git diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp index d9d873bb..8dbd2d0e 100644 --- a/source/framebuffer.cpp +++ b/source/framebuffer.cpp @@ -8,65 +8,212 @@ Distributed under the LGPL #include "extension.h" #include "ext_framebuffer_object.h" #include "framebuffer.h" +#include "misc.h" #include "renderbuffer.h" #include "texture2d.h" +using namespace std; + namespace Msp { namespace GL { -Framebuffer::Framebuffer() +Framebuffer::Framebuffer(unsigned i): + id(i), + dirty(0) +{ + if(id) + throw InvalidParameterValue("System framebuffer must have id 0"); + + int viewport[4]; + glGetIntegerv(GL_VIEWPORT, viewport); + width = viewport[2]; + height = viewport[3]; +} + +Framebuffer::Framebuffer(): + width(0), + height(0), + dirty(0) { - require_extension("GL_EXT_framebuffer_object"); + static RequireExtension _ext("GL_EXT_framebuffer_object"); glGenFramebuffersEXT(1, &id); - bind(); } Framebuffer::~Framebuffer() { - glDeleteFramebuffersEXT(1, &id); + if(id) + glDeleteFramebuffersEXT(1, &id); + if(current()==this) + unbind(); } -void Framebuffer::bind() const +void Framebuffer::update_attachment(unsigned mask) const +{ + if(current()==this) + { + bool has_color = false; + bool has_depth = false; + for(unsigned i=0; iget_id()); + else if(attch.type==GL_TEXTURE_2D) + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level); + else + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0); + } + + if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3) + has_color = true; + if(attch.attachment==DEPTH_ATTACHMENT) + has_depth = true; + } + + glDrawBuffer(has_color ? GL_FRONT : GL_NONE); + glDepthMask(has_depth); + } + else + dirty |= mask; +} + +void Framebuffer::check_size() { - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id); - current=this; + for(vector::iterator i=attachments.begin(); i!=attachments.end(); ++i) + if(i->type) + { + if(i->type==GL_RENDERBUFFER_EXT) + { + width = i->rbuf->get_width(); + height = i->rbuf->get_height(); + } + else if(i->type==GL_TEXTURE_2D) + { + Texture2D *tex = static_cast(i->tex); + width = tex->get_width(); + height = tex->get_height(); + } + if(current()==this) + glViewport(0, 0, width, height); + break; + } } void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf) { - maybe_bind(); - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, rbuf.get_id()); + if(!id) + throw InvalidState("Can't attach to system framebuffer"); + + unsigned i = get_attachment_index(attch); + attachments[i].set(rbuf); + update_attachment(1<(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); } -void Framebuffer::unbind() +void Framebuffer::clear(BufferBits bits) { - if(current) + Bind _bind(this, true); + glClear(bits); +} + +void Framebuffer::bind() const +{ + if(set_current(this)) { - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); - current=0; + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id); + if(dirty) + { + update_attachment(dirty); + dirty = 0; + } + if(width && height) + glViewport(0, 0, width, height); } } -void Framebuffer::maybe_bind() const +const Framebuffer *Framebuffer::current() +{ + if(!cur_obj) + cur_obj = &system(); + return cur_obj; +} + +void Framebuffer::unbind() +{ + system().bind(); +} + +Framebuffer &Framebuffer::system() +{ + static Framebuffer sys_framebuf(0); + return sys_framebuf; +} + +unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch) { - if(current!=this) - bind(); + for(unsigned i=0; i