//DEMO18
//功能:隐藏模型fountain.osg下的第一个结点。然后不断的旋转喷头
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Geode>
#include <osgDB/ReadFile>
#include <osgDB/Registry>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
{
//路径实体
osg::AnimationPath* animationPath = new osg::AnimationPath;
//设置循环模式为LOOP
animationPath->setLoopMode(osg::AnimationPath::LOOP);
//设置关键点数
int numSamples = 40;
float yaw = 0.0f;
float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
float roll = osg::inDegrees(30.0f);
//设置时间间隔
double time = 0.0f;
double time_delta = looptime/(double)numSamples;
//插入关键点与时间以及旋转角度和位置
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& center,float radius)
{
float animationLength = 10.0f;
//创建的路径
osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
osg::Group * model = new osg::Group;
//读取模型,并隐藏他下面的第一个结点
osg::Node* fountain = osgDB::readNodeFile("fountain.osg");
fountain->asGroup()->getChild(0)->setNodeMask(0);
//如果读取成功,则更新负于他的路径
if(fountain)
{
osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;
//设置更新回调
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
//加入子模型结点
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;
//创建移动的结点,以radius为半径转圈
osg::Node* movingModel = createMovingModel(center,radius*0.8f);
//把结点加入到root中并返回
root->addChild(movingModel);
return root;
}
int main()
{
osgViewer::Viewer viewer;
//创建模型
osg::Node* model = createModel();
viewer.setSceneData(model);
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
viewer.realize();
return viewer.run();
}
osg demo18 回调
最新推荐文章于 2020-10-22 17:49:13 发布
该示例展示了如何在osg中创建一个动画路径,使模型(fountain.osg)围绕中心点旋转,并隐藏其第一个子节点。通过AnimationPathCallback更新模型的位置和姿态,使用TrackballManipulator进行视角控制。
摘要由CSDN通过智能技术生成