Skip to content

Vp::Framebuffer

A utility class for managing an OpenGL FrameBufferObject. More...

#include "Viewport/framebuffer/Framebuffer.h"

Inherits from std::enable_shared_from_this< Framebuffer >

Public Functions

Name
Framebuffer *createDefault(bool multisample, unsigned int samples, bool alpha)<br>Create a basic framebuffer with a single color attachment and a depth-stencil buffer.
Framebuffer *createDefault(int width, int height, bool multisample, unsigned int samples, bool alpha)<br>Create a basic framebuffer with a single color attachment and a depth-stencil buffer.
Framebuffer()<br>Create an empty framebuffer.
Framebuffer(bool multisample, unsigned int samples)<br>Create an empty framebuffer.
Framebuffer(int width, int height)<br>Create an empty framebuffer.
Framebuffer(int width, int height, bool multisample, unsigned int samples)<br>Create an empty framebuffer.
~Framebuffer()
voidaddColorAttachment(ColorAttachment colorAttachment)<br>Add a color attachment.
voidbind() const<br>Binds the FBO.
voiddispose()<br>Frees up all OpenGL resources used by the framebuffer.
voidend(bool resolveMultisample =true)<br>Finalizes the FBO and unbinds it.
ColorAttachment &getColorAttachment(unsigned int index)
GLuintgetColorTexture(unsigned int index =0, bool multisampled =false) const<br>Returns the id of the color texture.
std::optional< DepthAttachment > &getDepthAttachment()
intgetHeight() const
GLuintgetId() const
std::weak_ptr< Framebuffer >getResolvedFramebuffer()<br>If this framebuffer is multisampled this methods returns a weak pointer to the internal single sampled framebuffer that was used to resolve it.
unsigned intgetSampleCount() const
intgetWidth() const
boolisInitialized() const
boolisMultisampled()
voidmultisampleResolveColor(unsigned int colorAttachmentIndex)<br>The DEFAULT (0) frame buffer is bound after this call!
voidmultisampleResolveColors()<br>The DEFAULT (0) frame buffer is bound after this call!
voidmultisampleResolveDepth()<br>The DEFAULT (0) frame buffer is bound after this call!
voidrecreate()<br>Completely reinitializes the framebuffer.
voidremoveAllColorAttachments()
voidremoveDepthAttachment()
voidsetDepthAttachment(DepthAttachment depthAttachment)<br>Set a depth attachment.
voidsetDrawBuffers()
voidsetDrawBuffers(std::vector< unsigned int > indices)
voidsetMultisampled(bool multisample, unsigned int samples)
voidstart()<br>Binds the FBO, clears it and resizes glViewport.
voidstart(int width, int height)<br>Binds the FBO and resizes glViewport.
voidupdate(int width, int height)<br>Ensure the fbo has the specified size.

Detailed Description

cpp
class Vp::Framebuffer;
class Vp::Framebuffer;

A utility class for managing an OpenGL FrameBufferObject.

Usage:

  • //Create fbo object
  • fbo.start(); //The fbo is lazily initialized here if necessary
  • //Render stuff
  • fbo.end(); //The fbo is unbound by BINDING THE DEFAULT (0) FRAMEBUFFER!
  • //Multisampling is resolved here unless fbo.end(false) is called
  • //Get color texture using fbo.getColorTexture()

Supports multi sampled antialiasing (MSAA) which can be enabled in the constructor. When MSAA is enabled, the framebuffer creates a second intermediate framebuffer instance which is used to "resolve" the multisampled framebuffer into a regular one.

Multisampled framebuffers with multiple color attachments are also supported. In that case the intermediate framebuffer creates equivalent single-sampled color attachment(s) for each multisampled one.

Note that the framebuffer sampling settings overwrite any attachment settings as all attachments must have the same sample count and either be all multisampled or none.

After resolving multisampling the resolved framebuffer can be accessed using getResolvedFramebuffer() and further used.

Public Functions Documentation

function createDefault

cpp
static Framebuffer * createDefault(
    bool multisample,
    unsigned int samples,
    bool alpha
)
static Framebuffer * createDefault(
    bool multisample,
    unsigned int samples,
    bool alpha
)

Create a basic framebuffer with a single color attachment and a depth-stencil buffer.

Parameters:

  • multisample Whether MSAA should be enabled
  • samples Amount of MSAA samples
  • alpha Whether the color buffer should have an alpha component

Return: Raw unmanaged pointer to the newly created Framebuffer.

function createDefault

cpp
static Framebuffer * createDefault(
    int width,
    int height,
    bool multisample,
    unsigned int samples,
    bool alpha
)
static Framebuffer * createDefault(
    int width,
    int height,
    bool multisample,
    unsigned int samples,
    bool alpha
)

Create a basic framebuffer with a single color attachment and a depth-stencil buffer.

Parameters:

  • width Width of the framebuffer in pixels
  • height Height of the framebuffer in pixels
  • multisample Whether MSAA should be enabled
  • samples Amount of MSAA samples
  • alpha Whether the color buffer should have an alpha component

Return: Raw unmanaged pointer to the newly created Framebuffer.

function Framebuffer

cpp
Framebuffer()
Framebuffer()

Create an empty framebuffer.

function Framebuffer

cpp
Framebuffer(
    bool multisample,
    unsigned int samples
)
Framebuffer(
    bool multisample,
    unsigned int samples
)

Create an empty framebuffer.

Parameters:

  • multisample Whether MSAA should be enabled
  • samples Amount of MSAA samples

function Framebuffer

cpp
Framebuffer(
    int width,
    int height
)
Framebuffer(
    int width,
    int height
)

Create an empty framebuffer.

Parameters:

  • width Width of the framebuffer in pixels
  • height Height of the framebuffer in pixels

