osg获取帧率
作者:互联网
#include<Windows.h>
#include<osgViewer/Viewer>
#include<osgDB/ReadFile>
#include<osgViewer/ViewerEventHandlers>
#include<osgGA/StateSetManipulator>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
class GetFrameRate :public osgGA::GUIEventHandler
{
public:
GetFrameRate(osgEarth::Util::LabelControl* fpsLabelContral):fpsLabel(fpsLabelContral)
{
curFrameNum = 0;
curTime = 0;
nextFrameNum = 0;
nextTime = 0;
fps = 0;
}
bool handle(const osgGA::GUIEventAdapter&ea, osgGA::GUIActionAdapter&aa)
{
osgViewer::Viewer *viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
//获取帧率
osg::FrameStamp* FS = viewer->getViewerFrameStamp();
curFrameNum = FS->getFrameNumber();
curTime = FS->getSimulationTime();
if ((curTime - nextTime)<=0.005)
{
}
else
{
fps = 1 / ((curTime - nextTime));
char wsrc[512];
sprintf(wsrc, "%.2f", fps);
fpsLabel->setText(wsrc);
std::cout << fps << std::endl;
}
nextFrameNum = FS->getFrameNumber();
nextTime = FS->getSimulationTime();
return false;
}
public:
double curFrameNum, curTime, nextFrameNum, nextTime;
double fps;
osgEarth::Util::LabelControl* fpsLabel;
};
int main()
{
osg::ref_ptr<osgViewer::Viewer>viewer = new osgViewer::Viewer;
//添加状态事件,可以相应键盘和鼠标事件,响应L T B W
viewer->addEventHandler(new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()));
//窗口大小变化,响应F
viewer->addEventHandler(new osgViewer::WindowSizeHandler);
//添加路径记录 Z
viewer->addEventHandler(new osgViewer::RecordCameraPathHandler);
//帮助文档显示H
viewer->addEventHandler(new osgViewer::HelpHandler);
//截屏 C
viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);
//添加一些常用状态设置,响应S
viewer->addEventHandler(new osgViewer::StatsHandler);
osg::ref_ptr<osg::Group>root = new osg::Group;
root->addChild(osgDB::readNodeFile("cow.osg"));
viewer->setSceneData(root.get());
//contralLable显示信息
osgEarth::Util::Grid* grid = new osgEarth::Util::Grid();
//设置几个Label文字控件显示在场景中
grid->setControl(1, 1, new osgEarth::Util::LabelControl("FPS: "));
osgEarth::Util::LabelControl* fpsLabelContral = grid->setControl(2, 1, new osgEarth::Util::LabelControl("60.0"));
grid->setPosition(0, 50);
//控件绘制容器
osgEarth::Util::ControlCanvas* fpsCanvas = new osgEarth::Util::ControlCanvas();
//将要显示的控件加入到root组节点中去
root->addChild(fpsCanvas);
fpsCanvas->addControl(grid);
fpsCanvas->setNodeMask(1);
//获取帧率
viewer->addEventHandler(new GetFrameRate(fpsLabelContral));
viewer->run();
return 0;
}
标签:addEventHandler,viewer,Util,获取,osgEarth,new,include,osg 来源: https://blog.csdn.net/ljjjjjjjjjjj/article/details/123598736