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 bool has_depth = false;
57 for(unsigned i=0; i<attachments.size(); ++i)
59 const Attachment &attch = attachments[i];
62 if(attch.type==GL_RENDERBUFFER_EXT)
63 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, GL_RENDERBUFFER_EXT, attch.rbuf->get_id());
64 else if(attch.type==GL_TEXTURE_2D)
66 static_cast<Texture2D *>(attch.tex)->allocate(attch.level);
67 glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attch.attachment, attch.type, attch.tex->get_id(), attch.level);
70 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, attch.attachment, 0, 0);
73 if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3)
74 color_buf = attch.attachment;
75 if(attch.attachment==DEPTH_ATTACHMENT)
79 glDrawBuffer(color_buf);
80 glDepthMask(has_depth);
86 void Framebuffer::check_size()
88 for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
91 if(i->type==GL_RENDERBUFFER_EXT)
93 width = i->rbuf->get_width();
94 height = i->rbuf->get_height();
96 else if(i->type==GL_TEXTURE_2D)
98 Texture2D *tex = static_cast<Texture2D *>(i->tex);
99 width = tex->get_width();
100 height = tex->get_height();
103 glViewport(0, 0, width, height);
108 void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf)
111 throw InvalidState("Can't attach to system framebuffer");
113 unsigned i = get_attachment_index(attch);
114 attachments[i].set(rbuf);
115 update_attachment(1<<i);
119 void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level)
122 throw InvalidState("Can't attach to system framebuffer");
124 unsigned i = get_attachment_index(attch);
125 attachments[i].set(tex, level);
126 update_attachment(1<<i);
130 void Framebuffer::detach(FramebufferAttachment attch)
133 throw InvalidState("Can't detach from system framebuffer");
135 unsigned i = get_attachment_index(attch);
136 attachments[i].clear();
137 update_attachment(1<<i);
141 FramebufferStatus Framebuffer::check_status() const
143 Bind _bind(this, true);
144 return static_cast<FramebufferStatus>(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
147 void Framebuffer::clear(BufferBits bits)
149 Bind _bind(this, true);
153 void Framebuffer::bind() const
155 if(set_current(this))
157 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
160 update_attachment(dirty);
164 glViewport(0, 0, width, height);
168 const Framebuffer *Framebuffer::current()
175 void Framebuffer::unbind()
180 Framebuffer &Framebuffer::system()
182 static Framebuffer sys_framebuf(0);
186 unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch)
188 for(unsigned i=0; i<attachments.size(); ++i)
189 if(attachments[i].attachment==attch)
191 attachments.push_back(Attachment(attch));
192 return attachments.size()-1;
196 Framebuffer::Attachment::Attachment(FramebufferAttachment a):
202 void Framebuffer::Attachment::set(Renderbuffer &r)
204 type = GL_RENDERBUFFER_EXT;
209 void Framebuffer::Attachment::set(Texture &t, unsigned l)
211 type = t.get_target();
216 void Framebuffer::Attachment::clear()