#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Group>
#include <osg/AutoTransform>
#include <osg/ProxyNode>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgText/Text>
#include <osgUtil/Optimizer>
#include<osg/Camera>
#include<osgViewer/Viewer>
#include <osgManipulator/Dragger>
#include <osgManipulator/Selection>
#include <osgManipulator/Constraint>
#include <osgManipulator/TranslateAxisDragger>
#include <osgManipulator/CommandManager>
#include<osgFX/Scribe>
#include<osg/MatrixTransform>
#include<osgAnimation/Animation>
#include<osgAnimation/Bone>
#include<osgAnimation/Skeleton>
#include<osgAnimation/UpdateMatrixTransform>
#include<osgAnimation/BasicAnimationManager>
#include <iostream>
#include <thread>
#include <future>
#include <string>
#include<map>
#include<vector>
#include<list>
using std::vector;
using std::string;
using std::list;
using namespace std;

osgAnimation::Bone* createBone(const char* name, const osg::Vec3& trans)
{
	osg::ref_ptr<osgAnimation::Bone> bone = new osgAnimation::Bone;
	bone->setMatrixInSkeletonSpace(osg::Matrix::translate(trans));
	bone->setName(name);
	bone->setDefaultUpdateCallback();

	osg::ref_ptr<osg::Box> box = new osg::Box(trans*0.5, trans.length(), 0.2, 0.2);

	osg::Quat quat;
	quat.makeRotate(osg::Vec3(1.0, 0.0, 0.0), trans);
	box->setRotation(quat);

	osg::ref_ptr<osg::Geode>geode = new osg::Geode;
	geode->addDrawable(new osg::ShapeDrawable(box.get()));
	bone->addChild(geode.get());

	return bone.release();
}

osgAnimation::Channel* createChannel(const char* name, float rad)
{
	osg::ref_ptr<osgAnimation::QuatSphericalLinearChannel> channel = new osgAnimation::QuatSphericalLinearChannel;
	channel->setName("quaternion");
	channel->setTargetName(name);

	osgAnimation::QuatKeyframeContainer* ks = channel->getOrCreateSampler()->getOrCreateKeyframeContainer();
	ks->push_back(osgAnimation::QuatKeyframe(0.0, osg::Quat(0.0, osg::Y_AXIS)));
	ks->push_back(osgAnimation::QuatKeyframe(8.0, osg::Quat(rad, osg::Y_AXIS)));

	return channel.release();
}
int main()
{
	
	osgAnimation::Bone* bone0 = createBone("bone0", osg::Vec3(0.0, 0.0, 0.0));
	osgAnimation::Bone* bone1 = createBone("bone1", osg::Vec3(1.0, 0.0, 0.0));
	osgAnimation::Bone* bone2 = createBone("bone2", osg::Vec3(1.0, 0.0, 0.0));

	osg::ref_ptr < osgAnimation::Skeleton> skelroot = new osgAnimation::Skeleton;
	skelroot->setDefaultUpdateCallback();
	skelroot->addChild(bone0);
	bone0->addChild(bone1);
	bone1->addChild(bone2);

	osg::ref_ptr<osgAnimation::Animation>anim = new osgAnimation::Animation;
	anim->setPlayMode(osgAnimation::Animation::PPONG);
	anim->addChannel(createChannel("bone1", osg::PI_2));
	anim->addChannel(createChannel("bone2", osg::PI_2));

	osg::ref_ptr<osgAnimation::BasicAnimationManager> manager = new osgAnimation::BasicAnimationManager;
	manager->registerAnimation(anim.get());
	manager->playAnimation(anim.get());

	osg::ref_ptr<osg::Group> root = new osg::Group;
	root->addChild(skelroot.get());
	root->setUpdateCallback(manager.get());

	osgViewer::Viewer viewer;
	viewer.setSceneData(root.get());
	return viewer.run();
}

在osg 3.6.2版本下动画失败,会弹出如下提示:

Warning: a Bone was found after a non-Bone child within a Skeleton. Children of a Bone must be ordered with all child Bones first for correct update order.

原因是骨骼的孩子节点不能是非骨骼节点,即上面代码第53行插入box作为骨骼节点的孩子导致的,那不能插入box就看不见骨骼了啊。

Logo

NVIDIA官方入驻,分享最新的官方资源以及活动/会议信息,精选收录AI相关技术内容,欢迎大家加入社区并参与讨论。

更多推荐