function Framebuffer

cpp
Framebuffer(
    int width,
    int height,
    bool multisample,
    unsigned int samples
)
Framebuffer(
    int width,
    int height,
    bool multisample,
    unsigned int samples
)

Create an empty framebuffer.

Parameters:

  • width Width of the framebuffer in pixels
  • height Height of the framebuffer in pixels
  • multisample Whether MSAA should be enabled
  • samples Amount of MSAA samples

function ~Framebuffer

cpp
~Framebuffer()
~Framebuffer()

function addColorAttachment

cpp
void addColorAttachment(
    ColorAttachment colorAttachment
)
void addColorAttachment(
    ColorAttachment colorAttachment
)

Add a color attachment.

Parameters:

  • colorAttachment

Color attachment index is set to its current order in the color attachments list. Multisampling settings are synced with the framebuffer multisampling settings.

Note that color attachments are meant to be setup before using the framebuffer.

function bind

cpp
void bind() const
void bind() const

Binds the FBO.

function dispose

cpp
void dispose()
void dispose()

Frees up all OpenGL resources used by the framebuffer.

The framebuffer is considered uninitialized after this call.

function end

cpp
void end(
    bool resolveMultisample =true
)
void end(
    bool resolveMultisample =true
)

Finalizes the FBO and unbinds it.

Parameters:

  • resolveMultisample If the fbo is multisampled and this argument is true this call resolves the multisampled buffer into an intermediate regular (single sampled) one. Whose color texture is then returned via getColorTexture(). Note that only the first (index 0) color attachment gets resolved by this call. To resolve multiple color buffers or the depth buffer you can use the multisampleResolveXXX() methods.

The DEFAULT (0) frame buffer is bound after this call!

function getColorAttachment

cpp
ColorAttachment & getColorAttachment(
    unsigned int index
)
ColorAttachment & getColorAttachment(
    unsigned int index
)

function getColorTexture

cpp
GLuint getColorTexture(
    unsigned int index =0,
    bool multisampled =false
) const
GLuint getColorTexture(
    unsigned int index =0,
    bool multisampled =false
) const

Returns the id of the color texture.

Parameters:

  • index Optional index of the color attachment
  • multisampled Optional flag specifying if the multisampled or resolved single sampled texture should be returned. If multisampling is disabled the single sampled texture will be always returned.

function getDepthAttachment

cpp
std::optional< DepthAttachment > & getDepthAttachment()
std::optional< DepthAttachment > & getDepthAttachment()

function getHeight

cpp
int getHeight() const
int getHeight() const

function getId

cpp
GLuint getId() const
GLuint getId() const

Return: The id of the Frame Buffer Object

function getResolvedFramebuffer

cpp
std::weak_ptr< Framebuffer > getResolvedFramebuffer()
std::weak_ptr< Framebuffer > getResolvedFramebuffer()

If this framebuffer is multisampled this methods returns a weak pointer to the internal single sampled framebuffer that was used to resolve it.

Otherwise it just returns a weak pointer to itself.

function getSampleCount

cpp
unsigned int getSampleCount() const
unsigned int getSampleCount() const

function getWidth

cpp
int getWidth() const
int getWidth() const

function isInitialized

cpp
bool isInitialized() const
bool isInitialized() const

function isMultisampled

cpp
bool isMultisampled()
bool isMultisampled()

function multisampleResolveColor

cpp
void multisampleResolveColor(
    unsigned int colorAttachmentIndex
)
void multisampleResolveColor(
    unsigned int colorAttachmentIndex
)

The DEFAULT (0) frame buffer is bound after this call!

function multisampleResolveColors

cpp
void multisampleResolveColors()
void multisampleResolveColors()

The DEFAULT (0) frame buffer is bound after this call!

function multisampleResolveDepth

cpp
void multisampleResolveDepth()
void multisampleResolveDepth()

The DEFAULT (0) frame buffer is bound after this call!

function recreate

cpp
void recreate()
void recreate()

Completely reinitializes the framebuffer.

All associated OpenGL resources are freed and can no longer be used. New resources are allocated.

function removeAllColorAttachments

cpp
void removeAllColorAttachments()
void removeAllColorAttachments()

function removeDepthAttachment

cpp
void removeDepthAttachment()
void removeDepthAttachment()

function setDepthAttachment

cpp
void setDepthAttachment(
    DepthAttachment depthAttachment
)
void setDepthAttachment(
    DepthAttachment depthAttachment
)

Set a depth attachment.

Parameters:

  • depthAttachment

Multisampling settings are synced with the framebuffer multisampling settings.

function setDrawBuffers

cpp
void setDrawBuffers()
void setDrawBuffers()

function setDrawBuffers

cpp
void setDrawBuffers(
    std::vector< unsigned int > indices
)
void setDrawBuffers(
    std::vector< unsigned int > indices
)

function setMultisampled

cpp
void setMultisampled(
    bool multisample,
    unsigned int samples
)
void setMultisampled(
    bool multisample,
    unsigned int samples
)

function start

cpp
void start()
void start()

Binds the FBO, clears it and resizes glViewport.

The FBO is also initialised if necessary. Uses previously set width and height. **Don't forget to call glClear after this call!**glViewport is set to the fbo's dimensions.

function start

cpp
void start(
    int width,
    int height
)
void start(
    int width,
    int height
)

Binds the FBO and resizes glViewport.

The FBO is also initialised if necessary. **Don't forget to call glClear after this call!**glViewport is set to the fbo's dimensions.

function update

cpp
void update(
    int width,
    int height
)
void update(
    int width,
    int height
)

Ensure the fbo has the specified size.

Fbo is initialized and/or resized accordingly. Has no effect if fbo is already initialized and sized as requested.


Updated on 2024-11-06 at 20:16:53 +0000