Easy3D 2.6.1
Loading...
Searching...
No Matches
FramebufferObject Class Reference

An implementation of framebuffer object (FBO). More...

#include <easy3d/renderer/framebuffer_object.h>

Public Member Functions

 FramebufferObject (int w, int h, int samples=0)
 Constructor.
 
virtual ~FramebufferObject ()
 Destructor.
 
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.
 
bool add_color_buffer (GLenum internal_format=GL_RGBA8, GLenum format=GL_RGBA, GLenum type=GL_UNSIGNED_BYTE)
 Add a color render buffer.
 
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.
 
bool add_depth_buffer (GLenum internal_format=GL_DEPTH24_STENCIL8)
 Add a depth render buffer.
 
bool attach_color_texture (GLenum target, GLuint texture_id, GLenum attachment)
 Attach an existing color texture to the framebuffer.
 
bool attach_depth_texture (GLenum target, GLuint texture_id, GLenum attachment)
 Attach an existing depth texture to the framebuffer.
 
void ensure_size (int w, int h)
 Ensure the size of the buffers is (w, h). If its size matches the required size, i.e, (w, h), it does nothing. Otherwise, it resizes the buffers.
 
bool bind (GLenum target=GL_FRAMEBUFFER)
 Bind FBO to FRAMEBUFFER, DRAW_FRAMEBUFFER, or READ_FRAMEBUFFER.
 
bool release (GLenum target=GL_FRAMEBUFFER)
 Switches rendering back to the default framebuffer.
 
bool is_valid () const
 Returns true if the framebuffer object is valid.
 
bool is_bound (GLenum target=GL_FRAMEBUFFER) const
 Check if the framebuffer object is currently bound to the current context.
 
void activate_draw_buffer (unsigned int index) const
 Choose the buffers to render into. This command lets you select which attachments are written to.
 
void activate_draw_buffers (unsigned int num_buffers, const unsigned int indices[]) const
 Choose the buffers to render into.
 
void activate_draw_buffers (unsigned int minId, unsigned int maxId) const
 Choose the buffers in the range [minId, ... maxId] to render into.
 
void deactivate_draw_buffers () const
 Deactivate all draw buffers.
 
void activate_read_buffer (unsigned int index) const
 Choose the buffers to read from. This command lets you select which attachment to read from.
 
void deactivate_read_buffer () const
 Deactivates reading from the buffers.
 
GLuint handle () const
 Returns the OpenGL framebuffer object handle for this framebuffer object. This handle can be used to attach new images or buffers to the framebuffer. If you attach images or buffers, you are responsible for cleaning up and destroying these objects.
 
int width () const
 Returns the width of the render buffers.
 
int height () const
 Returns the height of the render buffers.
 
int samples () const
 Returns the number of samples per pixel.
 
int num_color_attachments () const
 Returns the number of color attachments.
 
bool has_color_attachment (unsigned int index) const
 Checks if the framebuffer object has a color attachment at the given index.
 
bool has_depth_attachment () const
 Checks if the framebuffer object has a depth attachment.
 
int depth_bits () const
 Returns the depth bits.
 
bool has_stencil () const
 Checks if the framebuffer object has a stencil buffer.
 
GLenum texture_target () const
 Returns the texture target.
 
GLuint color_texture (unsigned int index=0, bool resolve=true) const
 Returns the texture ID attached to the color attachment at the given index.
 
bool has_color_texture (unsigned int index) const
 Checks if the framebuffer object has a color texture at the given index.
 
GLuint depth_texture (bool resolve=true) const
 Returns the texture ID for the texture attached to the depth attachment.
 
bool has_depth_texture () const
 Checks if the framebuffer object has a depth texture.
 
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).
 
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.
 
void print_attachments () const
 Print all the attachments of the current framebuffer object.
 
void print_draw_buffers () const
 Print the draw buffers.
 
void print_read_buffer () const
 Print the read buffer.
 
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.
 
bool read_color (unsigned int index, std::vector< unsigned char > &buffer, unsigned int format, bool flip_vertically=true) const
 Read the color render buffer attached to color attachment at the given index.
 
