Easy3D 2.5.3
framebuffer_object.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_RENDERER_FRAMEBUFFER_OBJECT_H
28#define EASY3D_RENDERER_FRAMEBUFFER_OBJECT_H
29
30
31#include <string>
32#include <vector>
33
34#include <easy3d/renderer/opengl.h>
35
36
37namespace easy3d {
38
122 {
123 public:
125 static bool is_supported();
126
128 FramebufferObject(int w, int h, int samples = 0);
130 virtual ~FramebufferObject();
131
133
136 GLenum internal_format = GL_RGBA8, // GL_[components​][size​][type​], e.g., GL_RG8, GL_RGBA16, GL_R16F, GL_RG16, GL_RGBA32F ...
137 GLenum format = GL_RGBA, // The format of the pixel data (GL_RED, GL_RG, GL_RGB, GL_BGR, GL_BGRA ...)
138 GLenum type = GL_UNSIGNED_BYTE, // The data type of the pixel data (GL_BYTE, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT ...)
139 GLenum filter = GL_NEAREST // The texture minifying/magnification functions. e.g., GL_NEAREST, GL_LINEAR
140 );
142 bool add_color_buffer(
143 GLenum internal_format = GL_RGBA8, // GL_[components​][size​][type​], e.g., GL_RG8, GL_RGBA16, GL_R16F, GL_RG16, GL_RGBA32F ...
144 GLenum format = GL_RGBA, // The format of the pixel data (GL_RED, GL_RG, GL_RGB, GL_BGR, GL_BGRA ...)
145 GLenum type = GL_UNSIGNED_BYTE // The data type of the pixel data (GL_BYTE, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT ...)
146 );
149 GLenum internal_format = GL_DEPTH24_STENCIL8, // GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT32F, GL_DEPTH24_STENCIL8, GL_DEPTH32F_STENCIL8, GL_DEPTH_COMPONENT16
150 GLenum filter = GL_NEAREST, // The texture minifying/magnification functions. e.g., GL_NEAREST, GL_LINEAR
151 GLenum compare_mode = GL_NONE, // GL_COMPARE_REF_TO_TEXTURE.
152 GLenum compare_func = GL_LEQUAL // GL_GEQUAL, GL_LESS, GL_GREATER, GL_EQUAL, GL_NOTEQUAL, GL_ALWAYS(for 1.0) and GL_NEVER(for 0.0).
153 );
155 bool add_depth_buffer(
156 GLenum internal_format = GL_DEPTH24_STENCIL8 // GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT32F, GL_DEPTH24_STENCIL8, GL_DEPTH32F_STENCIL8, GL_DEPTH_COMPONENT16
157 );
158
162 bool attach_color_texture(GLenum target, GLuint texture_id, GLenum attachment);
163
167 bool attach_depth_texture(GLenum target, GLuint texture_id, GLenum attachment);
168
170
174 void ensure_size(int w, int h);
175
179 bool bind(GLenum target = GL_FRAMEBUFFER);
181 bool release(GLenum target = GL_FRAMEBUFFER);
182
184 bool is_valid() const;
186 bool is_bound(GLenum target = GL_FRAMEBUFFER) const;
187
190 void activate_draw_buffer(unsigned int index) const;
191 void activate_draw_buffers(unsigned int num_buffers, const unsigned int indices[]) const;
192 void activate_draw_buffers(unsigned int minId, unsigned int maxId) const; // activate draw buffer in the range [minId, ... maxId]
193 void deactivate_draw_buffers() const;
194
197 void activate_read_buffer(unsigned int index) const;
199 void deactivate_read_buffer() const;
200
204 GLuint handle() const { return fbo_id_; }
205
207 int width() const { return width_; }
209 int height() const { return height_; }
210
213 int samples() const { return samples_; }
214
216 int num_color_attachments() const;
218 bool has_color_attachment(unsigned int index) const;
219
221 bool has_depth_attachment() const;
223 int depth_bits() const;
224
226 bool has_stencil() const;
227
229 GLenum texture_target() const;
230
238 GLuint color_texture(unsigned int index = 0, bool resolve = true) const;
240 bool has_color_texture(unsigned int index) const;
241
248 GLuint depth_texture(bool resolve = true) const;
250 bool has_depth_texture() const;
251
281 GLuint& texture_handle,
282 unsigned int index = 0,
283 int internalFormat = GL_RGBA8,
284 GLenum format = GL_RGBA,
285 GLenum type = GL_UNSIGNED_BYTE,
286 GLenum filter = GL_NEAREST
287 );
288
299 GLuint& texture_handle,
300 unsigned int internal_format = GL_DEPTH24_STENCIL8,
301 GLenum filter = GL_NEAREST
302 );
303
305 void print_attachments() const;
307 void print_draw_buffers() const;
309 void print_read_buffer() const;
310
314 bool read_color(unsigned int index, unsigned char* buffer, unsigned int format, bool flip_vertically = true) const;
318 bool read_color(unsigned int index, std::vector<unsigned char>& buffer, unsigned int format, bool flip_vertically = true) const;
319
321 bool read_depth(float* buffer, bool flip_vertically = true) const;
323 bool read_depth(std::vector<float>& buffer, bool flip_vertically = true) const;
324
328 bool read_color(unsigned char rgba[4], int x, int y, int index = 0) const;
329
333 bool read_depth(float& depth, int x, int y) const;
334
337 bool snapshot_color(unsigned int index, const std::string& file_name) const;
338
341 bool snapshot_depth(const std::string& file_name) const;
342
366 static void blit_framebuffer(
367 FramebufferObject* target, const FramebufferObject* source,
368 GLbitfield buffers, // e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
369 GLenum filter = GL_NEAREST
370 );
371
373 static void blit_framebuffer(
374 FramebufferObject* target, const FramebufferObject* source,
375 int target_color_attachment_index, int source_color_attachment_index,
376 GLbitfield buffers, // e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
377 GLenum filter = GL_NEAREST
378 );
379
381 static void blit_framebuffer(
382 FramebufferObject* target, int tx0, int ty0, int tx1, int ty1,
383 const FramebufferObject* source, int sx0, int sy0, int sx1, int sy1,
384 GLbitfield buffers, // e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
385 GLenum filter = GL_NEAREST
386 );
387
389 static void blit_framebuffer(
390 FramebufferObject* target, int tx0, int ty0, int tx1, int ty1,
391 const FramebufferObject* source, int sx0, int sy0, int sx1, int sy1,
392 int target_color_attachment_index, int source_color_attachment_index,
393 GLbitfield buffers, // e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
394 GLenum filter = GL_NEAREST
395 );
396
397 private:
398
399 void init(int w, int h, int samples);
400
401 void clear();
402
403 bool check_status() const;
404
405 // Display a given attachment for the current framebuffer object.
406 void _print_attachment(unsigned int index) const;
407
408 // Display any buffer (convert value into string).
409 void _print_buffer(int value) const;
410
411 void _prepare_resolve_fbo();
412
413 private:
414 GLuint fbo_id_;
415 GLuint prev_draw_fbo_; // for release()
416 GLuint prev_read_fbo_; // for release()
417
418 int width_;
419 int height_;
420
421 int samples_;
422 FramebufferObject* resolved_fbo_; // for multisample FBO,
423
424 GLenum texture_target_;
425
426 GLuint depth_buffer_;
427 GLuint depth_texture_;
428 GLenum depth_internal_format_;
429 GLenum depth_texture_filter_;
430 GLenum depth_texture_compare_mode_;
431 GLenum depth_texture_compare_func_;
432
433 struct ColorAttachment {
434 ColorAttachment() : buffer(0), texture(0), internal_format(GL_RGBA8), format(GL_RGBA), type(GL_UNSIGNED_BYTE), texture_filter(GL_NEAREST) {}
435 GLuint buffer;
436 GLuint texture;
437 GLenum internal_format;
438 GLenum format;
439 GLenum type;
440 GLenum texture_filter;
441 };
442 std::vector<ColorAttachment> color_attachments_;
443
444 bool valid_;
445
446 private:
447 //copying disabled
449 FramebufferObject& operator=(const FramebufferObject&);
450 };
451
452
453} // namespace easy3d
454
455
456#endif // EASY3D_RENDERER_FRAMEBUFFER_OBJECT_H
An implementation of framebuffer object (FBO).
Definition: framebuffer_object.h:122
bool snapshot_color(unsigned int index, const std::string &file_name) const
Definition: framebuffer_object.cpp:1443
void print_attachments() const
Print all the attachments of the current framebuffer object.
Definition: framebuffer_object.cpp:1112
void deactivate_read_buffer() const
Deactivates reading from the buffers.
Definition: framebuffer_object.cpp:966
GLuint color_texture(unsigned int index=0, bool resolve=true) const
Definition: framebuffer_object.cpp:1032
void activate_read_buffer(unsigned int index) const
Definition: framebuffer_object.cpp:948
void ensure_size(int w, int h)
Definition: framebuffer_object.cpp:144
bool release(GLenum target=GL_FRAMEBUFFER)
Switches rendering back to the default framebuffer.
Definition: framebuffer_object.cpp:813
FramebufferObject(int w, int h, int samples=0)
Default constructor.
Definition: framebuffer_object.cpp:49
bool read_depth(float *buffer, bool flip_vertically=true) const
Read the depth render buffer into a specified buffer.
Definition: framebuffer_object.cpp:1484
bool add_depth_texture(GLenum internal_format=GL_DEPTH24_STENCIL8, GLenum filter=GL_NEAREST, GLenum compare_mode=GL_NONE, GLenum compare_func=GL_LEQUAL)
Add a depth texture render buffer.
Definition: framebuffer_object.cpp:436
GLenum texture_target() const
Returns the texture target, i.e., GL_TEXTURE_2D or GL_TEXTURE_2D_MULTISAMPLE.
Definition: framebuffer_object.cpp:1073
bool attach_depth_texture(GLenum target, GLuint texture_id, GLenum attachment)
Definition: framebuffer_object.cpp:629
int num_color_attachments() const
Returns the number of color attachments.
Definition: framebuffer_object.cpp:983
static bool is_supported()
Queries if FramebufferObject is supported.
Definition: framebuffer_object.cpp:42
bool copy_color_to_texture(GLuint &texture_handle, unsigned int index=0, int internalFormat=GL_RGBA8, GLenum format=GL_RGBA, GLenum type=GL_UNSIGNED_BYTE, GLenum filter=GL_NEAREST)
Makes a copy of the current buffer into a texture (regardless the attachments already have textures).
Definition: framebuffer_object.cpp:1805
bool add_depth_buffer(GLenum internal_format=GL_DEPTH24_STENCIL8)
Add a depth render buffer.
Definition: framebuffer_object.cpp:534
void activate_draw_buffer(unsigned int index) const
Definition: framebuffer_object.cpp:850
void print_draw_buffers() const
Print the draw buffers.
Definition: framebuffer_object.cpp:1235
bool is_valid() const
Returns true if the framebuffer object is valid.
Definition: framebuffer_object.cpp:739
GLuint depth_texture(bool resolve=true) const
Definition: framebuffer_object.cpp:1055
int samples() const
Definition: framebuffer_object.h:213
bool copy_depth_to_texture(GLuint &texture_handle, unsigned int internal_format=GL_DEPTH24_STENCIL8, GLenum filter=GL_NEAREST)
Makes a copy of the current depth buffer into a texture.
Definition: framebuffer_object.cpp:1858
bool bind(GLenum target=GL_FRAMEBUFFER)
Definition: framebuffer_object.cpp:772
void print_read_buffer() const
Print the read buffer.
Definition: framebuffer_object.cpp:1265
bool has_color_texture(unsigned int index) const
Does the fbo have a color texture for color attachment index?
Definition: framebuffer_object.cpp:993
bool has_color_attachment(unsigned int index) const
Does the fbo have color attachment at index?
Definition: framebuffer_object.cpp:988
bool add_color_buffer(GLenum internal_format=GL_RGBA8, GLenum format=GL_RGBA, GLenum type=GL_UNSIGNED_BYTE)
Add a color render buffer.
Definition: framebuffer_object.cpp:356
bool attach_color_texture(GLenum target, GLuint texture_id, GLenum attachment)
Definition: framebuffer_object.cpp:597
virtual ~FramebufferObject()
Destructor.
Definition: framebuffer_object.cpp:103
bool has_depth_attachment() const
Does the fbo have a depth attachment.
Definition: framebuffer_object.cpp:1083
int depth_bits() const
Returns the depth bits.
Definition: framebuffer_object.cpp:1093
int height() const
Returns the height of the render buffers.
Definition: framebuffer_object.h:209
int width() const
Returns the width of the render buffers.
Definition: framebuffer_object.h:207
bool add_color_texture(GLenum internal_format=GL_RGBA8, GLenum format=GL_RGBA, GLenum type=GL_UNSIGNED_BYTE, GLenum filter=GL_NEAREST)
Add a color texture render buffer.
Definition: framebuffer_object.cpp:271
static void blit_framebuffer(FramebufferObject *target, const FramebufferObject *source, GLbitfield buffers, GLenum filter=GL_NEAREST)
Blit the whole sized buffer.
Definition: framebuffer_object.cpp:1631
GLuint handle() const
Definition: framebuffer_object.h:204
bool is_bound(GLenum target=GL_FRAMEBUFFER) const
Check if the framebuffer object is currently bound to the current context.
Definition: framebuffer_object.cpp:744
bool has_depth_texture() const
Does the fbo have a depth texture?
Definition: framebuffer_object.cpp:1088
bool read_color(unsigned int index, unsigned char *buffer, unsigned int format, bool flip_vertically=true) const
Definition: framebuffer_object.cpp:1348
bool snapshot_depth(const std::string &file_name) const
Definition: framebuffer_object.cpp:1539
bool has_stencil() const
Does the fbo have a stencil buffer.
Definition: framebuffer_object.cpp:1078
Definition: collider.cpp:182