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