X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fframebuffer.cpp;h=8dbd2d0ee93bd20940b26326c1d2aaeb0c3f25a1;hb=3663bed18358a2399b2a8a8f7779d85e0ed81bd0;hp=cf3761d0abb2d26d639ace3ac1a16c8ea9534cb9;hpb=c473f9193d3e5088772b6e5ebf53b710e2976a92;p=libs%2Fgl.git diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp index cf3761d0..8dbd2d0e 100644 --- a/source/framebuffer.cpp +++ b/source/framebuffer.cpp @@ -17,152 +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 { - const Framebuffer *old = current(); - if(set_current(this)) + if(current()==this) { - if(!old) - get(GL_VIEWPORT, sys_viewport); - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id); - if(width && height) - viewport(0, 0, width, height); + 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() +{ + 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) { - 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)); } -void Framebuffer::unbind() +void Framebuffer::clear(BufferBits bits) +{ + Bind _bind(this, true); + glClear(bits); +} + +void Framebuffer::bind() const { - if(set_current(0)) + if(set_current(this)) { - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 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); } } -Framebuffer::Attachment &Framebuffer::get_or_create_attachment(FramebufferAttachment attch) +const Framebuffer *Framebuffer::current() +{ + if(!cur_obj) + cur_obj = &system(); + return cur_obj; +} + +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(current()==this) - viewport(0, 0, width, height); - } + static Framebuffer sys_framebuf(0); + return sys_framebuf; } -int Framebuffer::sys_viewport[4] = { 0, 0, 1, 1 }; +unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch) +{ + for(unsigned i=0; i