// Example1.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <osgLib.h> #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgGA/GUIEventAdapter> #include <osgGA/TrackballManipulator> #include <iostream> using namespace std; class TestHandler:public osgGA::GUIEventHandler { public: bool handle(const osgGA::GUIEventHandler &ea,osgGA::GUIActionAdapter &aa) { return true; } virtual void getUsage(osg::ApplicationUsage &usage) const { usage.addKeyboardMouseBinding("sxj","sxjlinux"); } }; int _tmain(int argc, _TCHAR* argv[]) { osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer; //申请一个定时器类 osg::Timer *timer=new osg::Timer; osg::Timer_t start_timer=0,end_timer=0; osg::Timer_t start_frame_timer=0,end_frame_timer=0; double start_timer1=0.0,end_timer1=0.0; //控制帧速使用睡眠时间 double sleep_time=0.0; double last_sleep=0.0001; //每帧的实际使用时间 double current_time=0.0; //每帧控制法 double per_str_time=0.0; double per_end_time=0.0; //得到一个tick值为多少Seconds cout<<timer->getSecondsPerTick()<<endl; start_timer=timer->tick(); start_timer1=viewer->elapsedTime(); timer->setStartTick(); osg::ref_ptr<osg::Node> node=osgDB::readNodeFile("glider.osg"); cout<<"方法一、读取模型的时间为:"<<timer->time_s()<<endl; end_timer=timer->tick(); end_timer1=viewer->elapsedTime(); cout<<"方法二、读取模型的时间为:"<<timer->delta_s(start_timer,end_timer)<<endl; cout<<"方法三、读取模型的时间为:"<<end_timer1-start_timer1<<endl; viewer->addEventHandler(new osgViewer::HelpHandler); //帮助事件 viewer->addEventHandler(new TestHandler); viewer->addEventHandler(new osgViewer::StatsHandler);//显示帧事件 viewer->addEventHandler(new osgViewer::WindowSizeHandler);//是否全屏事件 viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);//截图事件 viewer->addEventHandler(new osgViewer::RecordCameraPathHandler);//记录当前的路径事件 viewer->setSceneData(node.get()); viewer->realize(); viewer->setCameraManipulator(new osgGA::TrackballManipulator);//加入操作器 //vewer->run(); int counts=0;//计算帧速 while(!viewer->done())//此循环默认情况下一般每秒钟执行60次 { per_str_time=timer->tick(); if (counts==0) { start_frame_timer=timer->tick(); } counts++; viewer->frame(); //方法二 per_end_time=timer->tick(); //0.1表示10帧每秒钟 //sleep_time=0.1-timer->delta_s(per_str_time,per_end_time); /*控制帧速的公式为:每帧睡眠的时间=期望每帧绘制的时间-实际绘制的时间 假如:帧速为x,那么每帧绘制的时间为:1.0/x(秒),实际绘制的的时间为:t 那么需要睡眠的时间=1.0/x-t */ //delta_s是计算per_str_time,per_end_time之间的差值,就是per_end_time-per_str_time sleep_time=1.0/35.0-timer->delta_s(per_str_time,per_end_time); //sleep_time是得到每帧 //如果sleep_time的值小于0,则说明实际绘制的时间大于期望的时间 //因此此时就不需要再睡眠了,如果在继续睡眠,则效率会更差 if (sleep_time<0) { sleep_time=0.0; } OpenThreads::Thread::microSleep(sleep_time*1000000); if (counts==3) { //限制帧速为10,每帧为0.1秒,那么10帧就为1秒 end_frame_timer=timer->tick(); cout<<"当前帧速为:"<<counts/timer->delta_s(start_frame_timer,end_frame_timer)<<endl; counts=0; //得到当前每帧绘制所用时间 //sleep_time=0.1-(timer->delta_s(start_frame_timer,end_frame_timer))/3;//方法一 //if (sleep_time<0)//如果小于0,则表示实际绘制的时间,比期望的时间要大 //{ // //比如期望的帧数为10,但实际绘制的为8帧,因此8-10会小于0 // sleep_time=last_sleep*0.8; // last_sleep=sleep_time; //} } } return 0; }
其中osgLib.h的内容为:
#ifdef _DEBUG #pragma comment(lib,"osgAnimationd.lib") #pragma comment(lib,"osgTerraind.lib") #pragma comment(lib,"osgQtd.lib") #pragma comment(lib,"osgParticled.lib") #pragma comment(lib,"osgPresentationd.lib") #pragma comment(lib,"osgShadowd.lib") #pragma comment(lib,"osgSimd.lib") #pragma comment(lib,"osgVolumed.lib") #pragma comment(lib,"osgManipulatord.lib") #pragma comment(lib,"osgFXd.lib") #pragma comment(lib,"osgWidgetd.lib") #pragma comment(lib,"osgViewerd.lib") #pragma comment(lib,"osgTextd.lib") #pragma comment(lib,"osgGAd.lib") #pragma comment(lib,"osgDBd.lib") #pragma comment(lib,"osgUtild.lib") #pragma comment(lib,"osgd.lib") #pragma comment(lib,"OpenThreadsd.lib") #else #pragma comment(lib,"osgAnimation.lib") #pragma comment(lib,"osgTerrain.lib") #pragma comment(lib,"osgQt.lib") #pragma comment(lib,"osgParticle.lib") #pragma comment(lib,"osgPresentation.lib") #pragma comment(lib,"osgShadow.lib") #pragma comment(lib,"osgSim.lib") #pragma comment(lib,"osgVolume.lib") #pragma comment(lib,"osgManipulator.lib") #pragma comment(lib,"osgFX.lib") #pragma comment(lib,"osgWidget.lib") #pragma comment(lib,"osgViewer.lib") #pragma comment(lib,"osgText.lib") #pragma comment(lib,"osgGA.lib") #pragma comment(lib,"osgDB.lib") #pragma comment(lib,"osgUtil.lib") #pragma comment(lib,"osg.lib") #pragma comment(lib,"OpenThreads.lib") #endif