X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fframebuffer.cpp;h=ea28d5db2d261eeb999d1e6c05eea38b6aefe70a;hb=df1f68d366e145716225f1a4dd223b0129280fb2;hp=229c7f0a094c01ce64bfc43563a62882b6606164;hpb=98c810b6d2256aa65986bbde12c38917678121bb;p=libs%2Fgl.git diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp index 229c7f0a..ea28d5db 100644 --- a/source/framebuffer.cpp +++ b/source/framebuffer.cpp @@ -17,162 +17,201 @@ using namespace std; namespace Msp { namespace GL { +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) + height(0), + dirty(0) { 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(!cur_fbo) - get(GL_VIEWPORT, sys_viewport); - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id); - cur_fbo=this; - if(width && height) - viewport(0, 0, width, height); -} - -void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf) -{ - maybe_bind(); - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, rbuf.get_id()); - get_or_create_attachment(attch)=rbuf; - check_size(); -} + if(current()==this) + { + GLenum color_buf = GL_NONE; + for(unsigned i=0; iget_id()); + else if(attch.type==GL_TEXTURE_2D) + { + static_cast(attch.tex)->allocate(attch.level); + 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) + color_buf = attch.attachment; + } -void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, int level) -{ - maybe_bind(); - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, tex.get_target(), tex.get_id(), level); - get_or_create_attachment(attch)=tex; - check_size(); + glDrawBuffer(color_buf); + } + else + dirty |= mask; } -void Framebuffer::detach(FramebufferAttachment attch) +void Framebuffer::check_size() { - maybe_bind(); for(vector::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->attachment==attch) + if(i->type) { if(i->type==GL_RENDERBUFFER_EXT) - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, 0); + { + width = i->rbuf->get_width(); + height = i->rbuf->get_height(); + } else if(i->type==GL_TEXTURE_2D) - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, GL_TEXTURE_2D, 0, 0); - attachments.erase(i); - check_size(); - return; + { + Texture2D *tex = static_cast(i->tex); + width = tex->get_width(); + height = tex->get_height(); + } + if(current()==this) + glViewport(0, 0, width, height); + break; } } -FramebufferStatus Framebuffer::check_status() const +void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf) { - maybe_bind(); - return static_cast(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); + 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)); } -Framebuffer::Attachment &Framebuffer::get_or_create_attachment(FramebufferAttachment attch) +void Framebuffer::clear(BufferBits bits) { - for(vector::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->attachment==attch) - return *i; - attachments.push_back(Attachment(attch)); - return attachments.back(); + Bind _bind(this, true); + glClear(bits); } -void Framebuffer::check_size() +void Framebuffer::bind() const { - if(!attachments.empty()) + if(set_current(this)) { - const Attachment &attch=attachments.front(); - if(attch.type==GL_RENDERBUFFER_EXT) + glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id); + if(dirty) { - width=attch.rbuf->get_width(); - height=attch.rbuf->get_height(); + update_attachment(dirty); + dirty = 0; } - else if(attch.type==GL_TEXTURE_2D) - { - Texture2D *tex=static_cast(attch.tex); - width=tex->get_width(); - height=tex->get_height(); - } - if(cur_fbo==this) - viewport(0, 0, width, height); + if(width && height) + glViewport(0, 0, width, height); } } -const Framebuffer *Framebuffer::cur_fbo=0; -int Framebuffer::sys_viewport[4]={ 0, 0, 1, 1 }; - - -Framebuffer::Attachment::Attachment(FramebufferAttachment a): - attachment(a), - type(0) -{ } +const Framebuffer *Framebuffer::current() +{ + if(!cur_obj) + cur_obj = &system(); + return cur_obj; +} -Framebuffer::Attachment &Framebuffer::Attachment::operator=(Renderbuffer &r) +void Framebuffer::unbind() { - type=GL_RENDERBUFFER_EXT; - rbuf=&r; - return *this; + system().bind(); } -Framebuffer::Attachment &Framebuffer::Attachment::operator=(Texture &t) +Framebuffer &Framebuffer::system() { - type=t.get_target(); - tex=&t; - return *this; + static Framebuffer sys_framebuf(0); + return sys_framebuf; } - -void viewport(int x, int y, unsigned w, unsigned h) +unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch) { - glViewport(x, y, w, h); + for(unsigned i=0; i