This tutorial shows how to use framebuffer objects to perform offscreen rendering.
Visualization Library uses the following classes to implement framebuffer objects and framebuffer attachments:
class App_Framebuffer_Object: public BaseDemo
{
static const int mFBO_Size = 1024;
public:
App_Framebuffer_Object(int test): mTestNum(test) {}
void initTest_FBO_Render_To_Texture()
{
setRendering(render_tree.
get());
mRTT_Rendering->camera()->viewport()->setClearColor( vl::crimson );
mRTT_Rendering->camera()->viewport()->set(0, 0, mFBO_Size, mFBO_Size);
mRTT_Rendering->camera()->setProjectionPerspective();
mRTT_Rendering->camera()->setViewMatrix(m);
mRTT_Rendering->renderer()->setFramebuffer( framebuffer_object.
get() );
mMainRendering->camera()->viewport()->setClearColor( vl::midnightblue );
mMainRendering->camera()->viewport()->set(0, 0, openglContext()->framebuffer()->width(), openglContext()->framebuffer()->height());
mMainRendering->camera()->setViewMatrix(m);
mMainRendering->renderer()->setFramebuffer( openglContext()->framebuffer() );
}
void initTest_FBO_Render_To_Texture_MRT()
{
{
exit(1);
}
setRendering(render_tree.
get());
mRTT_Rendering->camera()->viewport()->setClearColor( vl::crimson );
mRTT_Rendering->camera()->viewport()->set(0, 0, mFBO_Size, mFBO_Size);
mRTT_Rendering->camera()->setProjectionPerspective();
mRTT_Rendering->camera()->setViewMatrix(m);
mRTT_Rendering->renderer()->setFramebuffer( framebuffer_object.
get() );
{
}
else
{
}
mRTT_Rendering->onFinishedCallbacks()->push_back(read_pixels_0.
get());
read_pixels_0->
setSavePath(
"MRT-COLOR_ATTACHMENT0.tif");
mRTT_Rendering->onFinishedCallbacks()->push_back(read_pixels_1.
get());
read_pixels_1->
setSavePath(
"MRT-COLOR_ATTACHMENT1.tif");
mMainRendering->camera()->viewport()->setClearColor( vl::midnightblue );
mMainRendering->camera()->viewport()->set(0, 0, openglContext()->framebuffer()->width(), openglContext()->framebuffer()->height());
mMainRendering->camera()->setViewMatrix(m);
mMainRendering->renderer()->setFramebuffer( openglContext()->framebuffer() );
addCube(texture1.
get(), texture2.
get() );
}
void initTest_FBO_Render_To_Color_Buffer_And_Copy_To_Texture()
{
setRendering(render_tree.
get());
mRTT_Rendering->camera()->viewport()->setClearColor( vl::crimson );
mRTT_Rendering->camera()->viewport()->set(0, 0, mFBO_Size, mFBO_Size);
mRTT_Rendering->camera()->setProjectionPerspective();
mRTT_Rendering->camera()->setViewMatrix(m);
mRTT_Rendering->renderer()->setFramebuffer( framebuffer_object.
get() );
mRTT_Rendering->onFinishedCallbacks()->push_back(copytex.
get());
mMainRendering->camera()->viewport()->setClearColor( vl::midnightblue );
mMainRendering->camera()->viewport()->set(0, 0, openglContext()->framebuffer()->width(), openglContext()->framebuffer()->height());
mMainRendering->camera()->setViewMatrix(m);
mMainRendering->renderer()->setFramebuffer( openglContext()->framebuffer() );
}
void initTest_FBO_Framebuffer_Blit_Multisample()
{
{
exit(1);
}
const int samples = 4;
setRendering(render_tree.
get());
mRTT_Rendering->camera()->viewport()->setClearColor( vl::crimson );
mRTT_Rendering->camera()->viewport()->set(0, 0, mFBO_Size, mFBO_Size);
mRTT_Rendering->camera()->setProjectionPerspective();
mRTT_Rendering->camera()->setViewMatrix(m);
mRTT_Rendering->renderer()->setFramebuffer( framebuffer_object.
get() );
mRTT_Rendering->onFinishedCallbacks()->push_back(blit_fbo.
get());
blit_fbo->
setSrcRect( 0, 0, mFBO_Size, mFBO_Size );
blit_fbo->
setDstRect( 0, 0, mFBO_Size, mFBO_Size );
mMainRendering->camera()->viewport()->setClearColor( vl::midnightblue );
mMainRendering->camera()->viewport()->set(0, 0, openglContext()->framebuffer()->width(), openglContext()->framebuffer()->height());
mMainRendering->camera()->setViewMatrix(m);
mMainRendering->renderer()->setFramebuffer( openglContext()->framebuffer() );
}
void initTest_FBO_Multisample_Texture()
{
{
exit(1);
}
const int samples = 4;
setRendering(render_tree.
get());
mRTT_Rendering->camera()->viewport()->setClearColor( vl::crimson );
mRTT_Rendering->camera()->viewport()->set(0, 0, mFBO_Size, mFBO_Size);
mRTT_Rendering->camera()->setProjectionPerspective();
mRTT_Rendering->camera()->setViewMatrix(m);
mRTT_Rendering->renderer()->setFramebuffer( framebuffer_object.
get() );
mMainRendering->camera()->viewport()->setClearColor( vl::midnightblue );
mMainRendering->camera()->viewport()->set(0, 0, openglContext()->framebuffer()->width(), openglContext()->framebuffer()->height());
mMainRendering->camera()->setViewMatrix(m);
mMainRendering->renderer()->setFramebuffer( openglContext()->framebuffer() );
}
void initTest_RTT_Legacy()
{
setRendering(render_tree.
get());
mMainRendering->renderer()->setFramebuffer( openglContext()->framebuffer() );
mRTT_Rendering->renderer()->setFramebuffer( openglContext()->framebuffer() );
mRTT_Rendering->camera()->viewport()->setClearColor( vl::crimson );
mRTT_Rendering->camera()->viewport()->set(0,0,512,512);
mRTT_Rendering->camera()->setProjectionPerspective();
mRTT_Rendering->camera()->setViewMatrix( m );
mRTT_Rendering->onFinishedCallbacks()->push_back( copytex.
get() );
mMainRendering->camera()->viewport()->setClearColor( vl::midnightblue );
mMainRendering->camera()->viewport()->set(0,0,512,512);
mMainRendering->camera()->setProjectionPerspective();
mMainRendering->camera()->setViewMatrix( m );
}
{
{
}
const vl::real size = 50;
vl::ref<vl::Geometry> ground =
vl::makeGrid(
vl::vec3(0,0,0), size, size, 2, 2,
true,
vl::fvec2(0,0),
vl::fvec2(1,1) );
if (texture2)
{
}
else
{
}
}
{
}
{
addActor(mRTT_Rendering.get(), ring1.
get(), effect.
get(), mTransfRing1.get());
addActor(mRTT_Rendering.get(), ring2.
get(), effect.
get(), mTransfRing2.get());
addActor(mRTT_Rendering.get(), ring3.
get(), effect.
get(), mTransfRing3.get());
addActor(mRTT_Rendering.get(), ring4.
get(), effect.
get(), mTransfRing4.get());
addActor(mRTT_Rendering.get(), ring5.
get(), effect.
get(), mTransfRing5.get());
mRTT_Rendering->transform()->addChild(mTransfRing1.get());
mTransfRing1->addChild(mTransfRing2.get());
mTransfRing2->addChild(mTransfRing3.get());
mTransfRing3->addChild(mTransfRing4.get());
mTransfRing4->addChild(mTransfRing5.get());
}
virtual void initEvent()
{
{
exit(1);
}
switch(mTestNum)
{
case 0: initTest_FBO_Render_To_Texture();
break;
case 1: initTest_FBO_Render_To_Texture_MRT();
break;
case 2: initTest_FBO_Render_To_Color_Buffer_And_Copy_To_Texture();
break;
case 3: initTest_FBO_Framebuffer_Blit_Multisample();
break;
case 4: initTest_FBO_Multisample_Texture();
break;
case 5: initTest_RTT_Legacy();
break;
default:
break;
}
bindManipulators( mMainRendering->camera() );
}
void resizeEvent(int w, int h)
{
mMainRendering->camera()->viewport()->setWidth ( w );
mMainRendering->camera()->viewport()->setHeight( h );
mMainRendering->camera()->setProjectionPerspective();
}
virtual void updateScene()
{
}
virtual void destroyEvent()
{
BaseDemo::destroyEvent();
}
protected:
int mTestNum;
vl::real mX, mY, mX2, mY2, mX3;
};