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/msp_stereo_rendering.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,
34 FRAMEBUFFER_INCOMPLETE_FORMATS = GL_FRAMEBUFFER_INCOMPLETE_FORMATS,
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
51 FRONT_LEFT = GL_FRONT_LEFT,
52 FRONT_RIGHT = GL_FRONT_RIGHT,
53 BACK_LEFT = GL_BACK_LEFT,
54 BACK_RIGHT = GL_BACK_RIGHT,
59 FRONT_AND_BACK = GL_FRONT_AND_BACK
62 class framebuffer_incomplete: public std::runtime_error
65 framebuffer_incomplete(FramebufferStatus);
66 virtual ~framebuffer_incomplete() throw() { }
70 Framebuffer objects can be used to perform offscreen rendering. The most
71 common application is rendering to a texture, which can then be used for
72 fullscreen shader effects.
74 A framebuffer consist of a number of logical buffers, such as color and depth
75 buffers. Renderbuffers and Textures can be attached to the logical buffers. At
76 least one image must be attached for the framebuffer to be usable.
78 Requires the GL_EXT_framebuffer_object extension. The blit functions require
79 the GL_EXT_framebuffer_blit extension.
81 class Framebuffer: public Bindable<Framebuffer>
86 FramebufferAttachment attachment;
96 Attachment(FramebufferAttachment);
97 void set(Renderbuffer &);
98 void set(Texture &, unsigned, unsigned);
113 std::vector<Attachment> attachments;
117 mutable unsigned dirty;
119 Framebuffer(unsigned);
124 unsigned get_width() const { return width; }
125 unsigned get_height() const { return height; }
128 void update_attachment(unsigned) const;
130 unsigned get_attachment_index(FramebufferAttachment);
132 void attach(FramebufferAttachment attch, Renderbuffer &rbuf);
133 void attach(FramebufferAttachment attch, Texture2D &tex, unsigned level = 0);
134 void attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
135 void attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
136 void detach(FramebufferAttachment attch);
138 /** Checks the completeness of the framebuffer. Returns
139 FRAMEBUFFER_COMPLETE if the framebuffer is complete and can be rendered to,
140 or one of the error status codes otherwise. */
141 FramebufferStatus check_status() const;
143 /** Ensures that the framebuffer is complete, throwing an exception if it
145 void require_complete() const;
147 void viewport(int, int, unsigned, unsigned);
148 void reset_viewport();
150 void clear(BufferBits);
152 /** Blits a region from another framebuffer into this one. If the source
153 and destination regions have different dimensions, the contents will be
154 stretched. If filter is true, linear interpolation will be used, otherwise
155 no interpolation is done. */
156 void blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1,
157 int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter);
159 /** Blits a region from another framebuffer into this one, retaining its
161 void blit_from(const Framebuffer & other, int sx, int sy,
162 unsigned wd, unsigned ht, int dx, int dy, BufferBits bits);
164 /** Blits the entire contents of another framebuffer into this one. */
165 void blit_from(const Framebuffer &other, BufferBits bits, bool filter);
169 static const Framebuffer *current();
170 static void unbind();
172 static Framebuffer &system();
175 inline BufferBits operator|(BufferBits a, BufferBits b)
176 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }