X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fframebuffer.cpp;h=cf478ce29b686d842ce2074078952633b2e47b02;hp=a0fcbc474203c5588d04e410bf4d0975c49fa983;hb=HEAD;hpb=656b4577fccfb02bea747871e5ab10148f002443 diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp deleted file mode 100644 index a0fcbc47..00000000 --- a/source/framebuffer.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -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(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) -{ - static RequireExtension _ext("GL_EXT_framebuffer_object"); - - glGenFramebuffersEXT(1, &id); -} - -Framebuffer::~Framebuffer() -{ - if(id) - glDeleteFramebuffersEXT(1, &id); - if(current()==this) - unbind(); -} - -void Framebuffer::update_attachment(unsigned mask) const -{ - 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) - { - 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; - 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) -{ - 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::clear(BufferBits bits) -{ - Bind _bind(this, true); - glClear(bits); -} - -void Framebuffer::bind() const -{ - if(set_current(this)) - { - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id); - if(dirty) - { - update_attachment(dirty); - dirty = 0; - } - if(width && height) - glViewport(0, 0, width, height); - } -} - -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) -{ - for(unsigned i=0; i