+ if(current()==this)
+ {
+ std::vector<GLenum> color_bufs;
+ color_bufs.reserve(attachments.size());
+ for(unsigned i=0; i<attachments.size(); ++i)
+ {
+ const Attachment &attch = attachments[i];
+ if(mask&(1<<i))
+ {
+ if(attch.type==GL_RENDERBUFFER)
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
+ else if(attch.type==GL_TEXTURE_2D)
+ {
+ static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
+ }
+ else if(attch.type==GL_TEXTURE_CUBE_MAP)
+ {
+ static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
+ glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
+ }
+ else
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
+ }
+
+ if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
+ color_bufs.push_back(attch.attachment);
+ }
+
+ if(color_bufs.empty())
+ glDrawBuffer(GL_NONE);
+ else if(color_bufs.size()==1)
+ glDrawBuffer(color_bufs.front());
+ else
+ {
+ static Require _req(ARB_draw_buffers);
+ glDrawBuffers(color_bufs.size(), &color_bufs[0]);
+ }
+ }
+ else
+ dirty |= mask;
+}
+
+void Framebuffer::check_size()
+{
+ for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
+ if(i->type)
+ {
+ if(i->type==GL_RENDERBUFFER)
+ {
+ width = i->rbuf->get_width();
+ height = i->rbuf->get_height();
+ }
+ else if(i->type==GL_TEXTURE_2D)
+ {
+ Texture2D *tex = static_cast<Texture2D *>(i->tex);
+ width = tex->get_width();
+ height = tex->get_height();
+ }
+ else if(i->type==GL_TEXTURE_CUBE_MAP)
+ {
+ width = static_cast<TextureCube *>(i->tex)->get_size();
+ height = width;
+ }
+ if(current()==this)
+ glViewport(0, 0, width, height);
+ break;
+ }
+}
+
+unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
+{
+ for(unsigned i=0; i<attachments.size(); ++i)
+ if(attachments[i].attachment==attch)
+ return i;
+ attachments.push_back(Attachment(attch));
+ return attachments.size()-1;