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