2 #include "ext_framebuffer_blit.h"
3 #include "ext_framebuffer_object.h"
4 #include "framebuffer.h"
6 #include "renderbuffer.h"
14 Framebuffer::Framebuffer(unsigned i):
19 throw invalid_argument("System framebuffer must have id 0");
22 glGetIntegerv(GL_VIEWPORT, viewport);
27 Framebuffer::Framebuffer():
32 static Require _req(EXT_framebuffer_object);
34 glGenFramebuffers(1, &id);
37 Framebuffer::~Framebuffer()
40 glDeleteFramebuffers(1, &id);
45 void Framebuffer::update_attachment(unsigned mask) const
49 GLenum color_buf = GL_NONE;
50 for(unsigned i=0; i<attachments.size(); ++i)
52 const Attachment &attch = attachments[i];
55 if(attch.type==GL_RENDERBUFFER)
56 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, GL_RENDERBUFFER, attch.rbuf->get_id());
57 else if(attch.type==GL_TEXTURE_2D)
59 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
60 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
62 else if(attch.type==GL_TEXTURE_CUBE_MAP)
64 static_cast<TextureCube *>(attch.tex)->allocate(attch.level);
65 glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, attch.cube_face, attch.tex->get_id(), attch.level);
68 glFramebufferRenderbuffer(GL_FRAMEBUFFER, attch.attachment, 0, 0);
71 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
72 color_buf = attch.attachment;
75 glDrawBuffer(color_buf);
81 void Framebuffer::check_size()
83 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
86 if(i->type==GL_RENDERBUFFER)
88 width = i->rbuf->get_width();
89 height = i->rbuf->get_height();
91 else if(i->type==GL_TEXTURE_2D)
93 Texture2D *tex = static_cast<Texture2D *>(i->tex);
94 width = tex->get_width();
95 height = tex->get_height();
97 else if(i->type==GL_TEXTURE_CUBE_MAP)
99 width = static_cast<TextureCube *>(i->tex)->get_size();
103 glViewport(0, 0, width, height);
108 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
110 for(unsigned i=0; i<attachments.size(); ++i)
111 if(attachments[i].attachment==attch)
113 attachments.push_back(Attachment(attch));
114 return attachments.size()-1;
117 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
120 throw invalid_operation("Framebuffer::attach");
122 unsigned i = get_attachment_index(attch);
123 attachments[i].set(rbuf);
124 update_attachment(1<<i);
128 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
131 throw invalid_operation("Framebuffer::attach");
133 unsigned i = get_attachment_index(attch);
134 attachments[i].set(tex, 0, level);
135 update_attachment(1<<i);
139 void Framebuffer::attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
142 throw invalid_operation("Framebuffer::attach");
144 unsigned i = get_attachment_index(attch);
145 attachments[i].set(tex, face, level);
146 update_attachment(1<<i);
150 void Framebuffer::detach(FramebufferAttachment attch)
153 throw invalid_operation("Framebuffer::detach");
155 unsigned i = get_attachment_index(attch);
156 attachments[i].clear();
157 update_attachment(1<<i);
161 FramebufferStatus Framebuffer::check_status() const
163 Bind _bind(this, true);
164 return static_cast<FramebufferStatus>(glCheckFramebufferStatus(GL_FRAMEBUFFER));
167 void Framebuffer::clear(BufferBits bits)
169 Bind _bind(this, true);
173 void Framebuffer::blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1, int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter)
175 static Require _req(EXT_framebuffer_blit);
177 const Framebuffer *old = current();
178 if(set_current(this))
180 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, id);
183 update_attachment(dirty);
188 glBindFramebuffer(GL_READ_FRAMEBUFFER, other.id);
190 glBlitFramebuffer(sx0, sy0, sx1, sy1, dx0, dy0, dx1, dy1, bits, (filter ? GL_LINEAR : GL_NEAREST));
193 glBindFramebuffer(GL_FRAMEBUFFER, (old ? old->id : 0));
196 void Framebuffer::blit_from(const Framebuffer &other, int sx, int sy, unsigned wd, unsigned ht, int dx, int dy, BufferBits bits)
198 blit_from(other, sx, sy, sx+wd, sy+ht, dx, dy, dx+wd, dy+ht, bits, false);
201 void Framebuffer::blit_from(const Framebuffer &other, BufferBits bits, bool filter)
203 blit_from(other, 0, 0, other.width, other.height, 0, 0, width, height, bits, filter);
206 void Framebuffer::bind() const
208 if(set_current(this))
210 glBindFramebuffer(GL_FRAMEBUFFER, id);
213 update_attachment(dirty);
217 glViewport(0, 0, width, height);
221 const Framebuffer *Framebuffer::current()
228 void Framebuffer::unbind()
233 Framebuffer &Framebuffer::system()
235 static Framebuffer sys_framebuf(0);
240 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
246 void Framebuffer::Attachment::set(Renderbuffer &r)
248 type = GL_RENDERBUFFER;
253 void Framebuffer::Attachment::set(Texture &t, GLenum f, unsigned l)
255 type = t.get_target();
261 void Framebuffer::Attachment::clear()