bool read_depth (float *buffer, bool flip_vertically=true) const
 Read the depth render buffer into a specified buffer.
 
bool read_depth (std::vector< float > &buffer, bool flip_vertically=true) const
 Read the depth render buffer into a specified buffer.
 
bool read_color (unsigned char rgba[4], int x, int y, int index=0) const
 Reads the color at the given pixel coordinates from the color render buffer attached to color attachment at the given index.
 
bool read_depth (float &depth, int x, int y) const
 Reads the depth at the given pixel coordinates.
 
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.
 
bool snapshot_depth (const std::string &file_name) const
 Snapshots the depth render buffer into an image file.
 

Static Public Member Functions

static bool is_supported ()
 Queries if FramebufferObject is supported.
 
static void blit_framebuffer (FramebufferObject *target, const FramebufferObject *source, GLbitfield buffers, GLenum filter=GL_NEAREST)
 Blit the whole sized buffer.
 
static void blit_framebuffer (FramebufferObject *target, const FramebufferObject *source, int target_color_attachment_index, int source_color_attachment_index, GLbitfield buffers, GLenum filter=GL_NEAREST)
 Blit the whole sized buffer of a specific color attachment.
 
static void blit_framebuffer (FramebufferObject *target, int tx0, int ty0, int tx1, int ty1, const FramebufferObject *source, int sx0, int sy0, int sx1, int sy1, GLbitfield buffers, GLenum filter=GL_NEAREST)
 Blit a specified region.
 
static void blit_framebuffer (FramebufferObject *target, int tx0, int ty0, int tx1, int ty1, const FramebufferObject *source, int sx0, int sy0, int sx1, int sy1, int target_color_attachment_index, int source_color_attachment_index, GLbitfield buffers, GLenum filter=GL_NEAREST)
 Blit a specified region of a specific color attachment.
 

Detailed Description

An implementation of framebuffer object (FBO).

A framebuffer object is conceptually a structure containing pointers to GPU memory. The memory pointed to is either an OpenGL texture or an OpenGL RenderBuffer. FBOs can be used to render to one or more textures, share depth buffers between multiple sets of color buffers/textures. http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt for details.

Note that you need to create a FramebufferObject with more than one sample per pixel for primitives to be antialiased. To create a multisample framebuffer object you should set the sample property to a non-zero value. The default sample count of 0 represents a regular non-multisample framebuffer object. If the desired amount of samples per pixel is not supported by the hardware then the maximum number of samples per pixel will be used. The GL_EXT_framebuffer_multisample extension is required to create a framebuffer with more than one sample per pixel.

Note
  • A valid OpenGL context must be present when creating a FramebufferObject, otherwise initialization will fail.
  • GL_TEXTURE_2D textures must have a power of 2 width and height(e.g. 256x512), unless you are using OpenGL 2.0 or higher.
  • To create a multisample framebuffer object you should set the sample property to a non-zero value.
  • If you want to use a multisample framebuffer object as texture, you need to blit it to a regular framebuffer object using blit_framebuffer().
  • It is more efficient(but not required) to call bind() on an FBO before making multiple method calls. For example :
    fbo.bind();
    void print_attachments() const
    Print all the attachments of the current framebuffer object.
    Definition framebuffer_object.cpp:1112
    FramebufferObject(int w, int h, int samples=0)
    Constructor.
    Definition framebuffer_object.cpp:49
    bool bind(GLenum target=GL_FRAMEBUFFER)
    Bind FBO to FRAMEBUFFER, DRAW_FRAMEBUFFER, or READ_FRAMEBUFFER.
    Definition framebuffer_object.cpp:772
    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
    To provide a complete encapsulation, the following usage pattern works correctly but is less efficient :
    // NOTE: No bind() call
    The first usage pattern binds the FBO only once, whereas the second usage binds/unbinds the FBO for each method call.

Example usage 1: draw to a fbo:

