Easy3D 2.6.1
Loading...
Searching...
No Matches
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:
128 static bool is_supported();
129
136 FramebufferObject(int w, int h, int samples = 0);
138 virtual ~FramebufferObject();
139
141
152 GLenum internal_format = GL_RGBA8,
153 GLenum format = GL_RGBA,
154 GLenum type = GL_UNSIGNED_BYTE,
155 GLenum filter = GL_NEAREST
156 );
165 bool add_color_buffer(
166 GLenum internal_format = GL_RGBA8,
167 GLenum format = GL_RGBA,
168 GLenum type = GL_UNSIGNED_BYTE
169 );
181 GLenum internal_format = GL_DEPTH24_STENCIL8,
182 GLenum filter = GL_NEAREST,
183 GLenum compare_mode = GL_NONE,
184 GLenum compare_func = GL_LEQUAL
185 );
192 bool add_depth_buffer(GLenum internal_format = GL_DEPTH24_STENCIL8);
193
201 bool attach_color_texture(GLenum target, GLuint texture_id, GLenum attachment);
209 bool attach_depth_texture(GLenum target, GLuint texture_id, GLenum attachment);
210
212
219 void ensure_size(int w, int h);
220
227 bool bind(GLenum target = GL_FRAMEBUFFER);
233 bool release(GLenum target = GL_FRAMEBUFFER);
234
239 bool is_valid() const;
245 bool is_bound(GLenum target = GL_FRAMEBUFFER) const;
246
252 void activate_draw_buffer(unsigned int index) const;
258 void activate_draw_buffers(unsigned int num_buffers, const unsigned int indices[]) const;
264 void activate_draw_buffers(unsigned int minId, unsigned int maxId) const;
268 void deactivate_draw_buffers() const;
269
275 void activate_read_buffer(unsigned int index) const;
279 void deactivate_read_buffer() const;
280
287 GLuint handle() const { return fbo_id_; }
288
293 int width() const { return width_; }
298 int height() const { return height_; }
299
306 int samples() const { return samples_; }
307
312 int num_color_attachments() const;
318 bool has_color_attachment(unsigned int index) const;
319
324 bool has_depth_attachment() const;
329 int depth_bits() const;
330
335 bool has_stencil() const;
336
341 GLenum texture_target() const;
342
350 GLuint color_texture(unsigned int index = 0, bool resolve = true) const;
356 bool has_color_texture(unsigned int index) const;
357
364 GLuint depth_texture(bool resolve = true) const;
369 bool has_depth_texture() const;
370
402 GLuint& texture_handle,
403 unsigned int index = 0,
404 int internal_format = GL_RGBA8,
405 GLenum format = GL_RGBA,
406 GLenum type = GL_UNSIGNED_BYTE,
407 GLenum filter = GL_NEAREST
408 );
409
420 GLuint& texture_handle,
421 unsigned int internal_format = GL_DEPTH24_STENCIL8,
422 GLenum filter = GL_NEAREST
423 );
424
426 void print_attachments() const;
428 void print_draw_buffers() const;
430 void print_read_buffer() const;
431
440 bool read_color(unsigned int index, unsigned char* buffer, unsigned int format, bool flip_vertically = true) const;
449 bool read_color(unsigned int index, std::vector<unsigned char>& buffer, unsigned int format, bool flip_vertically = true) const;
450
457 bool read_depth(float* buffer, bool flip_vertically = true) const;
464 bool read_depth(std::vector<float>& buffer, bool flip_vertically = true) const;
465
476 bool read_color(unsigned char rgba[4], int x, int y, int index = 0) const;
477
486 bool read_depth(float& depth, int x, int y) const;
487
495 bool snapshot_color(unsigned int index, const std::string& file_name) const;
502 bool snapshot_depth(const std::string& file_name) const;
503
532 static void blit_framebuffer(
533 FramebufferObject* target,
534 const FramebufferObject* source,
535 GLbitfield buffers,
536 GLenum filter = GL_NEAREST
537 );
538
549 static void blit_framebuffer(
550 FramebufferObject* target,
551 const FramebufferObject* source,
552 int target_color_attachment_index, int source_color_attachment_index,
553 GLbitfield buffers,
554 GLenum filter = GL_NEAREST
555 );
556
573 static void blit_framebuffer(
574 FramebufferObject* target, int tx0, int ty0, int tx1, int ty1,
575 const FramebufferObject* source, int sx0, int sy0, int sx1, int sy1,
576 GLbitfield buffers,
577 GLenum filter = GL_NEAREST
578 );
579
598 static void blit_framebuffer(
599 FramebufferObject* target, int tx0, int ty0, int tx1, int ty1,
600 const FramebufferObject* source, int sx0, int sy0, int sx1, int sy1,
601 int target_color_attachment_index, int source_color_attachment_index,
602 GLbitfield buffers,
603 GLenum filter = GL_NEAREST
604 );
605
606 private:
607
608 void init(int w, int h, int samples);
609
610 void clear();
611
612 bool check_status() const;
613
614 // Display a given attachment for the current framebuffer object.
615 void _print_attachment(unsigned int index) const;
616
617 // Display any buffer (convert value into string).
618 void _print_buffer(int value) const;
619
620 void _prepare_resolve_fbo();
621
622 private:
623 GLuint fbo_id_;
624 GLuint prev_draw_fbo_; // for release()
625 GLuint prev_read_fbo_; // for release()
626
627 int width_;
628 int height_;
629
630 int samples_;
631 FramebufferObject* resolved_fbo_; // for multisample FBO,
632
633 GLenum texture_target_;
634
635 GLuint depth_buffer_;
636 GLuint depth_texture_;
637 GLenum depth_internal_format_;
638 GLenum depth_texture_filter_;
639 GLenum depth_texture_compare_mode_;
640 GLenum depth_texture_compare_func_;
641
642 struct ColorAttachment {
643 ColorAttachment() : buffer(0), texture(0), internal_format(GL_RGBA8), format(GL_RGBA), type(GL_UNSIGNED_BYTE), texture_filter(GL_NEAREST) {}
644 GLuint buffer;
645 GLuint texture;
646 GLenum internal_format;
647 GLenum format;
648 GLenum type;
649 GLenum texture_filter;
650 };
651 std::vector<ColorAttachment> color_attachments_;
652
653 bool valid_;
654
655 private:
656 //copying disabled
658 FramebufferObject& operator=(const FramebufferObject&);
659 };
660
661
662} // namespace easy3d
663
664
665#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
Snapshots the color render buffer attached to color attachment at the given index into an image file.
Definition framebuffer_object.cpp:1443
bool copy_color_to_texture(GLuint &texture_handle, unsigned int index=0, int internal_format=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
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
void deactivate_draw_buffers() const
Deactivate all draw buffers.
Definition framebuffer_object.cpp:928
GLuint color_texture(unsigned int index=0, bool resolve=true) const
Returns the texture ID attached to the color attachment at the given index.
Definition framebuffer_object.cpp:1032
void activate_read_buffer(unsigned int index) const
Choose the buffers to read from. This command lets you select which attachment to read from.
Definition framebuffer_object.cpp:948
void ensure_size(int w, int h)
Ensure the size of the buffers is (w, h). If its size matches the required size, i....
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)
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.
Definition framebuffer_object.cpp:1073
bool attach_depth_texture(GLenum target, GLuint texture_id, GLenum attachment)
Attach an existing depth texture to the framebuffer.
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 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
Choose the buffers to render into. This command lets you select which attachments are written to.
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
Returns the texture ID for the texture attached to the depth attachment.
Definition framebuffer_object.cpp:1055
int samples() const
Returns the number of samples per pixel.
Definition framebuffer_object.h:306
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)
Bind FBO to FRAMEBUFFER, DRAW_FRAMEBUFFER, or READ_FRAMEBUFFER.
Definition framebuffer_object.cpp:772
void activate_draw_buffers(unsigned int num_buffers, const unsigned int indices[]) const
Choose the buffers to render into.
Definition framebuffer_object.cpp:870
void print_read_buffer() const
Print the read buffer.
Definition framebuffer_object.cpp:1265
bool has_color_texture(unsigned int index) const
Checks if the framebuffer object has a color texture at the given index.
Definition framebuffer_object.cpp:993
bool has_color_attachment(unsigned int index) const
Checks if the framebuffer object has a color attachment at the given 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)
Attach an existing color texture to the framebuffer.
Definition framebuffer_object.cpp:597
virtual ~FramebufferObject()
Destructor.
Definition framebuffer_object.cpp:103
bool has_depth_attachment() const
Checks if the framebuffer object has 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:298
int width() const
Returns the width of the render buffers.
Definition framebuffer_object.h:293
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
Returns the OpenGL framebuffer object handle for this framebuffer object. This handle can be used to ...
Definition framebuffer_object.h:287
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
Checks if the framebuffer object has 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
Read the color render buffer attached to color attachment at the given index.
Definition framebuffer_object.cpp:1348
bool snapshot_depth(const std::string &file_name) const
Snapshots the depth render buffer into an image file.
Definition framebuffer_object.cpp:1539
bool has_stencil() const
Checks if the framebuffer object has a stencil buffer.
Definition framebuffer_object.cpp:1078
Functions for updating render buffers.
Definition buffer.cpp:45
Definition collider.cpp:182