我是 d3d 开发的初学者,我被困在我的第一个问题上。我创建了一个小程序,该程序在每个顶点中绘制具有随机高度的地形和从左到右移动的灯光(加上基本的键盘管理)。问题是动画灯非常跳跃,输入识别经常失败,cpu 使用率是 50% 恒定(我有一个双核 cpu)。我认为这个问题是由不正确的节流引起的,但即使在修复代码之后我仍然有问题。我的主循环是
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
HWND wndHandle;
int width = 640;
int height = 480;
wndHandle = InitWindow(hInstance, width, height);
InitDirect3D(wndHandle, width, height);
DInputInit(hInstance, wndHandle);
SceneSetUp();
MSG msg = {0};
QueryPerformanceFrequency((LARGE_INTEGER *) &perf_cnt);
time_count = perf_cnt / fixedtic;
QueryPerformanceCounter((LARGE_INTEGER *) &next_time);
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
continue;
}
HandleInput();
QueryPerformanceCounter((LARGE_INTEGER *) &cur_time);
if (cur_time>next_time) {
Render();
next_time += time_count;
if (next_time < cur_time)
next_time = cur_time + time_count;
}
}
ShutdownDirect3D();
return (int)msg.wParam;
}
而渲染功能是
void Render()
{
QueryPerformanceCounter((LARGE_INTEGER *) &curtime);
timespan = (curtime - last_time) * time_factor;
last_time = curtime;
model.pD3DDevice->ClearRenderTargetView(model.pRenderTargetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f));
lightpos.x = (float)cos(ang) * 256.0f;
ang += timespan * 0.5;
lpvar->SetFloatVector((float *) lightpos);
D3DXMatrixLookAtLH(&V, &model.campos, new D3DXVECTOR3(0.0f, 0.0f, 0.0f), new D3DXVECTOR3(0.0f, 1.0f, 0.0f));
D3DXMATRIX VP = V*P;
camvar->SetFloatVector((float *)model.campos);
ViewProjection->SetMatrix((float *) &VP);
for(UINT p=0; p < techniqueDescription.Passes; ++p)
{
pTechnique->GetPassByIndex(p)->Apply(0);
model.pD3DDevice->DrawIndexed(numIndices, 0, 0);
}
model.pSwapChain->Present(0, 0);
}
任何帮助表示赞赏