FramebufferObject fbo(w, h, 0);
fbo.bind();
glClearDepth(1.0f); // optional, done by default
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // optional, done by default
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // optional, done by default
you draw code here...
fbo.release();
//... Now you can use fbo.color_texture() for other purposes.
bool release(GLenum target=GL_FRAMEBUFFER)
Switches rendering back to the default framebuffer.
Definition framebuffer_object.cpp:813
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
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

Example usage 2: draw the depth map of an object or a scene:

FramebufferObject fbo(w, h, 0);
fbo.bind();
glClearDepth(1.0f); // optional, done by default
glClear(GL_DEPTH_BUFFER_BIT); // optional, done by default
you draw code here...
fbo.release();
//... Now you can use the fbo.depth_texture().
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

For both the above examples, it's easy to enable MSAA by creating a multisample FBO and render to it. But do remember to blit to a normal FBO before using the texture.

Current implement support only GL_TEXTURE_2D (see the texture_target_ variable), and it does not manage externally created textures.

Todo
  • add a class TextureFormat for flexible format specification and add related functions using the TextureFormat class.
  • modify the attach_color_texture() and attach_depth_texture() to use TextureFormat.
  • replace ColorAttachment by TextureFormat.
Examples
Tutorial_404_VirtualScanner/main.cpp, and Tutorial_506_DepthMap/main.cpp.

Constructor & Destructor Documentation

◆ FramebufferObject()

FramebufferObject ( int w,
int h,
int samples = 0 )

Constructor.

Parameters
wThe width of the framebuffer.
hThe height of the framebuffer.
samplesThe number of samples per pixel.

Member Function Documentation

◆ activate_draw_buffer()

void activate_draw_buffer ( unsigned int index) const

Choose the buffers to render into. This command lets you select which attachments are written to.

Parameters
indexThe index of the buffer.
Note
The default buffer is the 0. In such a case calling to this function is optional.

◆ activate_draw_buffers() [1/2]

void activate_draw_buffers ( unsigned int minId,
unsigned int maxId ) const

Choose the buffers in the range [minId, ... maxId] to render into.

Parameters
minIdThe minimum index of the buffer.
maxIdThe maximum index of the buffer.

◆ activate_draw_buffers() [2/2]

void activate_draw_buffers ( unsigned int num_buffers,
const unsigned int indices[] ) const

Choose the buffers to render into.

Parameters
num_buffersThe number of buffers.
indicesThe indices of the buffers.

◆ activate_read_buffer()

void activate_read_buffer ( unsigned int index) const

Choose the buffers to read from. This command lets you select which attachment to read from.

Parameters
indexThe index of the buffer.
Note
The default buffer is the 0. In such a case calling to this function is optional.

◆ add_color_buffer()

bool add_color_buffer ( GLenum internal_format = GL_RGBA8,
GLenum format = GL_RGBA,
GLenum type = GL_UNSIGNED_BYTE )

Add a color render buffer.

Parameters
internal_formatThe internal format of the buffer. It looks like GL_[components][size][type], e.g., GL_RG8, GL_RGBA16, GL_R16F, GL_RG16, GL_RGBA32F.
formatThe format of the pixel data, e.g., GL_RED, GL_RG, GL_RGB, GL_BGR, GL_BGRA.
typeThe data type of the pixel data, e.g., GL_BYTE, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT.
Returns
True if successful, false otherwise.

◆ add_color_texture()

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.

Parameters
internal_formatThe internal format of the texture. It looks like GL_[components][size][type], e.g., GL_RG8, GL_RGBA16, GL_R16F, GL_RG16, GL_RGBA32F.
formatThe format of the pixel data, e.g., GL_RED, GL_RG, GL_RGB, GL_BGR, GL_BGRA.
typeThe data type of the pixel data, e.g., GL_BYTE, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT.
filterThe texture minifying/magnification functions, e.g., GL_NEAREST, GL_LINEAR.
Returns
True if successful, false otherwise.

◆ add_depth_buffer()

bool add_depth_buffer ( GLenum internal_format = GL_DEPTH24_STENCIL8)

Add a depth render buffer.

Parameters
internal_formatThe internal format of the buffer, e.g., GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT32F, GL_DEPTH24_STENCIL8, GL_DEPTH32F_STENCIL8, GL_DEPTH_COMPONENT16
Returns
True if successful, false otherwise.

◆ add_depth_texture()

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.

Parameters
internal_formatThe internal format of the texture, e.g., GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT32F, GL_DEPTH24_STENCIL8, GL_DEPTH32F_STENCIL8, GL_DEPTH_COMPONENT16
filterThe texture minifying/magnification functions, e.g., GL_NEAREST, GL_LINEAR.
compare_modeThe compare mode for the texture, e.g., GL_NONE, GL_COMPARE_REF_TO_TEXTURE.
compare_funcThe compare function for the texture, e.g., GL_GEQUAL, GL_LESS, GL_GREATER, GL_EQUAL, GL_NOTEQUAL, GL_ALWAYS(for 1.0) and GL_NEVER(for 0.0).
Returns
True if successful, false otherwise.

◆ attach_color_texture()

bool attach_color_texture ( GLenum target,
GLuint texture_id,
GLenum attachment )

Attach an existing color texture to the framebuffer.

Parameters
targetThe target texture type. GL_TEXTURE_2D, GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_2D_ARRAY
texture_idThe ID of the texture.
attachmentThe attachment point. GL_COLOR_ATTACHMENTi
Returns
True if successful, false otherwise.

◆ attach_depth_texture()

bool attach_depth_texture ( GLenum target,
GLuint texture_id,
GLenum attachment )

Attach an existing depth texture to the framebuffer.

Parameters
targetThe target texture type. GL_TEXTURE_2D, GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_2D_ARRAY
texture_idThe ID of the texture.
attachmentThe attachment point. GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT or GL_DEPTH_STENCIL_ATTACHMENT.
Returns
True if successful, false otherwise.

◆ bind()

bool bind ( GLenum target = GL_FRAMEBUFFER)

Bind FBO to FRAMEBUFFER, DRAW_FRAMEBUFFER, or READ_FRAMEBUFFER.

Parameters
targetThe target framebuffer.
Returns
True if successful, false otherwise.
Note
bind() and release() should be called in pair.

◆ blit_framebuffer() [1/4]

void blit_framebuffer ( FramebufferObject * target,
const FramebufferObject * source,
GLbitfield buffers,
GLenum filter = GL_NEAREST )
static

Blit the whole sized buffer.

Blits from the source rectangle in the source framebuffer object to the target rectangle in the target framebuffer object. If source or target is 0, the default framebuffer will be used instead of a framebuffer object as source or target respectively. The buffers parameter should be a mask consisting of any combination of GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, and GL_STENCIL_BUFFER_BIT. Any buffer type that is not present both in the source and target buffers is ignored.

The source and target rectangles may have different sizes; in this case buffers should not contain GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT. The filter parameter should be set to GL_LINEAR or GL_NEAREST specifying whether linear or nearest interpolation should be used for scaling. For GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT, the filter parameter must be GL_NEAREST.

If source equals target a copy is performed within the same buffer. Results are undefined if the source and target rectangles overlap and have different sizes. The sizes must also be the same if any of the framebuffer objects are multisample frame buffers.

When multiple render targets are in use, source_color_attachment_index and target_color_attachment_index specify the index of the color attachments in the source and destination frame buffers.

Parameters
targetThe target framebuffer object.
sourceThe source framebuffer object.
buffersThe buffers to blit (e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).
filterThe interpolation method (GL_LINEAR or GL_NEAREST).
Note
The scissor test will restrict the blit area if enabled.

◆ blit_framebuffer() [2/4]

void blit_framebuffer ( FramebufferObject * target,
const FramebufferObject * source,
int target_color_attachment_index,
int source_color_attachment_index,
GLbitfield buffers,
GLenum filter = GL_NEAREST )
static

Blit the whole sized buffer of a specific color attachment.

