#include <Windows.h>
#include <osgViewer/Viewer>
#include <osg/Math>
#include <osgDB/ReadFile>
#include <osg/NodeCallback>
#include <osg/MatrixTransform>
//从osg::NodeCallBack继承一个新类,实现simpleCallBack的回调
class simpleCallBack:public osg::NodeCallback
{
public:
simpleCallBack():_angle(0)
{
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
//创建矩阵转换节点
osg::ref_ptr<osg::MatrixTransform> mt=dynamic_cast<osg::MatrixTransform*>(node);
//创建矩阵
osg::Matrix mx;
//绕着Z轴旋转
mx.makeRotate(_angle,osg::Vec3(0.0,0.0,1.0));
//设置矩阵
mt->setMatrix(mx);
_angle+=0.01;
//继续遍历
traverse(node,nv);
}
public:
//旋转角度
double _angle;
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
osg::ref_ptr<osg::Group> root=new osg::Group;
//读取模型
osg::ref_ptr<osg::Node> node=osgDB::readNodeFile("cow.osg");
//创建矩阵变换节点
osg::ref_ptr<osg::MatrixTransform> mt=new osg::MatrixTransform;
mt->addChild(node.get());
//设置更新回调函数
mt->setUpdateCallback(new simpleCallBack());
root->addChild(mt.get());
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
return 0;
}
#include <osgViewer/Viewer>
#include <osg/Math>
#include <osgDB/ReadFile>
#include <osg/NodeCallback>
#include <osg/MatrixTransform>
//从osg::NodeCallBack继承一个新类,实现simpleCallBack的回调
class simpleCallBack:public osg::NodeCallback
{
public:
simpleCallBack():_angle(0)
{
}
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
{
//创建矩阵转换节点
osg::ref_ptr<osg::MatrixTransform> mt=dynamic_cast<osg::MatrixTransform*>(node);
//创建矩阵
osg::Matrix mx;
//绕着Z轴旋转
mx.makeRotate(_angle,osg::Vec3(0.0,0.0,1.0));
//设置矩阵
mt->setMatrix(mx);
_angle+=0.01;
//继续遍历
traverse(node,nv);
}
public:
//旋转角度
double _angle;
};
int main()
{
osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
osg::ref_ptr<osg::Group> root=new osg::Group;
//读取模型
osg::ref_ptr<osg::Node> node=osgDB::readNodeFile("cow.osg");
//创建矩阵变换节点
osg::ref_ptr<osg::MatrixTransform> mt=new osg::MatrixTransform;
mt->addChild(node.get());
//设置更新回调函数
mt->setUpdateCallback(new simpleCallBack());
root->addChild(mt.get());
viewer->setSceneData(root.get());
viewer->realize();
viewer->run();
return 0;
}