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