Parameters
targetThe target framebuffer object.
sourceThe source framebuffer object.
target_color_attachment_indexThe index of the color attachment in the target framebuffer.
source_color_attachment_indexThe index of the color attachment in the source framebuffer.
buffersThe buffers to blit (e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).
filterThe interpolation method (GL_LINEAR or GL_NEAREST).

◆ blit_framebuffer() [3/4]

void blit_framebuffer ( FramebufferObject * target,
int tx0,
int ty0,
int tx1,
int ty1,
const FramebufferObject * source,
int sx0,
int sy0,
int sx1,
int sy1,
GLbitfield buffers,
GLenum filter = GL_NEAREST )
static

Blit a specified region.

Parameters
targetThe target framebuffer object.
tx0The x-coordinate of the lower-left corner of the target rectangle.
ty0The y-coordinate of the lower-left corner of the target rectangle.
tx1The x-coordinate of the upper-right corner of the target rectangle.
ty1The y-coordinate of the upper-right corner of the target rectangle.
sourceThe source framebuffer object.
sx0The x-coordinate of the lower-left corner of the source rectangle.
sy0The y-coordinate of the lower-left corner of the source rectangle.
sx1The x-coordinate of the upper-right corner of the source rectangle.
sy1The y-coordinate of the upper-right corner of the source rectangle.
buffersThe buffers to blit (e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, , or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).
filterThe interpolation method (GL_LINEAR or GL_NEAREST).

◆ blit_framebuffer() [4/4]

void blit_framebuffer ( FramebufferObject * target,
int tx0,
int ty0,
int tx1,
int ty1,
const FramebufferObject * source,
int sx0,
int sy0,
int sx1,
int sy1,
int target_color_attachment_index,
int source_color_attachment_index,
GLbitfield buffers,
GLenum filter = GL_NEAREST )
static

Blit a specified region of a specific color attachment.

Parameters
targetThe target framebuffer object.
tx0The x-coordinate of the lower-left corner of the target rectangle.
ty0The y-coordinate of the lower-left corner of the target rectangle.
tx1The x-coordinate of the upper-right corner of the target rectangle.
ty1The y-coordinate of the upper-right corner of the target rectangle.
sourceThe source framebuffer object.
sx0The x-coordinate of the lower-left corner of the source rectangle.
sy0The y-coordinate of the lower-left corner of the source rectangle.
sx1The x-coordinate of the upper-right corner of the source rectangle.
sy1The y-coordinate of the upper-right corner of the source rectangle.
target_color_attachment_indexThe index of the color attachment in the target framebuffer.
source_color_attachment_indexThe index of the color attachment in the source framebuffer.
buffersThe buffers to blit (e.g., GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT, , or GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT).
filterThe interpolation method (GL_LINEAR or GL_NEAREST).

◆ color_texture()

GLuint color_texture ( unsigned int index = 0,
bool resolve = true ) const

Returns the texture ID attached to the color attachment at the given index.

Parameters
indexThe index of the color attachment.
resolveIf a multisample framebuffer object is used, the function blits and returns the resolved non-multisample texture when resolve is true.
Returns
The texture ID.

◆ copy_color_to_texture()

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).

Internally it creates a texture and uses glCopyTexSubImage2D() to directly copy the buffer in it.

Parameters
texture_handleThe target texture (will be created if it does not exist).
indexThe index of the color attachment.
internal_formatThe internal format of the texture.
formatThe format of the pixel data.
typeThe data type of the pixel data. The internal_format / format / type together define the texture format and hence which and how components of the buffer are copied into the texture. See the glTexImage2D() documentation for details. The typical internal_format/format/type combinations are: For depth: GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT24 / GL_DEPTH_COMPONENT / GL_UNSIGNED_BYTE GL_DEPTH_COMPONENT32F / GL_DEPTH_COMPONENT / GL_FLOAT For color: GL_RGBA8 / GL_RGBA / GL_UNSIGNED_BYTE internal_format must be GL_[components][size][type], e.g., GL_RG8, GL_RGBA16, GL_R16F, GL_RG16, GL_RGBA32F ... format must be one of the GL_RED, GL_RG, GL_RGB, GL_BGR, GL_BGRA ... type can be GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT, GL_FLOAT ... Use GL_LUMINANCE as the internal_format and GL_RED, GL_GREEN or GL_BLUE as format to capture a single color component as a luminance (gray scaled) value.
filterThe texture minifying/magnification functions.
Returns
True if successful, false otherwise.
Note
  • GL_STENCIL is not supported as a format.
  • The GL_DEPTH_COMPONENT format may not be supported by all hardware. It may sometimes be emulated in software, resulting in poor performances.
  • You need to release the texture when you're done.

