|
楼主 |
发表于 2011-2-28 14:52:50
|
显示全部楼层
是这个程序
/**********************************************************
*Write by FlySky
*zzuxp@163.com http://www.OsgChina.org
**********************************************************/
#include <osgViewer/Viewer>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/Camera>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osg/Sequence>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgUtil/Optimizer>
#include <iostream>
//对节点进行适当的缩放
osg::ref_ptr<osg::Node> createScaledNode(osg::ref_ptr<osg::Node> node, float targetScale)
{
//通过包围盒确定合适的缩放
const osg::BoundingSphere& bsphere = node->getBound();
float scale = targetScale / bsphere._radius;
osg::ref_ptr<osg:ositionAttitudeTransform> pat = new osg::PositionAttitudeTransform() ;
pat->setPosition(osg::Vec3(0.0f,0.0f,0.0f));
pat->setScale(osg::Vec3(scale,scale,scale)) ;
pat->setDataVariance(osg::Object:YNAMIC);
pat->addChild(node.get());
//法线归一化,保证光照明暗均匀
osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet() ;
stateset=pat->getOrCreateStateSet() ;
stateset->setMode(GL_NORMALIZE,osg::StateAttribute::ON);
return pat.get() ;
}
//创建帧动画
osg::ref_ptr<osg::Sequence> createSequence()
{
//创建帧动画对象
osg::ref_ptr<osg::Sequence> seq = new osg::Sequence;
//文件名向量对象
typedef std::vector<std::string> Filenames;
Filenames filenames;
//添加模型名
filenames.push_back("cow.osg");
filenames.push_back("spaceship.osg");
filenames.push_back("dumptruck.osg");
filenames.push_back("cessna.osg");
filenames.push_back("glider.osg");
for(Filenames::iterator itr = filenames.begin();itr != filenames.end();++itr)
{
//加载模型
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(*itr);
if (node)
{
//添加子节点
seq->addChild(createScaledNode(node, 100.0f));
//设定节点的持续时间
seq->setTime(seq->getNumChildren()-1, 1.0f);
}
}
//设置帧动画持续的时间
seq->setInterval(osg::Sequence:OOP, 0,-1);
//设置播放的速度及重复的次数
seq->setDuration(1.0f, -1);
//开始播放
seq->setMode(osg::Sequence::START);
return seq.get();
}
//帧动画事件控制
class SequenceEventHandler : public osgGA::GUIEventHandler
{
public:
//构造函数
SequenceEventHandler(osg::ref_ptr<osg::Sequence> seq)
{
_seq = seq;
}
virtual bool handle(const osgGA::GUIEventAdapter& ea,
osgGA::GUIActionAdapter&)
{
osg::Sequence::SequenceMode mode = _seq->getMode();
if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN)
{
switch (ea.getKey())
{
//暂停
case 'p':
{
mode = osg::Sequence::PAUSE ;
_seq->setMode(mode);
}
break;
//开始
case 's':
{
mode = osg::Sequence::START ;
_seq->setMode(mode) ;
}
break ;
//继续
case 'r':
{
mode = osg::Sequence::RESUME ;
_seq->setMode(mode);
}
break ;
//停止
case 't':
{
mode = osg::Sequence::STOP ;
_seq->setMode(mode) ;
}
break ;
default:
break ;
}
}
return false;
}
private:
osg::ref_ptr<osg::Sequence> _seq;
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
osg::ref_ptr<osg::Group> root = new osg::Group();
//向场景中添加帧动画
osg::ref_ptr<osg::Sequence> sq = new osg::Sequence();
sq = createSequence();
root->addChild(sq.get()) ;
//添加帧动画控制事件
viewer->addEventHandler(new SequenceEventHandler(sq.get()));
osgUtil::Optimizer optimizer ;
optimizer.optimize(root.get()) ;
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
return 0 ;
} |
|