关闭

OSG控制帧率的方法

763人阅读 评论(0) 收藏 举报
分类:

 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
 osg::ref_ptr<osg::Node> node;
   
 //申请一个定时器
 osg::Timer* timer = new osg::Timer;
 osg::Timer_t start_frame_time = 0;
 osg::Timer_t end_frame_time = 0;
    float start_time;
 float end_time;
 //控制帧率使用的睡眠时间
 float sleep_time = 0.0;
 float last_sleep_time = 0.0;
 //每帧的实际使用时间
 float current_time = 0.0;
 //每帧控制法
 //float per_start_time = 0.0;
 //float per_end_time = 0.0;
 //计算帧率
 int counts = 0;
 //得到一个tick值为多少second
 std::cout << timer->getSecondsPerTick()<<std::endl;
    //start_time = timer->tick();
 start_time = viewer->elapsedTime();
 timer->setStartTick();
 node = osgDB::readNodeFile("glider.osg");
 std::cout<<"方法一:读取模型的时间为:"<<timer->time_s()<<std::endl;
 //end_time = timer->tick();
 end_time = viewer->elapsedTime();
 std::cout<<"方法二:读取模型的时间为:"<<end_time - start_time<<std::endl;
 //添加帮助事件
 viewer->addEventHandler(new PrintName);
 viewer->addEventHandler(new osgViewer::HelpHandler);
 viewer->addEventHandler(new osgViewer::StatsHandler);
 viewer->addEventHandler(new osgViewer::WindowSizeHandler);
 viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);
 viewer->setSceneData(node);

 viewer->setCameraManipulator(new osgGA::TrackballManipulator());
 while(!viewer->done())
 {
  //per_start_time = timer->tick();
  if(counts == 0)
  {
   start_frame_time = timer->tick();
  }
  counts++;
  
  viewer->frame();
        //per_end_time = timer->tick();
  //sleep_time =   1/35.0 - timer->delta_s(per_start_time, per_end_time);
  if(sleep_time < 0)
  {
   sleep_time = 0.0;
  }
  OpenThreads::Thread::microSleep(sleep_time*1000000);
  //last_sleep_time = sleep_time;
  if(counts == 3)
  {
   //限制帧率为35
   counts = 0;
   end_frame_time = timer->tick();
   std::cout<<"当前帧速为: " << 3/timer->delta_s(start_frame_time, end_frame_time)<<std::endl;
   
   sleep_time =   sleep_time + 1.0/35.0 - timer->delta_s(start_frame_time, end_frame_time)/3;
   if(sleep_time < 0)
    sleep_time = 0;     
  }

转载地址:http://blog.sina.com.cn/s/blog_48ed03c801018blt.html

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:201593次
    • 积分:3104
    • 等级:
    • 排名:第11195名
    • 原创:73篇
    • 转载:284篇
    • 译文:0篇
    • 评论:8条
    最新评论