JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
FrameBuffer Class Reference

Image framebuffer object which can be rendered on. More...

#include <jngl/FrameBuffer.hpp>

Classes

struct  Context
 Lifetime object when the FrameBuffer is in use. More...
 

Public Member Functions

 FrameBuffer (Pixels width, Pixels height, bool hdr=false, bool antiAliased=false)
 Creates a framebuffer object with width times height actual pixels.
 
 FrameBuffer (ScaleablePixels width, ScaleablePixels height, bool hdr=false, bool antiAliased=false)
 Creates a framebuffer object with width times height scalable pixels.
 
 FrameBuffer (std::array< Pixels, 2 > size, bool hdr=false, bool antiAliased=false)
 Creates a framebuffer object with size[0] times size[1] pixels.
 
 FrameBuffer (const FrameBuffer &)=delete
 
FrameBuffer & operator= (const FrameBuffer &)=delete
 
 FrameBuffer (FrameBuffer &&) noexcept
 
FrameBuffer & operator= (FrameBuffer &&) noexcept
 
Context use () const
 Starts drawing on the FrameBuffer as long as Context is alive.
 
void draw (Vec2 position, const ShaderProgram *=nullptr) const
 Draws the framebuffer image to the screen.
 
void draw (double x, double y) const
 
void draw (Mat3 modelview, const ShaderProgram *=nullptr) const
 Draws the framebuffer image to the screen.
 
void draw (Mat3 modelview, TextureFilter textureFilter, const ShaderProgram *=nullptr) const
 Draws the framebuffer image to the screen with a specific texture filter.
 
void drawMesh (const std::vector< Vertex > &vertexes, const ShaderProgram *=nullptr) const
 Draws a list of triangles with the framebuffer's texture on it.
 
void drawMesh (const Mat3 &modelview, const std::vector< Vertex > &vertexes, const ShaderProgram *shaderProgram=nullptr) const
 Draws a list of triangles with the framebuffer's texture on it, with modelview instead of the global one, and tinted by color, e.g.
 
void drawMesh (const Mat3 &modelview, const std::vector< Vertex > &vertexes, Rgba color, const ShaderProgram *=nullptr) const
 
Vec2 getSize () const
 Returns the size in screen pixels.
 
Pixels getPixelWidth () const
 
Pixels getPixelHeight () const
 
uint32_t getTextureID () const
 Returns the OpenGL texture ID of the associated image buffer.
 

Static Public Member Functions

static void clear ()
 Clear the framebuffer with the color set by jngl::setBackgroundColor.
 

Detailed Description

Image framebuffer object which can be rendered on.

Example:

FrameBuffer foo(300_px, 200_px);
// ...
{
auto context = foo.use();
context.clear(0xff0000_rgb);
jngl::print("Hello World!", jngl::Vec2(-100, -20));
} // jngl::FrameBuffer::Context is destroyed, now drawing on the main buffer again
// ...
foo.draw(jngl::modelview()); // draws a red rectangle with Hello World! on it
Image framebuffer object which can be rendered on.
Two-dimensional vector.
Definition Vec2.hpp:36
void print(const std::string &text, jngl::Vec2 position)
Print text at position.
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.

Definition at line 38 of file FrameBuffer.hpp.

Constructor & Destructor Documentation

◆ FrameBuffer()

FrameBuffer ( Pixels width,
Pixels height,
bool hdr = false,
bool antiAliased = false )

Creates a framebuffer object with width times height actual pixels.

Parameters
hdrIf true, the framebuffer stores floating point channels (GL_RGBA16F) instead of 8-bit ones. This avoids banding in smooth gradients and lets channel values exceed 1.0, which is useful for HDR lighting.
antiAliasedIf true, what's drawn into it is anti-aliased like what's drawn onto the window: multisampled as long as jngl::getAntiAliasing() is true and the window supports it. Each time it's done being used (see use()), the samples are resolved into the image that's drawn, which costs a little, so it's only worth it for shapes, e.g. jngl::drawCircle(), not for e.g. sprites, which are smooth anyway. While jngl::setAntiAliasing(false), it still has its samples and resolves them, only without smoothing anything (except on OpenGL ES, which can't turn it off, like on the window): to save that too, make it again with antiAliased false.

Member Function Documentation

◆ use()

Context use ( ) const

Starts drawing on the FrameBuffer as long as Context is alive.

The projection matrix gets adjusted so that (0, 0) is in the center of the FrameBuffer. The modelview matrix is saved (i.e. pushMatrix() is called) and restored by ~Context (i.e. popMatrix() is called).

◆ draw() [1/2]

void draw ( Mat3 modelview,
const ShaderProgram * = nullptr ) const

Draws the framebuffer image to the screen.

Parameters
shaderProgramIf not nullptr, this shader program is used instead of the default.

◆ draw() [2/2]

void draw ( Mat3 modelview,
TextureFilter textureFilter,
const ShaderProgram * = nullptr ) const

Draws the framebuffer image to the screen with a specific texture filter.

Parameters
textureFilterUse this filtering for this one draw call, ignoring jngl::AppParameters::pixelArt. You'd probably want to use TextureFilter::NearestNeighbor for cases where you have a framebuffer the same size as the screen.
shaderProgramIf not nullptr, this shader program is used instead of the default.

◆ drawMesh() [1/2]

void drawMesh ( const std::vector< Vertex > & vertexes,
const ShaderProgram * = nullptr ) const

Draws a list of triangles with the framebuffer's texture on it.

The texture's v coordinate runs from 1 at the framebuffer's top to 0 at its bottom.

◆ drawMesh() [2/2]

void drawMesh ( const Mat3 & modelview,
const std::vector< Vertex > & vertexes,
const ShaderProgram * shaderProgram = nullptr ) const

Draws a list of triangles with the framebuffer's texture on it, with modelview instead of the global one, and tinted by color, e.g.

its alpha for a see-through framebuffer

Parameters
shaderProgramPassing nullptr uses the default.

◆ clear()

static void clear ( )
static

Clear the framebuffer with the color set by jngl::setBackgroundColor.

Deprecated
use jngl::FrameBuffer::Context::clear(Color) instead

◆ getTextureID()

uint32_t getTextureID ( ) const

Returns the OpenGL texture ID of the associated image buffer.

While this is an implementation detail, it can be useful if you want to draw the GL_TEXTURE_2D object yourself or need to pass it to another library. The return type is equivalent to GLuint.

Deprecated
There's no OpenGL texture with other rendering backends (e.g. Vulkan). Draw the FrameBuffer with draw() instead, also with your own ShaderProgram.

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