◆ copy_depth_to_texture()

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.

Parameters
texture_handleThe target texture (will be created if it does not exist).
internal_formatThe internal format of the texture.
filterThe texture minifying/magnification functions.
Returns
True if successful, false otherwise.
Note
  • You need to release the texture when you're done.

◆ depth_bits()

int depth_bits ( ) const

Returns the depth bits.

Returns
The depth bits.

◆ depth_texture()

GLuint depth_texture ( bool resolve = true) const

Returns the texture ID for the texture attached to the depth attachment.

Parameters
resolveIf a multisample framebuffer object is used, the function blits and returns the resolved non-multisample texture if resolve is true.
Returns
The depth texture ID.

◆ ensure_size()

void ensure_size ( int w,
int h )

Ensure the size of the buffers is (w, h). If its size matches the required size, i.e, (w, h), it does nothing. Otherwise, it resizes the buffers.

Parameters
wThe width of the buffers.
hThe height of the buffers.

◆ handle()

GLuint handle ( ) const
inline

Returns the OpenGL framebuffer object handle for this framebuffer object. This handle can be used to attach new images or buffers to the framebuffer. If you attach images or buffers, you are responsible for cleaning up and destroying these objects.

Returns
The OpenGL framebuffer object handle.

◆ has_color_attachment()

bool has_color_attachment ( unsigned int index) const

Checks if the framebuffer object has a color attachment at the given index.

Parameters
indexThe index of the color attachment.
Returns
True if the color attachment exists, false otherwise.

◆ has_color_texture()

bool has_color_texture ( unsigned int index) const

Checks if the framebuffer object has a color texture at the given index.

Parameters
indexThe index of the color texture.
Returns
True if the color texture exists, false otherwise.

◆ has_depth_attachment()

bool has_depth_attachment ( ) const

Checks if the framebuffer object has a depth attachment.

Returns
True if the depth attachment exists, false otherwise.

◆ has_depth_texture()

bool has_depth_texture ( ) const

Checks if the framebuffer object has a depth texture.

Returns
True if the depth texture exists, false otherwise.

◆ has_stencil()

bool has_stencil ( ) const

Checks if the framebuffer object has a stencil buffer.

Returns
True if the stencil buffer exists, false otherwise.

◆ height()

int height ( ) const
inline

Returns the height of the render buffers.

Returns
The height of the render buffers.

◆ is_bound()

bool is_bound ( GLenum target = GL_FRAMEBUFFER) const

Check if the framebuffer object is currently bound to the current context.

Parameters
targetThe target framebuffer.
Returns
True if bound, false otherwise.

◆ is_supported()

bool is_supported ( )
static

Queries if FramebufferObject is supported.

Returns
True if supported, false otherwise.

◆ is_valid()

bool is_valid ( ) const

Returns true if the framebuffer object is valid.

Returns
True if valid, false otherwise.

Returns true if the framebuffer object is valid.

The framebuffer can become invalid if the initialization process fails, the user attaches an invalid buffer to the framebuffer object, or a non-power of two width/height is specified as the texture size if the texture target is {GL_TEXTURE_2D}. The non-power of two limitation does not apply if the OpenGL version is 2.0 or higher, or if the GL_ARB_texture_non_power_of_two extension is present.

The framebuffer can also become invalid if the OpenGL context that the framebuffer was created within is destroyed and there are no other shared contexts that can take over ownership of the framebuffer.

