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_INCOMPLETE_MULTISAMPLE = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE,
38 FRAMEBUFFER_INCOMPLETE_LAYER_COUNT = GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB,
39 FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS,
40 FRAMEBUFFER_UNSUPPORTED = GL_FRAMEBUFFER_UNSUPPORTED,
41 FRAMEBUFFER_COMPLETE = GL_FRAMEBUFFER_COMPLETE
46 COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT,
47 DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT,
48 STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT
51 class framebuffer_incomplete: public std::runtime_error
54 framebuffer_incomplete(FramebufferStatus);
55 virtual ~framebuffer_incomplete() throw() { }
59 Framebuffer objects can be used to perform offscreen rendering. The most
60 common application is rendering to a texture, which can then be used for
61 fullscreen shader effects.
63 A framebuffer consist of a number of logical buffers, such as color and depth
64 buffers. Renderbuffers and Textures can be attached to the logical buffers. At
65 least one image must be attached for the framebuffer to be usable.
67 Requires the GL_EXT_framebuffer_object extension. The blit functions require
68 the GL_EXT_framebuffer_blit extension.
70 class Framebuffer: public Bindable<Framebuffer>
75 FramebufferAttachment attachment;
85 Attachment(FramebufferAttachment);
86 void set(Renderbuffer &);
87 void set(Texture &, unsigned, unsigned);
102 std::vector<Attachment> attachments;
106 mutable unsigned dirty;
108 Framebuffer(unsigned);
113 unsigned get_width() const { return width; }
114 unsigned get_height() const { return height; }
117 void update_attachment(unsigned) const;
119 unsigned get_attachment_index(FramebufferAttachment);
121 void attach(FramebufferAttachment attch, Renderbuffer &rbuf);
122 void attach(FramebufferAttachment attch, Texture2D &tex, unsigned level = 0);
123 void attach(FramebufferAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
124 void attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
125 void detach(FramebufferAttachment attch);
127 /** Checks the completeness of the framebuffer. Returns
128 FRAMEBUFFER_COMPLETE if the framebuffer is complete and can be rendered to,
129 or one of the error status codes otherwise. */
130 FramebufferStatus check_status() const;
132 /** Ensures that the framebuffer is complete, throwing an exception if it
134 void require_complete() const;
136 void viewport(int, int, unsigned, unsigned);
137 void reset_viewport();
139 void clear(BufferBits);
141 /** Blits a region from another framebuffer into this one. If the source
142 and destination regions have different dimensions, the contents will be
143 stretched. If filter is true, linear interpolation will be used, otherwise
144 no interpolation is done. */
145 void blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1,
146 int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter);
148 /** Blits a region from another framebuffer into this one, retaining its
150 void blit_from(const Framebuffer & other, int sx, int sy,
151 unsigned wd, unsigned ht, int dx, int dy, BufferBits bits);
153 /** Blits the entire contents of another framebuffer into this one. */
154 void blit_from(const Framebuffer &other, BufferBits bits, bool filter);
158 static const Framebuffer *current();
159 static void unbind();
161 static Framebuffer &system();
164 inline BufferBits operator|(BufferBits a, BufferBits b)
165 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }