Drawing to an offscreen canvas
In this recipe, we will learn how to draw in an offscreen canvas using the OpenGL Frame Buffer Object (FBO).
We will draw in an FBO and draw it onscreen as well as texture a rotating cube.
Getting ready
Include the necessary files to work with OpenGL and the FBOs as well as the useful include
directives.
Add the following code to the top of the source file:
#include "cinder/gl/gl.h" #include "cinder/gl/Fbo.h" using namespace ci;
How to do it…
We will use a ci::gl::Fbo
object, a wrapper to an OpenGL FBO, to draw in an offscreen destination.
Declare a
ci::gl::Fbo
object as well as aci::Vec3f
object to define the cube's rotation:gl::FbomFbo; Vec3f mCubeRotation;
Initialize
mFbo
with a size of 256 x 256 pixels by adding the following code in thesetup
method:mFbo = gl::Fbo( 256, 256 );
Animate
mCubeRotation
in theupdate
method:mCubeRotation.x += 1.0f; mCubeRotation.y += 1.0f;
Declare a method where we will draw to the FBO:
void drawToFbo();
In the implementation of
drawToFbo...