]> git.tdb.fi Git - libs/gl.git/blob - source/framebuffer.h
Include extension headers in places that need enums from them
[libs/gl.git] / source / framebuffer.h
1 #ifndef MSP_GL_FRAMEBUFFER_H_
2 #define MSP_GL_FRAMEBUFFER_H_
3
4 #include <vector>
5 #include "bindable.h"
6 #include "gl.h"
7 #include "texturecube.h"
8 #include <msp/gl/extensions/ext_framebuffer_object.h>
9
10 namespace Msp {
11 namespace GL {
12
13 class Renderbuffer;
14 class Texture;
15 class Texture2D;
16
17 enum FramebufferAttachment
18 {
19         COLOR_ATTACHMENT0  = GL_COLOR_ATTACHMENT0,
20         COLOR_ATTACHMENT1  = GL_COLOR_ATTACHMENT1,
21         COLOR_ATTACHMENT2  = GL_COLOR_ATTACHMENT2,
22         COLOR_ATTACHMENT3  = GL_COLOR_ATTACHMENT3,
23         DEPTH_ATTACHMENT   = GL_DEPTH_ATTACHMENT,
24         STENCIL_ATTACHMENT = GL_STENCIL_ATTACHMENT
25 };
26
27 enum FramebufferStatus
28 {
29         FRAMEBUFFER_INCOMPLETE_ATTACHMENT         = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
30         FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
31         FRAMEBUFFER_INCOMPLETE_DIMENSIONS         = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT,
32         FRAMEBUFFER_INCOMPLETE_FORMATS            = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT,
33         FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
34         FRAMEBUFFER_INCOMPLETE_READ_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
35         FRAMEBUFFER_UNSUPPORTED                   = GL_FRAMEBUFFER_UNSUPPORTED,
36         FRAMEBUFFER_COMPLETE                      = GL_FRAMEBUFFER_COMPLETE
37 };
38
39 enum BufferBits
40 {
41         COLOR_BUFFER_BIT   = GL_COLOR_BUFFER_BIT,
42         DEPTH_BUFFER_BIT   = GL_DEPTH_BUFFER_BIT,
43         STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT,
44         ACCUM_BUFFER_BIT   = GL_ACCUM_BUFFER_BIT
45 };
46
47 enum RWBuffer
48 {
49         NO_BUFFER      = GL_NONE,
50         FRONT_LEFT     = GL_FRONT_LEFT,
51         FRONT_RIGHT    = GL_FRONT_RIGHT,
52         BACK_LEFT      = GL_BACK_LEFT,
53         BACK_RIGHT     = GL_BACK_RIGHT,
54         FRONT          = GL_FRONT,
55         BACK           = GL_BACK,
56         LEFT           = GL_LEFT,
57         RIGHT          = GL_RIGHT,
58         FRONT_AND_BACK = GL_FRONT_AND_BACK
59 };
60
61 class framebuffer_incomplete: public std::runtime_error
62 {
63 public:
64         framebuffer_incomplete(FramebufferStatus);
65         virtual ~framebuffer_incomplete() throw() { }
66 };
67
68 /**
69 Framebuffer objects can be used to perform offscreen rendering.  The most
70 common application is rendering to a texture, which can then be used for
71 fullscreen shader effects.
72
73 A framebuffer consist of a number of logical buffers, such as color and depth
74 buffers.  Renderbuffers and Textures can be attached to the logical buffers.  At
75 least one image must be attached for the framebuffer to be usable.
76
77 Requires the GL_EXT_framebuffer_object extension.  The blit functions require
78 the GL_EXT_framebuffer_blit extension.
79 */
80 class Framebuffer: public Bindable<Framebuffer>
81 {
82 private:
83         struct Attachment
84         {
85                 FramebufferAttachment attachment;
86                 GLenum type;
87                 union
88                 {
89                         Renderbuffer *rbuf;
90                         Texture *tex;
91                 };
92                 unsigned level;
93                 GLenum cube_face;
94
95                 Attachment(FramebufferAttachment);
96                 void set(Renderbuffer &);
97                 void set(Texture &, GLenum, unsigned);
98                 void clear();
99         };
100
101         unsigned id;
102         std::vector<Attachment> attachments;
103         unsigned width;
104         unsigned height;
105         mutable unsigned dirty;
106
107         Framebuffer(unsigned);
108 public:
109         Framebuffer();
110         ~Framebuffer();
111
112         unsigned get_width() const { return width; }
113         unsigned get_height() const { return height; }
114
115 private:
116         void update_attachment(unsigned) const;
117         void check_size();
118         unsigned get_attachment_index(FramebufferAttachment);
119 public:
120         void attach(FramebufferAttachment attch, Renderbuffer &rbuf);
121         void attach(FramebufferAttachment attch, Texture2D &tex, unsigned level = 0);
122         void attach(FramebufferAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
123         void detach(FramebufferAttachment attch);
124
125         /** Checks the completeness of the framebuffer.  Returns
126         FRAMEBUFFER_COMPLETE if the framebuffer is complete and can be rendered to,
127         or one of the error status codes otherwise. */
128         FramebufferStatus check_status() const;
129
130         /** Ensures that the framebuffer is complete, throwing an exception if it
131         isn't. */
132         void require_complete() const;
133
134         void clear(BufferBits);
135
136         /** Blits a region from another framebuffer into this one.  If the source
137         and destination regions have different dimensions, the contents will be
138         stretched.  If filter is true, linear interpolation will be used, otherwise
139         no interpolation is done. */
140         void blit_from(const Framebuffer &other, int sx0, int sy0, int sx1, int sy1,
141                 int dx0, int dy0, int dx1, int dy1, BufferBits bits, bool filter);
142
143         /** Blits a region from another framebuffer into this one, retaining its
144         dimensions. */
145         void blit_from(const Framebuffer & other, int sx, int sy,
146                 unsigned wd, unsigned ht, int dx, int dy, BufferBits bits);
147
148         /** Blits the entire contents of another framebuffer into this one. */
149         void blit_from(const Framebuffer &other, BufferBits bits, bool filter);
150
151         void bind() const;
152
153         static const Framebuffer *current();
154         static void unbind();
155
156         static Framebuffer &system();
157 };
158
159 inline BufferBits operator|(BufferBits a, BufferBits b)
160 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }
161
162 } // namespace GL
163 } // namespace Msp
164
165 #endif