自定义回调控制OSG模型进行移动操作

来源:互联网 发布:怎么复制淘宝宝贝视频 编辑:程序博客网 时间:2024/05/16 12:12

1、新建vs控制台项目,勾选空项目

2、新建.h文件命名为osg.h,用来包含用到的OSG头文件

#include <osgViewer/Viewer>#include <osgDB/ReadFile>#include <osgViewer/Viewer>#include <osg/Node>#include <osg/MatrixTransform>#include <osgUtil/Optimizer>

3、新建cpp文件,用来写回调,以及主函数


4、在cpp文件中加入以下代码。

//******************************************************************////此文件中添加mycallback回调函数和主程序////******************************************************************//#include "osg.h"#include <Windows.h>//添加回调class mycallback:public osg::NodeCallback{public:mycallback(float pos){m_pos=pos;}virtual void operator() (osg::Node* node,osg::NodeVisitor* nv){osg::ref_ptr<osg::MatrixTransform> transpos=dynamic_cast<osg::MatrixTransform*>(node);if (nv&&transpos&&nv->getFrameStamp()){//double time=nv->getFrameStamp()->getReferenceTime();获取当前运行时间m_pos++;
//飞机运行下一个位置,可以在此设置translate三个参数数学值,让它实现在固定路劲上飞行transpos->setMatrix(osg::Matrix::translate(0.0f+cosf(0.02*m_pos),1.0f,0.0f+sinf(0.02*m_pos))*osg::Matrix::rotate(60,1.0f,0.0f,0.0f));//Sleep(1000);}traverse(node,nv);}protected:int m_pos;};int main(){//主函数//创建viewerosgViewer::Viewer  FirViewer;
//创建group组节点osg::ref_ptr<osg::Group>FirRoot=new osg::Group();
//加载模型NODEosg::ref_ptr<osg::Node>firnode1=osgDB::readNodeFile("D:\\OSG\\data\\glider.osg");
//创建模型矩阵变换matrix节点osg::ref_ptr<osg::MatrixTransform> transpos=new osg::MatrixTransform();
//设置回调transpos->setUpdateCallback(new mycallback(1));
//为回调传入模型指针transpos->addChild(firnode1);
//将变换节点加入组节点FirRoot->addChild(transpos);
//加载viewer显示播放FirViewer.setSceneData(FirRoot);FirViewer.realize();FirViewer.run();return 0;}
5、运行结果,飞机转圈。



原创粉丝点击