1 #ifndef MSP_GL_FRAMEBUFFER_H_
2 #define MSP_GL_FRAMEBUFFER_H_
7 #include "texturecube.h"
8 #include <msp/gl/extensions/ext_framebuffer_object.h>
9 #include <msp/gl/extensions/nv_fbo_color_attachments.h>
19 enum FramebufferAttachment
21 COLOR_ATTACHMENT0 = GL_COLOR_ATTACHMENT0,
22 COLOR_ATTACHMENT1 = GL_COLOR_ATTACHMENT1,
23 COLOR_ATTACHMENT2 = GL_COLOR_ATTACHMENT2,
24 COLOR_ATTACHMENT3 = GL_COLOR_ATTACHMENT3,
25 DEPTH_ATTACHMENT = GL_DEPTH_ATTACHMENT,
26 STENCIL_ATTACHMENT = GL_STENCIL_ATTACHMENT
29 enum FramebufferStatus
31 FRAMEBUFFER_INCOMPLETE_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
32 FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
33 FRAMEBUFFER_INCOMPLETE_DIMENSIONS = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT,
34 FRAMEBUFFER_INCOMPLETE_FORMATS = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT,
35 FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
36 FRAMEBUFFER_INCOMPLETE_READ_BUFFER = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
37 FRAMEBUFFER_UNSUPPORTED = GL_FRAMEBUFFER_UNSUPPORTED,
38 FRAMEBUFFER_COMPLETE = GL_FRAMEBUFFER_COMPLETE
43 COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT,
44 DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT,
45 STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT
48 class framebuffer_incomplete: public std::runtime_error
51 framebuffer_incomplete(FramebufferStatus);
52 virtual ~framebuffer_incomplete() throw() { }
56 Framebuffer objects can be used to perform offscreen rendering. The most
57 common application is rendering to a texture, which can then be used for
58 fullscreen shader effects.
60 A framebuffer consist of a number of logical buffers, such as color and depth
61 buffers. Renderbuffers and Textures can be attached to the logical buffers. At
62 least one image must be attached for the framebuffer to be usable.
64 Requires the GL_EXT_framebuffer_object extension. The blit functions require
65 the GL_EXT_framebuffer_blit extension.
67 class Framebuffer: public Bindable<Framebuffer>
72 FramebufferAttachment attachment;
82 Attachment(FramebufferAttachment);
83 void set(Renderbuffer &);
84 void set(Texture &, unsigned, unsigned);
99 std::vector<Attachment> attachments;
103 mutable unsigned dirty;
105 Framebuffer(unsigned);
110 unsigned get_width() const { return width; }
111 unsigned get_height() const { return height; }
114 void update_attachment(unsigned) const;
116 unsigned get_attachment_index(FramebufferAttachment);
118 void attach(FramebufferAttachment attch, Renderbuffer &rbuf);
119 void attach(FramebufferAttachment attch, Texture2D &tex, unsigned level = 0);
120 void attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
121 void attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
122 void detach(FramebufferAttachment attch);
124 /** Checks the completeness of the framebuffer. Returns
125 FRAMEBUFFER_COMPLETE if the framebuffer is complete and can be rendered to,
126 or one of the error status codes otherwise. */
127 FramebufferStatus check_status() const;
129 /** Ensures that the framebuffer is complete, throwing an exception if it
131 void require_complete() const;
133 void viewport(int, int, unsigned, unsigned);
134 void reset_viewport();
136 void clear(BufferBits);
138 /** Blits a region from another framebuffer into this one. If the source
139 and destination regions have different dimensions, the contents will be
140 stretched. If filter is true, linear interpolation will be used, otherwise
141 no interpolation is done. */
142 void blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1,
143 int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter);
145 /** Blits a region from another framebuffer into this one, retaining its
147 void blit_from(const Framebuffer & other, int sx, int sy,
148 unsigned wd, unsigned ht, int dx, int dy, BufferBits bits);
150 /** Blits the entire contents of another framebuffer into this one. */
151 void blit_from(const Framebuffer &other, BufferBits bits, bool filter);
155 static const Framebuffer *current();
156 static void unbind();
158 static Framebuffer &system();
161 inline BufferBits operator|(BufferBits a, BufferBits b)
162 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }