Visualization Library includes a series of functions to create the most common primitive objects out of the box such as:
The example below demonstrates how to create a simple applet that displays several geometrical primitives and allows the user to navigate through them by pressing the left/right arrow keys. Note how creating a geometrical primitive is just a matter of calling a function such as vl::makeBox(), vl::makeTeapot() etc.
class App_Primitives: public BaseDemo
{
public:
void initEvent()
{
const float objdim = 1;
const float scene_radius = 4;
ref<Effect> fx = new Effect;
fx->shader()->setRenderState( new Light, 0 );
fx->shader()->gocMaterial()->setDiffuse(vl::royalblue);
#if defined(VL_OPENGL)
fx->lod(0)->push_back( new Shader );
fx->shader(0,1)->gocPolygonOffset()->set(-1.0f, -1.0f);
fx->shader(0,1)->gocColor()->setValue(vl::lightgreen);
fx->shader(0,1)->setRenderState( fx->shader()->getMaterial() );
fx->shader(0,1)->setRenderState( fx->shader()->getLight(0), 0 );
#endif
mGeometries.push_back(
vl::makeBox(
vec3( 0, 0, 0 ), objdim*0.8f, objdim*0.8f, objdim*0.8f ) );
mGeometries.push_back(
vl::makeTorus(
vec3( 0, 0, 0 ), objdim*1.2f, 0.2f, 20, 40 ) );
mGeometries.push_back(
vl::makeGrid(
vec3( 0, 0, 0 ), objdim, objdim, 10, 10 ) );
for(size_t i=0; i<mGeometries.size(); ++i)
{
ref<Transform> tr = new Transform;
rendering()->
as<Rendering>()->transform()->addChild( tr.get() );
mat4 m = mat4::getRotation( 360 * i / (real)mGeometries.size(), 0, 1, 0) *
mat4::getTranslation(0,0,-scene_radius);
tr->setLocalMatrix( m );
mGeometries[i]->computeBounds();
m = mat4::getTranslation( -mGeometries[i]->boundingBox().center() );
mGeometries[i]->transform( m );
if (!mGeometries[i]->normalArray())
mGeometries[i]->computeNormals();
sceneManager()->tree()->addActor( mGeometries[i].get(), fx.get(), tr.get() );
}
mRot0 = 0;
mRot1 = 0;
mAngle = 360.0f / mGeometries.size();
}
void keyPressEvent(
unsigned short ch,
vl::EKey key)
{
BaseDemo::keyPressEvent(ch, key);
if ( !mTimer.isStarted() )
{
{
mTimer.start();
mRot0 = mRot1;
mRot1 = mRot1 + mAngle;
}
else
{
mTimer.start();
mRot0 = mRot1;
mRot1 = mRot1 - mAngle;
}
}
}
void updateScene()
{
if ( mTimer.isStarted() )
{
float t = mTimer.elapsed();
if (t>1)
{
t = 1;
mTimer.stop();
}
mat4 m = mat4::getRotation( mRot0*(1.0f-t) + mRot1*t, 0, 1, 0 );
rendering()->
as<Rendering>()->transform()->setLocalMatrix(m);
}
}
protected:
std::vector< ref<Geometry> > mGeometries;
Time mTimer;
float mRot0;
float mRot1;
float mAngle;
};