X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fframebuffer.cpp;h=77ac21be6b0e6ef76da3928247b54b2c69074c5b;hb=bdd9a8e26efad3ba349e4ffd58171cfee3cacfb0;hp=6941c0342d7963208324df4c39c19812442aa6b1;hpb=b617c5d7b5283ad260a77f01e42e6170cabbc03d;p=libs%2Fgl.git diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp index 6941c034..77ac21be 100644 --- a/source/framebuffer.cpp +++ b/source/framebuffer.cpp @@ -17,162 +17,202 @@ 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); + if(current()==this) + { + GLenum color_buf = GL_NONE; + 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) + color_buf = attch.attachment; + if(attch.attachment==DEPTH_ATTACHMENT) + has_depth = true; + } + + glDrawBuffer(color_buf); + glDepthMask(has_depth); + } + else + dirty |= mask; +} + +void Framebuffer::check_size() +{ + 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()); - get_or_create_attachment(attch) = rbuf; + if(!id) + throw InvalidState("Can't attach to system framebuffer"); + + unsigned i = get_attachment_index(attch); + attachments[i].set(rbuf); + update_attachment(1<::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->attachment==attch) - { - if(i->type==GL_RENDERBUFFER_EXT) - glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch, GL_RENDERBUFFER_EXT, 0); - else if(i->type==GL_TEXTURE_2D) - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch, GL_TEXTURE_2D, 0, 0); - attachments.erase(i); - check_size(); - return; - } + if(!id) + throw InvalidState("Can't detach from system framebuffer"); + + unsigned i = get_attachment_index(attch); + attachments[i].clear(); + update_attachment(1<(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)); } -const Framebuffer *Framebuffer::current() +void Framebuffer::clear(BufferBits bits) { - return cur_fbo; + Bind _bind(this, true); + glClear(bits); } -void Framebuffer::unbind() +void Framebuffer::bind() const { - if(cur_fbo) + if(set_current(this)) { - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); - cur_fbo = 0; - viewport(sys_viewport[0], sys_viewport[1], sys_viewport[2], sys_viewport[3]); + 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 -{ - if(cur_fbo!=this) - bind(); +const Framebuffer *Framebuffer::current() +{ + if(!cur_obj) + cur_obj = &system(); + return cur_obj; } -Framebuffer::Attachment &Framebuffer::get_or_create_attachment(FramebufferAttachment attch) +void Framebuffer::unbind() { - for(vector::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->attachment==attch) - return *i; - attachments.push_back(Attachment(attch)); - return attachments.back(); + system().bind(); } -void Framebuffer::check_size() +Framebuffer &Framebuffer::system() { - if(!attachments.empty()) - { - const Attachment &attch = attachments.front(); - if(attch.type==GL_RENDERBUFFER_EXT) - { - width = attch.rbuf->get_width(); - height = attch.rbuf->get_height(); - } - 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); - } + static Framebuffer sys_framebuf(0); + return sys_framebuf; } -const Framebuffer *Framebuffer::cur_fbo = 0; -int Framebuffer::sys_viewport[4] = { 0, 0, 1, 1 }; +unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch) +{ + for(unsigned i=0; i