OSG学习:使用已有回调示例

 回调的类型有很多种,一般很容易就想到的是UpdateCallBack,或者EventCallBack,回调的意思就是说,你可以规定在某件事情发生时启动一个函数,这个函数可能做一些事情。这个函数就叫做回调函数。

#include<osg\MatrixTransform>
#include<osg\PositionAttitudeTransform>
#include<osg\Geode>
#include<osgDB\Registry>
#include<osgDB\ReadFile>
#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.osgt");
	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(int argc, char** argv)
{
	osgViewer::Viewer viewer;
	//创建模型
	osg::Node* model = createModel();
	viewer.setSceneData(model);
	viewer.setCameraManipulator(new osgGA::TrackballManipulator());
	viewer.realize();
	return viewer.run();
}
结果图:



转载于:https://www.cnblogs.com/huahai/p/7270942.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值