sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
framebuffer.cpp
1#include "framebuffer.h"
2
3using namespace sgltk;
4
5int Framebuffer::max_color_attachments = 0;
6
8 this->target = target;
9 width = 0;
10 height = 0;
11 glGenFramebuffers(1, &buffer);
12 if(max_color_attachments == 0)
13 glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS,
14 &Framebuffer::max_color_attachments);
15}
16
17Framebuffer::~Framebuffer() {
18 glDeleteFramebuffers(1, &buffer);
19}
20
22 bind();
23 GLenum ret = glCheckFramebufferStatus(target);
24 unbind();
25 return ret;
26}
27
29 glBindFramebuffer(target, buffer);
30}
31
32void Framebuffer::bind(GLenum target) {
33 glBindFramebuffer(target, buffer);
34 if(draw_buffers.size() == 0) {
35 glDrawBuffer(GL_NONE);
36 } else {
37 glDrawBuffers(draw_buffers.size(), draw_buffers.data());
38 }
39}
40
42 glBindFramebuffer(target, 0);
43}
44
45bool Framebuffer::attach_texture(GLenum attachment,
46 Texture& texture) {
47
48 if(width == 0 && height == 0) {
49 width = texture.width;
50 height = texture.height;
51 } else if(width != texture.width || height != texture.height) {
52 App::error_string.push_back("The size of the texture does not "
53 "match the size of previously attached textures or "
54 "renderbuffers.");
55 return false;
56 }
57 bind();
58 glFramebufferTexture(target, attachment, texture.texture, 0);
59 unbind();
60 GLenum attachment_max = GL_COLOR_ATTACHMENT0 + max_color_attachments;
61 switch(attachment) {
62 case GL_NONE:
63 case GL_FRONT_LEFT:
64 case GL_FRONT_RIGHT:
65 case GL_BACK_LEFT:
66 case GL_BACK_RIGHT:
67 draw_buffers.push_back(attachment);
68 break;
69 default:
70 if(attachment >= GL_COLOR_ATTACHMENT0 &&
71 attachment < attachment_max) {
72
73 draw_buffers.push_back(attachment);
74 }
75 break;
76 }
77 return true;
78}
79
80bool Framebuffer::attach_texture_layer(GLenum attachment,
81 Texture& texture,
82 unsigned int layer) {
83
84 if(width == 0 && height == 0) {
85 width = texture.width;
86 height = texture.height;
87 } else if(width != texture.width || height != texture.height) {
88 App::error_string.push_back("The size of the texture does not "
89 "match the size of previously attached textures or "
90 "renderbuffers.");
91 return false;
92 }
93 if(layer >= texture.num_layers) {
94 App::error_string.push_back("Layer argument is exceeding the"
95 " number of layers of the texture");
96 return false;
97 }
98 bind();
99 glFramebufferTextureLayer(target, attachment, texture.texture, 0, layer);
100 unbind();
101 GLenum attachment_max = GL_COLOR_ATTACHMENT0 + max_color_attachments;
102 switch(attachment) {
103 case GL_NONE:
104 case GL_FRONT_LEFT:
105 case GL_FRONT_RIGHT:
106 case GL_BACK_LEFT:
107 case GL_BACK_RIGHT:
108 draw_buffers.push_back(attachment);
109 break;
110 default:
111 if(attachment >= GL_COLOR_ATTACHMENT0 &&
112 attachment < attachment_max) {
113
114 draw_buffers.push_back(attachment);
115 }
116 break;
117 }
118 return true;
119}
120
121bool Framebuffer::attach_renderbuffer(GLenum attachment,
122 Renderbuffer& buffer) {
123
124 if(width == 0 && height == 0) {
125 width = buffer.width;
126 height = buffer.height;
127 } else if(width != buffer.width || height != buffer.height) {
128 App::error_string.push_back("The size of the renderbuffer does "
129 "not match the size of previously attached textures or "
130 "renderbuffers.");
131 return false;
132 }
133 bind();
134 glFramebufferRenderbuffer(target,
135 attachment,
136 GL_RENDERBUFFER,
137 buffer.buffer);
138 unbind();
139 GLenum attachment_max = GL_COLOR_ATTACHMENT0 + max_color_attachments;
140 switch(attachment) {
141 case GL_NONE:
142 case GL_FRONT_LEFT:
143 case GL_FRONT_RIGHT:
144 case GL_BACK_LEFT:
145 case GL_BACK_RIGHT:
146 draw_buffers.push_back(attachment);
147 break;
148 default:
149 if(attachment >= GL_COLOR_ATTACHMENT0 &&
150 attachment < attachment_max) {
151
152 draw_buffers.push_back(attachment);
153 }
154 break;
155 }
156 return true;
157}
158
160 bind();
161 if(draw_buffers.size() == 0) {
162 glDrawBuffer(GL_NONE);
163 } else {
164 glDrawBuffers(draw_buffers.size(), draw_buffers.data());
165 }
166 unbind();
167}
168
170 unsigned int src_x0,
171 unsigned int src_y0,
172 unsigned int src_x1,
173 unsigned int src_y1,
174 unsigned int dst_x0,
175 unsigned int dst_y0,
176 unsigned int dst_x1,
177 unsigned int dst_y1,
178 GLbitfield mask,
179 GLenum filter) {
180
181 if(!target) {
182 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
183 } else {
184 target->bind(GL_DRAW_FRAMEBUFFER);
185 }
186 bind(GL_READ_FRAMEBUFFER);
187 glBlitFramebuffer(src_x0, src_y0, src_x1, src_y1, dst_x0, dst_y0, dst_x1, dst_y1, mask, filter);
188 unbind();
189 if(target) {
190 target->unbind();
191 }
192}
193
194void Framebuffer::blit_from_default(unsigned int src_x0,
195 unsigned int src_y0,
196 unsigned int src_x1,
197 unsigned int src_y1,
198 unsigned int dst_x0,
199 unsigned int dst_y0,
200 unsigned int dst_x1,
201 unsigned int dst_y1,
202 GLbitfield mask,
203 GLenum filter) {
204
205 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
206 bind(GL_DRAW_FRAMEBUFFER);
207 glBlitFramebuffer(src_x0, src_y0, src_x1, src_y1, dst_x0, dst_y0, dst_x1, dst_y1, mask, filter);
208 unbind();
209}
static std::vector< std::string > error_string
A list of all error strings.
Definition app.h:151
Manages framebuffers.
Definition framebuffer.h:14
void finalize()
Sets the draw buffers to be drawn into.
bool attach_texture_layer(GLenum attachment, Texture &texture, unsigned int layer)
Attaches the layer of a texture to the framebuffer.
void blit_from_default(unsigned int src_x0, unsigned int src_y0, unsigned int src_x1, unsigned int src_y1, unsigned int dst_x0, unsigned int dst_y0, unsigned int dst_x1, unsigned int dst_y1, GLbitfield mask, GLenum filter)
Copies a block of pixels from the default framebuffer to this one.
void bind()
Binds the framebuffer to the target set in the constructor.
void blit_to(sgltk::Framebuffer *target, unsigned int src_x0, unsigned int src_y0, unsigned int src_x1, unsigned int src_y1, unsigned int dst_x0, unsigned int dst_y0, unsigned int dst_x1, unsigned int dst_y1, GLbitfield mask, GLenum filter)
Copies a block of pixels from this framebuffer to another one.
bool attach_renderbuffer(GLenum attachment, Renderbuffer &buffer)
Attaches a renderbuffer to the framebuffer.
Framebuffer(GLenum target=GL_FRAMEBUFFER)
bool attach_texture(GLenum attachment, Texture &texture)
Attaches a texture to the framebuffer.
GLenum get_buffer_status()
Checks the completeness status of the framebuffer.
void unbind()
Restores the original framebuffer as render target.
Manages renderbuffers.
Manages textures.
Definition texture.h:13
unsigned int num_layers
The number of layers.
Definition texture.h:46
unsigned int width
The width of the texture.
Definition texture.h:38
GLuint texture
The texture name.
Definition texture.h:34
unsigned int height
The height of the texture.
Definition texture.h:42