]> git.tdb.fi Git - libs/gl.git/blob - source/framebuffer.h
Drop Id tags and copyright notices from files
[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
8 namespace Msp {
9 namespace GL {
10
11 class Renderbuffer;
12 class Texture;
13 class Texture2D;
14
15 enum FramebufferAttachment
16 {
17         COLOR_ATTACHMENT0  = GL_COLOR_ATTACHMENT0_EXT,
18         COLOR_ATTACHMENT1  = GL_COLOR_ATTACHMENT1_EXT,
19         COLOR_ATTACHMENT2  = GL_COLOR_ATTACHMENT2_EXT,
20         COLOR_ATTACHMENT3  = GL_COLOR_ATTACHMENT3_EXT,
21         DEPTH_ATTACHMENT   = GL_DEPTH_ATTACHMENT_EXT,
22         STENCIL_ATTACHMENT = GL_STENCIL_ATTACHMENT_EXT
23 };
24
25 enum FramebufferStatus
26 {
27         FRAMEBUFFER_INCOMPLETE_ATTACHMENT         = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT,
28         FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT,
29         FRAMEBUFFER_INCOMPLETE_DIMENSIONS         = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT,
30         FRAMEBUFFER_INCOMPLETE_FORMATS            = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT,
31         FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT,
32         FRAMEBUFFER_INCOMPLETE_READ_BUFFER        = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT,
33         FRAMEBUFFER_UNSUPPORTED                   = GL_FRAMEBUFFER_UNSUPPORTED_EXT,
34         FRAMEBUFFER_COMPLETE                      = GL_FRAMEBUFFER_COMPLETE_EXT
35 };
36
37 enum BufferBits
38 {
39         COLOR_BUFFER_BIT   = GL_COLOR_BUFFER_BIT,
40         DEPTH_BUFFER_BIT   = GL_DEPTH_BUFFER_BIT,
41         STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT,
42         ACCUM_BUFFER_BIT   = GL_ACCUM_BUFFER_BIT
43 };
44
45 enum RWBuffer
46 {
47         NO_BUFFER      = GL_NONE,
48         FRONT_LEFT     = GL_FRONT_LEFT,
49         FRONT_RIGHT    = GL_FRONT_RIGHT,
50         BACK_LEFT      = GL_BACK_LEFT,
51         BACK_RIGHT     = GL_BACK_RIGHT,
52         FRONT          = GL_FRONT,
53         BACK           = GL_BACK,
54         LEFT           = GL_LEFT,
55         RIGHT          = GL_RIGHT,
56         FRONT_AND_BACK = GL_FRONT_AND_BACK
57 };
58
59 /**
60 Framebuffer objects can be used to perform offscreen rendering.  The most
61 common application is rendering to a texture, which can then be used for
62 fullscreen shader effects.
63
64 A framebuffer consist of a number of logical buffers, such as color and depth
65 buffers.  Renderbuffers and Textures can be attached to the logical buffers.  At
66 least one image must be attached for the framebuffer to be usable.
67
68 Requires the GL_EXT_framebuffer_object extension.
69 */
70 class Framebuffer: public Bindable<Framebuffer>
71 {
72 private:
73         struct Attachment
74         {
75                 FramebufferAttachment attachment;
76                 GLenum type;
77                 union
78                 {
79                         Renderbuffer *rbuf;
80                         Texture *tex;
81                 };
82                 unsigned level;
83
84                 Attachment(FramebufferAttachment);
85                 void set(Renderbuffer &);
86                 void set(Texture &, unsigned);
87                 void clear();
88         };
89
90         unsigned id;
91         std::vector<Attachment> attachments;
92         unsigned width;
93         unsigned height;
94         mutable unsigned dirty;
95
96         Framebuffer(unsigned);
97 public:
98         Framebuffer();
99         ~Framebuffer();
100
101         unsigned get_width() const { return width; }
102         unsigned get_height() const { return height; }
103
104 private:
105         void update_attachment(unsigned) const;
106         void check_size();
107 public:
108         void attach(FramebufferAttachment attch, Renderbuffer &rbuf);
109         void attach(FramebufferAttachment attch, Texture2D &tex, unsigned level = 0);
110         void detach(FramebufferAttachment attch);
111
112         /**
113         Checks the completeness status of the framebuffer.  Returns
114         FRAMEBUFFER_COMPLETE if the framebuffer is complate and can be rendered to,
115         or one of the error status codes otherwise.
116         */
117         FramebufferStatus check_status() const;
118
119         void clear(BufferBits);
120         void blit_from(const Framebuffer &, int, int, int, int, int, int, int, int, BufferBits, bool);
121         void blit_from(const Framebuffer &, int, int, unsigned, unsigned, int, int, BufferBits);
122         void blit_from(const Framebuffer &, BufferBits, bool);
123
124         void bind() const;
125
126         static const Framebuffer *current();
127         static void unbind();
128
129         static Framebuffer &system();
130 private:
131         unsigned get_attachment_index(FramebufferAttachment);
132 };
133
134 inline BufferBits operator|(BufferBits a, BufferBits b)
135 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }
136
137 } // namespace GL
138 } // namespace Msp
139
140 #endif