如何制作基本的FPS计数器? [英] How to Make a Basic FPS Counter?

查看:94
本文介绍了如何制作基本的FPS计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的多维数据集渲染程序中显示每秒的帧数.我想看看它的表现.那么,我该怎么办呢?我已经对此进行了研究,但是我看到的示例使用了多个类,但仍然无法使用,或者它们使用的是我没有的库.有没有办法通过使用预安装的库(例如ctime)来获取FPS?我在C ++中使用OpenGL.

I'm trying to display my frames-per-second in my cube-rendering program. I would like to see its performance. So, how can I do it? I have done research on this already, but the examples I've seen use either multiple classes and still don't work, or they use libraries that I don't have. Is there a way to get the FPS by using pre-installed libs like ctime? I am using OpenGL with C++.

这是我的(空)函数:

void GetFPS()
{

}

然后使用以下命令在渲染功能中显示我的FPS:

and then I display my FPS in my render function with:

std::cout << xRot << " " << yRot << " " << zRot << " " << FPS << "\n"; //xRot, yRot, and zRot are my cube's rotation.

我的程序设置为60FPS,但我想查看实际的FPS,而不是它的设置.

My program is set to 60FPS, but I would like to see the actual FPS, not what it's set to.

推荐答案

您必须使用clock()采样2个不同的时间间隔 但是请注意,存在几个问题:

You have to sample 2 different time intervals using clock() however notes that there are several problems:

  • 时钟的分辨率是几毫秒(您可以使用std :: chrono等来解决,但是根据实现的不同,甚至chrono也可能没有那么高的分辨率.在装有GCC 4.9.1的PC上,我的分辨率甚至都不会超过16毫秒使用std :: chrono.
  • 通常使用clock()您将获得0多次,并且有时将测量一个实时时间(在我的情况下,它只是跳了15/16毫秒)
  • 除非您使用垂直同步(vsync),否则您将不会测量实际帧时间,而只会测量渲染循环中花费的CPU时间(要激活vsync,您必须通过OS函数或例如使用库来设置SetSwapInterval(1)像SDL一样提供了可移植的跨平台实现方式
  • 要测量真实的渲染时间,您可以使用GL的时间查询(您随时都可以绑定1个计时器,因此,如果要测量帧速率,则无法测量特定渲染需要多长时间).
  • 不要测量FPS(好吧,除非您只想向用户展示它),而是以毫秒为单位测量帧时间,这样可以更直观地估算性能. (您知道从100 FPS到80 FPS的间隔是2.5毫秒,从40 FPS到20 FPS的间隔是25毫秒!)
  • resolution of clock is several milliseconds (you may workaround using std::chrono etc, however even chrono may have not so high resolution depending on implementation. On my PC with GCC 4.9.1 I never get better resolution than 16 milliseconds even with std::chrono.
  • tipically using clock() you will get 0 many times and at some time you will measure a real time (in my case it just make a jump of 15/16 milliseconds)
  • unless you are using vertical syncronization (vsync), you will not measure real frametime but just the CPU time spent in your render loop (to activate vsync you have to SetSwapInterval(1) wich your OS function or for example using a library like SDL that provide portable cross platform implementaiton)
  • To measure real rendering time you may use a GL'S time query (you may have only 1 timer bound at any time so if you are measuring framerate you cann't measure how long takes render something specific).
  • Do not measure FPS (well unless you want just to show it to users), instead measure frame time in milliseconds, that gives much more intuitive approximation of performance. (you know going from 100 to 80 FPS is 2,5 ms difference, going from 40 to 20 FPS is 25 ms difference!)

这样做:

double clockToMilliseconds(clock_t ticks){
    // units/(units/time) => time (seconds) * 1000 = milliseconds
    return (ticks/(double)CLOCKS_PER_SEC)*1000.0;
}
//...

clock_t deltaTime = 0;
unsigned int frames = 0;
double  frameRate = 30;
double  averageFrameTimeMilliseconds = 33.333;

while(rendering){

    clock_t beginFrame = clock();
    render();
    clock_t endFrame = clock();

    deltaTime += endFrame - beginFrame;
    frames ++;

    //if you really want FPS
    if( clockToMilliseconds(deltaTime)>1000.0){ //every second
        frameRate = (double)frames*0.5 +  frameRate*0.5; //more stable
        frames = 0;
        deltaTime -= CLOCKS_PER_SEC;
        averageFrameTimeMilliseconds  = 1000.0/(frameRate==0?0.001:frameRate);

        if(vsync)
            std::cout<<"FrameTime was:"<<averageFrameTimeMilliseconds<<std::endl;
        else
           std::cout<<"CPU time was:"<<averageFrameTimeMilliseconds<<std::endl;
    }
}

当您执行需要几秒钟的操作时,上面的代码也适用.我进行的计算每秒更新一次,您也可以更频繁地更新它. (请注意,我在大多数需要FPS的项目中都使用了该代码)

The above code works also when you do something that takes several seconds. I do a computation that is updated every second, you could as well update it more often. (note I use exactly that code in most of my projects that need FPS)

这篇关于如何制作基本的FPS计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