我在根据按下的键更新相机位置时遇到问题。简单地说,我试图让相机使用箭头键向左、向右、向前、向后移动。当程序运行时,相机从正确的位置开始,此外,当按下一个键时,程序会记录该键已被按下,但相机不会相应地更新。
如果有的话,我认为问题在于没有在循环的 else 语句中执行操作(我的旋转立方体所在的位置)。我最大的障碍是让按键的值进入循环,这样我就可以更新相机位置。关于下一步看哪里的任何想法或建议?
这是代码如下:
渲染.cpp
Render *renderDraw = new Render(renderWinWidth, renderWinHeight);
Camera *camera = new Camera();
void Render::renderScreen(Camera *cam)
{
...
gluLookAt(cam->getCameraX(), cam->getCameraY(), cam->getCameraZ(),
objPos[0], objPos[1], objPos[2], VECTOR_UP[0], VECTOR_UP[1], VECTOR_UP[2]);
...
glFlush();
}
相机.cpp
void Camera::setkeyPressed(WPARAM wParam, HWND hWnd, bool isPressed)
{
keyPressed[wParam] = isPressed;
switch(wParam)
{
case VK_UP:
//MessageBox(hWnd, L"key up", L"Key pressed", MB_OK | MB_ICONASTERISK);
newPos_ = getCameraX();
newPos_ -= 2.0f;
setCameraX(newPos_);
break;
case VK_DOWN:
//MessageBox(hWnd, L"key down", L"Key pressed", MB_OK | MB_ICONASTERISK);
newPos_ = getCameraX();
newPos_ += 2.0f;
setCameraX(newPos_);
break;
case VK_LEFT:
//MessageBox(hWnd, L"key left", L"Key pressed", MB_OK | MB_ICONASTERISK);
newPos_ = getCameraZ();
newPos_ += 2.0f;
setCameraZ(newPos_);
break;
case VK_RIGHT:
//MessageBox(hWnd, L"key right", L"Key pressed", MB_OK | MB_ICONASTERISK);
newPos_ = getCameraZ();
newPos_ -= 2.0f;
setCameraZ(newPos_);
break;
}
}
主文件
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
...
// Main Loop
while (isRunning)
{
if (PeekMessage (&msg, hWnd, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
isRunning = false;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
renderDraw->renderScreen(camera); <-Contains gyrating cube
SwapBuffers(*hDC);
}
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
....
switch (message)
{
case WM_KEYDOWN:
switch (wParam)
{
camera->setkeyPressed(wParam, hWndChild, true);
break;
}
break;
....
}
}