添加双打时的奇怪行为
Strange behaviour when adding doubles
我正在做一个 Direct3D 项目,在我的代码中有一个函数将双精度作为参数,这个双精度代表自上一帧以来的时间,我的想法是将这个时间与另一个双精度相加,它代表立方体的旋转角度,然后将新值存储为新角度,但在调试后我注意到角度(在代码中称为 rot)从第一帧的零跳到 ~79800 之类的值,每次都略有不同,但总是79作为第一个数字,我真的不明白问题是什么。
这里是运行每一帧:
double rot = 0.0f;
double seconds = 0.0;
unsigned int frames = 0;
// this is the function used to render a single frame
void RenderFrame(double time)
{
// clear the back buffer to a deep blue
devcon->ClearRenderTargetView(renderTarget, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
devcon->ClearDepthStencilView(stencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
//PROBLEM HERE
//////
rot += time;
//////
VSCBdata.transformation = XMMatrixTranspose(XMMatrixRotationX(rot) * XMMatrixRotationZ(rot) * XMMatrixTranslation(0.0f, 0.0f, 4.0f) * XMMatrixPerspectiveLH(1.0f, 1.0f, 0.5f, 10.0f));
devcon->UpdateSubresource(VSConstantBuffer, 0, NULL, &VSCBdata, 0, 0);
// draw the vertex buffer to the back buffer
devcon->DrawIndexed(ARRAYSIZE(indices), 0, 0);
VSCBdata.transformation = XMMatrixTranspose(XMMatrixRotationY(rot) * XMMatrixRotationZ(rot) * XMMatrixTranslation(0.0f, 2.0f, 6.0f) * XMMatrixPerspectiveLH(1.0f, 1.0f, 0.5f, 10.0f));
devcon->UpdateSubresource(VSConstantBuffer, 0, NULL, &VSCBdata, 0, 0);
// draw the vertex buffer to the back buffer
devcon->DrawIndexed(ARRAYSIZE(indices), 0, 0);
// do 3D rendering on the back buffer here
// switch the back buffer and the front buffer
swapchain->Present(0, 0);
}
这里是计算时间的代码:
double frequency = 0.0;
__int64 start = 0;
__int64 lastFrame = 0;
void StartTimer()
{
LARGE_INTEGER shish;
QueryPerformanceFrequency(&shish);
frequency = double(shish.QuadPart);
QueryPerformanceCounter(&shish);
start = shish.QuadPart;
}
double GetTime()
{
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
return (time.QuadPart - start) / frequency;
}
double GetFrameTime()
{
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
__int64 result = time.QuadPart - lastFrame;
lastFrame = time.QuadPart;
if (result < 0.0) result = 0.0;
return double(result)/frequency;
}
由于 lastFrame
被初始化为 0
,第一次调用 GetFrameTime
将 return QueryPerformanceCounter
的值(以秒为单位)。
您应该在程序开始时调用 GetFrameTime
一次并丢弃该值或在 StartTimer
.
中初始化 lastFrame
我正在做一个 Direct3D 项目,在我的代码中有一个函数将双精度作为参数,这个双精度代表自上一帧以来的时间,我的想法是将这个时间与另一个双精度相加,它代表立方体的旋转角度,然后将新值存储为新角度,但在调试后我注意到角度(在代码中称为 rot)从第一帧的零跳到 ~79800 之类的值,每次都略有不同,但总是79作为第一个数字,我真的不明白问题是什么。
这里是运行每一帧:
double rot = 0.0f;
double seconds = 0.0;
unsigned int frames = 0;
// this is the function used to render a single frame
void RenderFrame(double time)
{
// clear the back buffer to a deep blue
devcon->ClearRenderTargetView(renderTarget, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
devcon->ClearDepthStencilView(stencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
//PROBLEM HERE
//////
rot += time;
//////
VSCBdata.transformation = XMMatrixTranspose(XMMatrixRotationX(rot) * XMMatrixRotationZ(rot) * XMMatrixTranslation(0.0f, 0.0f, 4.0f) * XMMatrixPerspectiveLH(1.0f, 1.0f, 0.5f, 10.0f));
devcon->UpdateSubresource(VSConstantBuffer, 0, NULL, &VSCBdata, 0, 0);
// draw the vertex buffer to the back buffer
devcon->DrawIndexed(ARRAYSIZE(indices), 0, 0);
VSCBdata.transformation = XMMatrixTranspose(XMMatrixRotationY(rot) * XMMatrixRotationZ(rot) * XMMatrixTranslation(0.0f, 2.0f, 6.0f) * XMMatrixPerspectiveLH(1.0f, 1.0f, 0.5f, 10.0f));
devcon->UpdateSubresource(VSConstantBuffer, 0, NULL, &VSCBdata, 0, 0);
// draw the vertex buffer to the back buffer
devcon->DrawIndexed(ARRAYSIZE(indices), 0, 0);
// do 3D rendering on the back buffer here
// switch the back buffer and the front buffer
swapchain->Present(0, 0);
}
这里是计算时间的代码:
double frequency = 0.0;
__int64 start = 0;
__int64 lastFrame = 0;
void StartTimer()
{
LARGE_INTEGER shish;
QueryPerformanceFrequency(&shish);
frequency = double(shish.QuadPart);
QueryPerformanceCounter(&shish);
start = shish.QuadPart;
}
double GetTime()
{
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
return (time.QuadPart - start) / frequency;
}
double GetFrameTime()
{
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
__int64 result = time.QuadPart - lastFrame;
lastFrame = time.QuadPart;
if (result < 0.0) result = 0.0;
return double(result)/frequency;
}
由于 lastFrame
被初始化为 0
,第一次调用 GetFrameTime
将 return QueryPerformanceCounter
的值(以秒为单位)。
您应该在程序开始时调用 GetFrameTime
一次并丢弃该值或在 StartTimer
.
lastFrame