3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include "ext_framebuffer_object.h"
10 #include "framebuffer.h"
12 #include "renderbuffer.h"
13 #include "texture2d.h"
20 Framebuffer::Framebuffer(unsigned i):
25 throw InvalidParameterValue("System framebuffer must have id 0");
28 glGetIntegerv(GL_VIEWPORT, viewport);
33 Framebuffer::Framebuffer():
38 static RequireExtension _ext("GL_EXT_framebuffer_object");
40 glGenFramebuffersEXT(1, &id);
43 Framebuffer::~Framebuffer()
46 glDeleteFramebuffersEXT(1, &id);
51 void Framebuffer::update_attachment(unsigned mask) const
55 GLenum color_buf = GL_NONE;
56 for(unsigned i=0; i<attachments.size(); ++i)
58 const Attachment &attch = attachments[i];
61 if(attch.type==GL_RENDERBUFFER_EXT)
62 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, GL_RENDERBUFFER_EXT, attch.rbuf->get_id());
63 else if(attch.type==GL_TEXTURE_2D)
65 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
66 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
69 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0);
72 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
73 color_buf = attch.attachment;
76 glDrawBuffer(color_buf);
82 void Framebuffer::check_size()
84 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
87 if(i->type==GL_RENDERBUFFER_EXT)
89 width = i->rbuf->get_width();
90 height = i->rbuf->get_height();
92 else if(i->type==GL_TEXTURE_2D)
94 Texture2D *tex = static_cast<Texture2D *>(i->tex);
95 width = tex->get_width();
96 height = tex->get_height();
99 glViewport(0, 0, width, height);
104 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
107 throw InvalidState("Can't attach to system framebuffer");
109 unsigned i = get_attachment_index(attch);
110 attachments[i].set(rbuf);
111 update_attachment(1<<i);
115 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
118 throw InvalidState("Can't attach to system framebuffer");
120 unsigned i = get_attachment_index(attch);
121 attachments[i].set(tex, level);
122 update_attachment(1<<i);
126 void Framebuffer::detach(FramebufferAttachment attch)
129 throw InvalidState("Can't detach from system framebuffer");
131 unsigned i = get_attachment_index(attch);
132 attachments[i].clear();
133 update_attachment(1<<i);
137 FramebufferStatus Framebuffer::check_status() const
139 Bind _bind(this, true);
140 return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
143 void Framebuffer::clear(BufferBits bits)
145 Bind _bind(this, true);
149 void Framebuffer::bind() const
151 if(set_current(this))
153 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
156 update_attachment(dirty);
160 glViewport(0, 0, width, height);
164 const Framebuffer *Framebuffer::current()
171 void Framebuffer::unbind()
176 Framebuffer &Framebuffer::system()
178 static Framebuffer sys_framebuf(0);
182 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
184 for(unsigned i=0; i<attachments.size(); ++i)
185 if(attachments[i].attachment==attch)
187 attachments.push_back(Attachment(attch));
188 return attachments.size()-1;
192 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
198 void Framebuffer::Attachment::set(Renderbuffer &r)
200 type = GL_RENDERBUFFER_EXT;
205 void Framebuffer::Attachment::set(Texture &t, unsigned l)
207 type = t.get_target();
212 void Framebuffer::Attachment::clear()