opengl如何把glutIdleFunc的帧数调低,感觉转动时速度太快,可以不调角度来让旋转变慢么?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了opengl如何把glutIdleFunc的帧数调低,感觉转动时速度太快,可以不调角度来让旋转变慢么?相关的知识,希望对你有一定的参考价值。
参考技术A 你可以试试调用Windows的多媒体时钟,可以获得Windows时间。也就是说如果你把系统的1s“延长”,旋转就可以变慢。可能不太好,我只是猜想如何在 OpenGL 中捕获准确的帧速率
【中文标题】如何在 OpenGL 中捕获准确的帧速率【英文标题】:How to capture accurate framerate in OpenGL 【发布时间】:2010-01-29 05:23:53 【问题描述】:在原生 windows opengl c++ 中获得准确帧率(每秒帧数)的好方法是什么?
【问题讨论】:
您可能希望切换到每帧毫秒数:mvps.org/directx/articles/fps_versus_frame_time.htm 【参考方案1】:这是我以前在 ATL 项目中使用的计时器类。有一段时间没玩过C++或者opengl了,不过也许这会给你一些想法:
用法
// Put this in your class somewhere
CTimer m_timer;
// Initialize the timer using
m_timer.Init();
// Call this everytime you call draw your scene
m_timer.Update();
// Call this to get the frames/sec
m_timer.GetFPS();
定时器类
// Timer.h: Timer class used for determining elapsed time and
// frames per second.
//
//////////////////////////////////////////////////////////////////////
#ifndef _E_TIMER_H
#define _E_TIMER_H
#pragma once
//////////////////////////////////////////////////////////////////////
// INCLUDES
//////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <stdio.h>
#include <math.h>
//////////////////////////////////////////////////////////////////////
// CLASSES
//////////////////////////////////////////////////////////////////////
class CTimer
private:
//performance timer variables
__int64 m_i64PerformanceTimerStart;
__int64 m_i64PerformanceTimerElapsed;
//multimedia timer variables
unsigned long m_ulMMTimerElapsed;
unsigned long m_ulMMTimerStart;
//general timer variables
__int64 m_i64Frequency;
float m_fResolution;
bool m_bPerformanceTimer;
//FPS variables
float m_fTime1;
float m_fTime2;
float m_fDiffTime;
float m_fFPS;
int m_iFramesElapsed;
public:
//----------------------------------------------------------
// Name: CTimer::CTimer
// Desc: Default constructor
// Args: None
// Rets: None
//----------------------------------------------------------
CTimer( void )
: m_fFPS(0.0f), m_fTime1(0.0f), m_fTime2(0.0f), m_fDiffTime(0.0f), m_iFramesElapsed(0)
//----------------------------------------------------------
// Name: CTimer::CTimer
// Desc: Default destructor
// Args: None
// Rets: None
//----------------------------------------------------------
virtual ~CTimer( void )
//----------------------------------------------------------
// Name: CTimer::Init - public
// Desc: Initiate the timer for the program
// Args: None
// Rets: bool: -true: using performance timer
// -false: using multimedia timer
//----------------------------------------------------------
bool Init( void )
//check to see if we are going to be using the performance counter
if( QueryPerformanceFrequency( ( LARGE_INTEGER* )&m_i64Frequency ) )
//we are able to use the performance timer
m_bPerformanceTimer= true;
//get the current time and store it in m_i64PerformanceTimerStart
QueryPerformanceCounter( ( LARGE_INTEGER* )&m_i64PerformanceTimerStart );
//calculate the timer resolution
m_fResolution= ( float )( ( ( double )1.0f )/( ( double )m_i64Frequency ) );
//initialize the elapsed time variable
m_i64PerformanceTimerElapsed= m_i64PerformanceTimerStart;
//we cannot use the performence counter, so we'll use the multimedia counter
else
//we're using the multimedia counter
m_bPerformanceTimer= false;
m_ulMMTimerStart = timeGetTime( ); //record the time the program started
m_ulMMTimerElapsed = m_ulMMTimerStart; //initialize the elapsed time variable
m_fResolution = 1.0f/1000.0f;
m_i64Frequency = 1000;
return m_bPerformanceTimer;
//----------------------------------------------------------
// Name: CTimer::Update - public
// Desc: Update the timer (perform FPS counter calculations)
// Args: None
// Rets: None
//----------------------------------------------------------
void Update( void )
//increase the number of frames that have passed
m_iFramesElapsed++;
if ( m_iFramesElapsed % 5 == 1 )
m_fTime1 = GetTime( )/1000;
else if ( m_iFramesElapsed % 5 == 0 )
m_fTime1 = m_fTime2;
m_fTime2 = GetTime( )/1000;
m_fDiffTime= ( float )fabs( m_fTime2-m_fTime1 );
m_fFPS= 5/( m_fDiffTime );
/*m_fTime2 = GetTime( )/1000;
m_fDiffTime= ( float )fabs( m_fTime2-m_fTime1 );
if (m_fDiffTime > 1.0f)
m_fTime1 = m_fTime2;
m_fFPS= m_iFramesElapsed / ( m_fDiffTime );
m_iFramesElapsed = 0;
*/
//----------------------------------------------------------
// Name: CTimer::GetTime - public
// Desc: Get the current time since the program started
// Args: None
// Rets: float: The time elapsed since the program started.
//----------------------------------------------------------
float GetTime( void )
__int64 i64Time;
//check to see if we are using the performance counter
if( m_bPerformanceTimer )
//get the current performance time
QueryPerformanceCounter( ( LARGE_INTEGER* )&i64Time );
//return the time since the program started
return ( ( float )( i64Time - m_i64PerformanceTimerStart )*m_fResolution )*1000.0f;
//we are using the multimedia counter
else
//return the time since the program started
return ( ( float )( timeGetTime( ) - m_ulMMTimerStart )*m_fResolution )*1000.0f;
//----------------------------------------------------------
// Name: CTimer::GetElapsedSeconds - public
// Desc: Get the elapsed seconds since the last frame was drawn.
// Args: elapsedFrames:
// Rets: float: The time elapsed since the program started.
//----------------------------------------------------------
float GetElapsedSeconds(unsigned long elapsedFrames = 1)
return m_fDiffTime;
//----------------------------------------------------------
// Name: CTimer::GetFPS - public
// Desc: Get the current number of frames per second
// Args: None
// Rets: float: the number of frames per second
//----------------------------------------------------------
inline float GetFPS( void )
return m_fFPS;
;
#endif // _E_TIMER_H
【讨论】:
这门课太棒了!如果可以的话,我会给你两个赞成票。我将它用于流畅的鼠标外观代码和玩家移动。【参考方案2】:在 c++ 中,我最喜欢的计时器与史蒂夫建议的相同。
在你的 opengl 应用程序中也可能存在禁用 vsync 的问题,对我来说它一直默认开启,你必须加载一些功能来禁用它。
至于可能更独立于平台的解决方案,
使用时间.h
我不记得函数了
// in your loop:
//////////
static int lastTime = GetTime();
static int framesDone = 0;
int currentTime = GetTime();
if(currentTime > lastTime)
int fps = framesDone;
framesDone = 0;
lastTime = currentTime;
framesDone++;
/////////
但是,对于 Windows,第一个答案是最好的。
如果您在禁用垂直同步方面需要帮助,请告诉我们。
【讨论】:
以上是关于opengl如何把glutIdleFunc的帧数调低,感觉转动时速度太快,可以不调角度来让旋转变慢么?的主要内容,如果未能解决你的问题,请参考以下文章