查看: 1357|回复: 4

如何让一根骨骼自动旋转啊??

[复制链接]

该用户从未签到

发表于 2012-5-5 17:09:04 | 显示全部楼层 |阅读模式
/* -*-c++-*- Copyright (C) 2009 Wang Rui <wangray84 at gmail dot com>
* OpenSceneGraph Engine Book - Design and Implementation
* How to create character animations
*/

#include <osg/ShapeDrawable>
#include <osgAnimation/Bone>
#include <osgAnimation/Skeleton>
#include <osgAnimation/BasicAnimationManager>
#include <osgViewer/Viewer>


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*2, 10, 2, 2 );
    osg:uat quat;
    quat.makeRotate( osg::Vec3(0.0,1.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* keyframes =
        channel->getOrCreateSampler()->getOrCreateKeyframeContainer();
    keyframes->push_back( osgAnimation::QuatKeyframe(0.0, osg::Quat(0.0, osg::Y_AXIS)) );
    keyframes->push_back( osgAnimation::QuatKeyframe(8.0, osg::Quat(rad, osg::Y_AXIS)) );
    return channel.release();
}
//osg::Matrix* mat1()
//{
//}
//osg:: Matrix::rotate(osg:I_2,  10,  2,  2)
//{
  //  Matrix m;
  //  m.makeRotate(90,10,2,2);
  //   return m;
// }

int main( int argc, char** argv )
{
    osgAnimation::Bone* bone0 = createBone( "bone0", osg::Vec3(0.0,40.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::Matrix mat1;

        //mat1.makeRotate(osg::Quat(osg::PI_2,osg::Vec3(1.0,0.0,0.0)));
        //  osg::ref_ptr<Rotate3DCommand> inverse = new Rotate3DCommand();
//    *inverse = *this;
//    inverse->setRotation(_rotation.inverse());
//    return inverse.release();
// Transform ;
   // rot ->setMatrix(osg::Matrix::rotate(osg:egreesToRadians(90.0), 1, 0, 0)*osg::Matrix::scale(0.5, 0.5, 0.5)*osg::Matrix::translate(4, 0, -2)) ;
  //  rot ->addChild(osgcool.get()) ;

    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() );
   // root ->addChild(rot.get()) ;
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    return viewer.run();
}

该用户从未签到

 楼主| 发表于 2012-5-5 17:09:46 | 显示全部楼层
怎么搞的??Q和P都变成表情了

该用户从未签到

发表于 2012-5-7 11:52:28 | 显示全部楼层
已经在另一个帖子里给出了回答

该用户从未签到

发表于 2016-5-10 14:57:39 | 显示全部楼层
azjxyy 发表于 2012-5-5 17:09
怎么搞的??Q和P都变成表情了

楼主问题解决了吗?

该用户从未签到

发表于 2016-5-10 15:00:45 | 显示全部楼层
array 发表于 2012-5-7 11:52
已经在另一个帖子里给出了回答

是在哪个帖子了呀?找了半天没找到
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表