GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
framebuffer Class Reference

Class encapsulating an OpenGL framebuffer object. More...

#include <framebuffer.hpp>

Public Member Functions

GLuint name () const
 Retrieve the OpenGL name of this framebuffer. More...
 
void bind ()
 Bind this framebuffer to the GL_FRAMEBUFFER target. More...
 
void attach_texture (GLenum attachment, GLenum textarget, const texture< GL_TEXTURE_CUBE_MAP > &the_texture, GLint level)
 Attach a non-array cubemap to the given attachment. More...
 
template<GLenum Target>
void attach_texture (GLenum attachment, const texture< Target > &the_texture, GLint level)
 Attach a texture to the given attachment. More...
 
template<GLenum Target>
void attach_texture_layer (GLenum attachment, const texture< Target > &the_texture, GLint level, GLint layer)
 Attach a texture layer to the given attachment. More...
 
void detach_texture (GLenum attachment)
 Clear a texture attachment. More...
 
void add_renderbuffer (GLenum attachment, renderbuffer &&the_renderbuffer)
 Attach a renderbuffer to the given attachment. More...
 
void remove_renderbuffer (GLenum attachment)
 Clear a renderbuffer attachment. More...
 
GLenum status ()
 Check the status of the framebuffer. More...
 
bool complete ()
 Check if the framebuffer is complete. More...
 

Static Public Member Functions

static void unbind ()
 Unbind the framebuffer currently bound to the GL_FRAMEBUFFER target. More...
 

Detailed Description

Class encapsulating an OpenGL framebuffer object.

A gls::framebuffer is an object that encapsulates an OpenGL framebuffer object.

Like all objects in GLS, the underlying name is generated at object construction and deleted at destruction. This can be retrieved with name().

WARNING: Unlike other OpenGL objects, framebuffer objects are not shareable between contexts. As such, a gls::framebuffer object is only valid for use within the context where it was constructed. No checks are made to ensure it is used properly. If strange behaviour occurs, be sure to check the reported OpenGL errors in a debug configuration and assure you are not transferring the gls::framebuffer between contexts.

Framebuffer objects are the primary way of rendering to an offscreen surface on hardware that supports it. By default, i.e. when 0 is bound to GL_FRAMEBUFFER all draw commands affect the default framebuffer, typically the back buffer in double buffered applications. To draw to a framebuffer object instead, it has to be bound. All draw commands that are issued while it is bound will affect the framebuffer and its attachments. Once you want to draw to the back buffer once again, simply unbind the framebuffer.

The framebuffer attachments represent surfaces on which draw commands will have an affect. The depth attachment will have depth values written to it while the colour attachments will have colour values written to it and so on. The storage for these attachments comes either from standard texture objects or, since texture objects cannot store non-colour values, renderbuffer objects. The gls::framebuffer object will take ownership of any gls::renderbuffer objects that are attached to it. After all draw commands affecting the framebuffer have completed, one can use the attached texture objects much like one would use any other texture object. Just beware of feedback loops.

After assembling a framebuffer from its attachments and before rendering to it, it is always a good idea to check whether it is complete. This can be done simply by calling complete(). If it is incomplete, calling status() will return the reason for its incompleteness.

To render to a gls::framebuffer, simply call bind(). It will bind the gls::framebuffer to the GL_FRAMEBUFFER binding and replace any previously bound to that target. To clear the GL_FRAMEBUFFER binding and render to the back buffer again, call unbind().

Example usage:

auto texture = gls::texture<GL_TEXTURE_2D>();
texture.image_2d( 0, GL_RGBA, 100, 100, GL_RGBA, GL_FLOAT, nullptr );
auto framebuffer = gls::framebuffer();
framebuffer.attach_texture( GL_COLOR_ATTACHMENT0, texture, 0 );
framebuffer.add_renderbuffer( GL_DEPTH_ATTACHMENT, gls::renderbuffer( GL_DEPTH_COMPONENT24, 100, 100 ) );
if( !framebuffer.complete() ) {
... framebuffer is not complete ...
}
framebuffer.bind();
... draw stuff to the framebuffer ...
framebuffer.unbind();

Member Function Documentation

GLuint name ( ) const

Retrieve the OpenGL name of this framebuffer.

Returns
OpenGL name of this framebuffer
void bind ( )

Bind this framebuffer to the GL_FRAMEBUFFER target.

If you want to bind this framebuffer to a target other than GL_FRAMEBUFFER, you can always do this manually with:

glBindFramebuffer( <your target>, framebuffer.name() );
static void unbind ( )
static

Unbind the framebuffer currently bound to the GL_FRAMEBUFFER target.

If you want to unbind a framebuffer that is bound to a target other than GL_FRAMEBUFFER, you can always do this manually with:

glBindFramebuffer( <your target>, 0 );
void attach_texture ( GLenum  attachment,
GLenum  textarget,
const texture< GL_TEXTURE_CUBE_MAP > &  the_texture,
GLint  level 
)

Attach a non-array cubemap to the given attachment.

Parameters
attachmentAttachment to attach to
textargetCubemap texture target (GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, etc.)
the_textureThe cubemap texture to attach
levelLevel of the cubemap texture to attach
void attach_texture ( GLenum  attachment,
const texture< Target > &  the_texture,
GLint  level 
)

Attach a texture to the given attachment.

Parameters
attachmentAttachment to attach to
the_textureThe texture to attach
levelLevel of the texture to attach
void attach_texture_layer ( GLenum  attachment,
const texture< Target > &  the_texture,
GLint  level,
GLint  layer 
)

Attach a texture layer to the given attachment.

Parameters
attachmentAttachment to attach to
the_textureThe texture to attach
levelLevel of the texture to attach
layerLayer of the texture to attach
void detach_texture ( GLenum  attachment)

Clear a texture attachment.

Parameters
attachmentAttachment to detach a texture from
void add_renderbuffer ( GLenum  attachment,
renderbuffer &&  the_renderbuffer 
)

Attach a renderbuffer to the given attachment.

Note: This method requires that you transfer ownership (i.e. std::move or equivalent) of the renderbuffer to the framebuffer. It will be destroyed either when the attachment is cleared or the gls::framebuffer is destroyed.

Parameters
attachmentAttachment to attach to
the_renderbufferThe renderbuffer to attach
void remove_renderbuffer ( GLenum  attachment)

Clear a renderbuffer attachment.

Parameters
attachmentAttachment to detach a renderbuffer from
GLenum status ( )

Check the status of the framebuffer.

Returns
Status of the framebuffer
bool complete ( )

Check if the framebuffer is complete.

Returns
true if the framebuffer is complete, false otherwise