◆ num_color_attachments()

int num_color_attachments ( ) const

Returns the number of color attachments.

Returns
The number of color attachments.

◆ read_color() [1/3]

bool read_color ( unsigned char rgba[4],
int x,
int y,
int index = 0 ) const

Reads the color at the given pixel coordinates from the color render buffer attached to color attachment at the given index.

Parameters
rgbaThe buffer to read into.
xThe x-coordinate of the pixel (in the OpenGL coordinate system).
yThe y-coordinate of the pixel (in the OpenGL coordinate system).
indexThe index of the color attachment.
Returns
True if successful, false otherwise (if the color attachment index does not exist).
Note
(x, y) are in the OpenGL coordinate system.

◆ read_color() [2/3]

bool read_color ( unsigned int index,
std::vector< unsigned char > & buffer,
unsigned int format,
bool flip_vertically = true ) const

Read the color render buffer attached to color attachment at the given index.

Parameters
indexThe index of the color attachment.
bufferThe buffer to read into.
formatThe format of the pixel data. Supported formats: GL_RGB, GL_BGR, GL_RGBA, and GL_BGRA.
flip_verticallyIf true, flips the buffer vertically.
Returns
True if successful, false otherwise.

◆ read_color() [3/3]

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.

Parameters
indexThe index of the color attachment.
bufferThe buffer to read into.
formatThe format of the pixel data. Supported formats: GL_RGB, GL_BGR, GL_RGBA, and GL_BGRA.
flip_verticallyIf true, flips the buffer vertically.
Returns
True if successful, false otherwise.

◆ read_depth() [1/3]

bool read_depth ( float & depth,
int x,
int y ) const

Reads the depth at the given pixel coordinates.

Parameters
depthThe buffer to read into.
xThe x-coordinate of the pixel (in the OpenGL coordinate system).
yThe y-coordinate of the pixel (in the OpenGL coordinate system).
Returns
True if successful, false otherwise (if the depth attachment does not exist).
Note
(x, y) are in the OpenGL coordinate system.

◆ read_depth() [2/3]

bool read_depth ( float * buffer,
bool flip_vertically = true ) const

Read the depth render buffer into a specified buffer.

Parameters
bufferThe buffer to read into.
flip_verticallyIf true, flips the buffer vertically.
Returns
True if successful, false otherwise.

◆ read_depth() [3/3]

bool read_depth ( std::vector< float > & buffer,
bool flip_vertically = true ) const

Read the depth render buffer into a specified buffer.

Parameters
bufferThe buffer to read into.
flip_verticallyIf true, flips the buffer vertically.
Returns
True if successful, false otherwise.

◆ release()

bool release ( GLenum target = GL_FRAMEBUFFER)

Switches rendering back to the default framebuffer.

Parameters
targetThe target framebuffer.
Returns
True if successful, false otherwise.

◆ samples()

int samples ( ) const
inline

Returns the number of samples per pixel.

Returns
The number of samples per pixel.
Note
The returned value can be greater than the requested value since the typically supported values are 0, 4, 8, ..., and the requests are mapped to the next supported value.

◆ snapshot_color()

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.

Parameters
indexThe index of the color attachment.
file_nameThe name of the image file.
Returns
True if successful, false otherwise.
Note
Only png, jpg, bmp, tga, ppm are supported. File format is determined by the given extension.

◆ snapshot_depth()

bool snapshot_depth ( const std::string & file_name) const

Snapshots the depth render buffer into an image file.

Parameters
file_nameThe name of the image file.
Returns
True if successful, false otherwise.
Note
Only png, jpg, bmp, tga, ppm are supported. File format is determined by the given extension.

◆ texture_target()

GLenum texture_target ( ) const

Returns the texture target.

Returns
The texture target, i.e., GL_TEXTURE_2D or GL_TEXTURE_2D_MULTISAMPLE.

◆ width()

int width ( ) const
inline

Returns the width of the render buffers.

Returns
The width of the render buffers.

The documentation for this class was generated from the following files: