In this tutorial you will learn how to use the various interpolator classes to generate splines and smooth aniamtions.
class App_Interpolators: public BaseDemo
{
public:
App_Interpolators()
{
mTest = 0;
}
void initEvent()
{
rendering()->
as<
vl::Rendering>()->transform()->addChild(mTransform1.get());
rendering()->
as<
vl::Rendering>()->transform()->addChild(mTransform2.get());
showCatmullRomPentagonOpen();
showText();
}
void showCatmullRomPentagonOpen()
{
std::vector<vl::fvec3> pentagon;
float radius = 5.0f;
for(int i=0; i<5; ++i)
{
}
showPath(pentagon, vl::green);
std::vector<vl::fvec3> pentagon_spline;
int segments = 41;
for(int i=0; i<segments; ++i)
{
float t = (float)i/(segments-1);
}
showPath(pentagon_spline, vl::red, true);
}
void showCatmullRomPentagonLoop()
{
std::vector<vl::fvec3> pentagon;
float radius = 5.0f;
for(int i=0; i<5; ++i)
{
}
showPath(pentagon, vl::green);
std::vector<vl::fvec3> pentagon_loop;
int segments = 50;
for(int i=0; i<segments; ++i)
{
float t = (float)i/segments;
}
showPath(pentagon_loop, vl::yellow, true, true);
}
void showLinearPentagon()
{
std::vector<vl::fvec3> pentagon;
float radius = 5.0f;
for(int i=0; i<5; ++i)
{
}
showPath(pentagon, vl::green);
std::vector<vl::fvec3> pentagon_linear;
int segments = 20;
for(int i=0; i<segments; ++i)
{
float t = (float)i/(segments-1);
}
vl::Actor* line = showPath(pentagon_linear, vl::blue,
true);
}
void showInterpolatorAnimation()
{
std::vector<vl::fvec3> pentagon;
float radius = 5.0f;
for(int i=0; i<5; ++i)
{
}
mCatmullRomInterpolator->interpolator()->setPath( pentagon );
std::vector<vl::fvec3> pentagon_loop;
mCatmullRomInterpolator->interpolator()->setupEndPoints(true);
int segments = 50;
for(int i=0; i<segments; ++i)
{
float t = (float)i/segments;
pentagon_loop.push_back( mCatmullRomInterpolator->computePoint(t)*1.2f );
}
showPath(pentagon_loop, vl::yellow, true, true);
mLinearInterpolator->interpolator()->setPath( pentagon );
mLinearInterpolator->interpolator()->path().push_back(pentagon[0]);
std::vector<vl::fvec3> pentagon_linear;
segments = 20;
for(int i=0; i<segments; ++i)
{
float t = (float)i/segments;
pentagon_linear.push_back( mLinearInterpolator->computePoint(t) );
}
showPath(pentagon_linear, vl::blue, true, true);
sceneManager()->tree()->addActor( arrow.
get(), arrow_fx.
get(), mTransform1.get() );
sceneManager()->tree()->addActor( arrow.
get(), arrow_fx.
get(), mTransform2.get() );
}
vl::Actor* showPath(
const std::vector<vl::fvec3>& ctrl_points,
const vl::fvec4& color,
bool points=
false,
bool loop=
false)
{
if (points)
return sceneManager()->tree()->addActor( geom.
get(), effect.
get(),
NULL );
}
void updateScene()
{
if (mTest == 3)
{
float delta = 1.0f/50.0f;
mTransform1->setLocalMatrix((
vl::mat4)m);
mTransform2->setLocalMatrix((
vl::mat4)m);
}
}
void keyPressEvent(
unsigned short ch,
vl::EKey key)
{
BaseDemo::keyPressEvent(ch,key);
{
mTest--;
mTest++;
if (mTest < 0) mTest = 3;
if (mTest > 3) mTest = 0;
sceneManager()->tree()->actors()->clear();
switch(mTest)
{
case 0: showCatmullRomPentagonOpen(); break;
case 1: showCatmullRomPentagonLoop(); break;
case 2: showLinearPentagon(); break;
case 3: showInterpolatorAnimation(); break;
}
showText();
}
}
void showText()
{
text->
setText(
"Press the left/right arrow keys to change test.");
sceneManager()->tree()->addActor(text.
get(), effect.
get());
}
protected:
int mTest;
};