27#ifndef EASY3D_RENDERER_FRAMEBUFFER_OBJECT_H
28#define EASY3D_RENDERER_FRAMEBUFFER_OBJECT_H
34#include <easy3d/renderer/opengl.h>
136 GLenum internal_format = GL_RGBA8,
137 GLenum format = GL_RGBA,
138 GLenum type = GL_UNSIGNED_BYTE,
139 GLenum filter = GL_NEAREST
143 GLenum internal_format = GL_RGBA8,
144 GLenum format = GL_RGBA,
145 GLenum type = GL_UNSIGNED_BYTE
149 GLenum internal_format = GL_DEPTH24_STENCIL8,
150 GLenum filter = GL_NEAREST,
151 GLenum compare_mode = GL_NONE,
152 GLenum compare_func = GL_LEQUAL
156 GLenum internal_format = GL_DEPTH24_STENCIL8
179 bool bind(GLenum target = GL_FRAMEBUFFER);
181 bool release(GLenum target = GL_FRAMEBUFFER);
186 bool is_bound(GLenum target = GL_FRAMEBUFFER)
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;
193 void deactivate_draw_buffers()
const;
204 GLuint
handle()
const {
return fbo_id_; }
207 int width()
const {
return width_; }
238 GLuint
color_texture(
unsigned int index = 0,
bool resolve =
true)
const;
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
299 GLuint& texture_handle,
300 unsigned int internal_format = GL_DEPTH24_STENCIL8,
301 GLenum filter = GL_NEAREST
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;
321 bool read_depth(
float* buffer,
bool flip_vertically =
true)
const;
323 bool read_depth(std::vector<float>& buffer,
bool flip_vertically =
true)
const;
328 bool read_color(
unsigned char rgba[4],
int x,
int y,
int index = 0)
const;
333 bool read_depth(
float& depth,
int x,
int y)
const;
337 bool snapshot_color(
unsigned int index,
const std::string& file_name)
const;
369 GLenum filter = GL_NEAREST
375 int target_color_attachment_index,
int source_color_attachment_index,
377 GLenum filter = GL_NEAREST
385 GLenum filter = GL_NEAREST
392 int target_color_attachment_index,
int source_color_attachment_index,
394 GLenum filter = GL_NEAREST
399 void init(
int w,
int h,
int samples);
403 bool check_status()
const;
406 void _print_attachment(
unsigned int index)
const;
409 void _print_buffer(
int value)
const;
411 void _prepare_resolve_fbo();
415 GLuint prev_draw_fbo_;
416 GLuint prev_read_fbo_;
424 GLenum texture_target_;
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_;
433 struct ColorAttachment {
434 ColorAttachment() : buffer(0), texture(0), internal_format(GL_RGBA8), format(GL_RGBA), type(GL_UNSIGNED_BYTE), texture_filter(GL_NEAREST) {}
437 GLenum internal_format;
440 GLenum texture_filter;
442 std::vector<ColorAttachment> color_attachments_;
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