OSG程序设计教程 第七章
CreateMovingNode
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Geode>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
// create a animation path
osg::AnimationPath * createAnimationPath(const osg::Vec3 ¢er, float radius,double looptime)
{
// declare animation path
osg::AnimationPath * animationPath = new osg::AnimationPath;
// set loop
animationPath->setLoopMode(osg::AnimationPath::LOOP);
// set the number of control points
int numSamples = 40;
float yaw = 0.0f;
float yaw_delta = 2.0f * osg::PI/((float)numSamples - 1.0f);
float roll = osg::inDegrees(30.0f);
// set interval between control points
double time = 0.0f;
double time_delta = looptime/(double)numSamples;
// insert cocntrol points
for (int i=0; i< numSamples; ++i)
{
osg::Vec3 position(0,0,0);
osg::Quat rotation( osg::Quat(roll, osg::Vec3(0.0,1.0,0.0)) * osg::Quat(-(yaw+osg::inDegrees(90.0f)), osg::Vec3(0.0,0.0,1.0)) );
animationPath->insert(time, osg::AnimationPath::ControlPoint(position,rotation));
yaw += yaw_delta;
time += time_delta;
}
return animationPath;
}
osg::Node * createMovingModel(const osg::Vec3 ¢er,float radius)
{
float animationLength = 10.0f;
// animation path
osg::AnimationPath *animationPath = createAnimationPath(center,radius,animationLength);
osg::Group * model = new osg::Group;
// read model, and hide the first child node
osg::Node * fountain = osgDB::readNodeFile("fountain.osg");
fountain->asGroup()->getChild(0)->setNodeMask(0);
// if fountain, update its nanimation path
if (fountain)
{
osg::PositionAttitudeTransform *xform = new osg::PositionAttitudeTransform;
// set update callback
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
// add children node
xform->addChild(fountain);
model->addChild(xform);
}
return model;
}
osg::Node * createModel()
{
osg::Vec3 center(0.0f,0.0f,0.0f);
float radius = 1.0f;
osg::Group *root = new osg::Group;
// create moving node, with radius
osg::Node *movingModel = createMovingModel(center, radius *0.8f);
// add node to root
root->addChild(movingModel);
return root;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->setSceneData(createModel());
viewer->setCameraManipulator(new osgGA::TrackballManipulator());
viewer->realize();
viewer->run();
}
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Geode>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
// create a animation path
osg::AnimationPath * createAnimationPath(const osg::Vec3 ¢er, float radius,double looptime)
{
// declare animation path
osg::AnimationPath * animationPath = new osg::AnimationPath;
// set loop
animationPath->setLoopMode(osg::AnimationPath::LOOP);
// set the number of control points
int numSamples = 40;
float yaw = 0.0f;
float yaw_delta = 2.0f * osg::PI/((float)numSamples - 1.0f);
float roll = osg::inDegrees(30.0f);
// set interval between control points
double time = 0.0f;
double time_delta = looptime/(double)numSamples;
// insert cocntrol points
for (int i=0; i< numSamples; ++i)
{
osg::Vec3 position(0,0,0);
osg::Quat rotation( osg::Quat(roll, osg::Vec3(0.0,1.0,0.0)) * osg::Quat(-(yaw+osg::inDegrees(90.0f)), osg::Vec3(0.0,0.0,1.0)) );
animationPath->insert(time, osg::AnimationPath::ControlPoint(position,rotation));
yaw += yaw_delta;
time += time_delta;
}
return animationPath;
}
osg::Node * createMovingModel(const osg::Vec3 ¢er,float radius)
{
float animationLength = 10.0f;
// animation path
osg::AnimationPath *animationPath = createAnimationPath(center,radius,animationLength);
osg::Group * model = new osg::Group;
// read model, and hide the first child node
osg::Node * fountain = osgDB::readNodeFile("fountain.osg");
fountain->asGroup()->getChild(0)->setNodeMask(0);
// if fountain, update its nanimation path
if (fountain)
{
osg::PositionAttitudeTransform *xform = new osg::PositionAttitudeTransform;
// set update callback
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
// add children node
xform->addChild(fountain);
model->addChild(xform);
}
return model;
}
osg::Node * createModel()
{
osg::Vec3 center(0.0f,0.0f,0.0f);
float radius = 1.0f;
osg::Group *root = new osg::Group;
// create moving node, with radius
osg::Node *movingModel = createMovingModel(center, radius *0.8f);
// add node to root
root->addChild(movingModel);
return root;
}
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->setSceneData(createModel());
viewer->setCameraManipulator(new osgGA::TrackballManipulator());
viewer->realize();
viewer->run();
}
TrackballManipulator貌似没有用。