Directx11入门之第四章 初始化Direct3D
@(读书笔记)[读书笔记, 技术交流]
4.3.3 两帧的时间间隔
当我们一帧帧渲染动画时,我们更新物体时需要知道距离上一帧的时间间隔。
- 比如我们维护一辆匀速行驶中的汽车时,上一帧与这一帧的位移量需要根据两帧之间的时间间隔计算得出。
以下说明计算两帧的时间间隔的方式。设 ti t i 为第 i i 帧的Windows性能计数器的值,则 就是第 i−1 i − 1 帧的性能计数器的值。显然两帧的间隔 Δt=ti−ti−1 Δ t = t i − t i − 1 。为了使得实时渲染看起来更平滑流畅,一秒钟渲染的帧数至少需30帧,因此 Δt Δ t 的值必须较小。
以下给出获取 Δt Δ t 的代码
void GameTimer::Tick()
{
if (mStopped)
{
mDeltaTime = 0.0;
return;
}
// Get the time this frame.
__int64 currTime;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
mCurrTime = currTime;
// Time difference between this frame and the previous.
mDeltaTime = (mCurrTime - mPrevTime)*mSecondsPerCount;
// Prepare for next frame.
mPrevTime = mCurrTime;
// Force nonnegative. The DXSDK's CDXUTTimer mentions that if the
// processor goes into a power save mode or we get shuffled to another
// processor, then mDeltaTime can be negative.
if (mDeltaTime < 0.0)
{
mDeltaTime = 0.0;
}
}
float GameTimer::DeltaTime()const
{
return (float)mDeltaTime;
}
在以下循环中调用上述函数Tick
int D3DApp::Run()
{
MSG msg = { 0 };
mTimer.Reset();
while (msg.message != WM_QUIT)
{
// If there are Window messages then process them.
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Otherwise, do animation/game stuff.
else
{
mTimer.Tick();