OpenGL에서 일부 항목을 렌더링하는 데 GLFW를 사용하고 있습니다. 나는 카메라를 가지고 움직이고 주변을 둘러 볼 수있는 Camera 클래스를 만들었다. 이들은 단독으로 사용하면 잘 작동하지만 함께 사용하면 다음과 같은 일이 발생합니다.카메라 충돌로 이동 및 둘러보기
카메라를 앞으로 움직이면 회전이 느려지고 회전하면 카메라가 원래 위치로 재설정됩니다.
카메라를 뒤로, 왼쪽 또는 오른쪽으로 움직이면 회전으로 모든 것이 밖으로 나갑니다. 제가 지금 렌더링하고있는 것은 카메라 바로 앞에있는 두 개의 삼각형입니다. 그래서 카메라가 무엇을보고 끝나는 지 모르겠습니다. 또한 위에서와 같이 카메라 위치를 가끔 재설정하지만 처음부터 먼저 이동해야합니다.
코드는 비교적 간단하며 전체 소스는 here, mainly in the Camera, Entity, and main source and header files이지만 아래 관련 비트도 포함합니다.
void cursorMoveCallback(GLFWwindow * window, double xpos, double ypos)
{
printf("Mouse Moved to (%f, %f)\n", xpos, ypos);
// Change camera angle
glm::vec3 cameraMovement;
// Change in x (rotation about y axis)
if(xpos > 0)
cameraMovement.x = -0.0005;
else if (xpos < 0)
cameraMovement.x = 0.0005;
// Change in y (rotation about x axis)
if(ypos > 0)
cameraMovement.y = 0.0005;
else if(ypos < 0)
cameraMovement.y = -0.0005;
// Reset Cursor position
glfwSetCursorPos(window, 0, 0);
// Change Position of Camera
cameraMovement += Camera::getCurrentCamera()->getDirection();
Camera::getCurrentCamera()->setDirection(cameraMovement);
}
:
void keyCallback(GLFWwindow * window, int key, int scancode, int action, int mods)
{
switch(key)
{
case GLFW_KEY_W:
cam->move(glm::vec3(0.0, 0.0, -0.05));
break;
case GLFW_KEY_S:
cam->move(glm::vec3(0.0, 0.0, 0.05));
break;
case GLFW_KEY_A:
cam->move(glm::vec3(-0.05, 0.0, 0.0));
break;
case GLFW_KEY_D:
cam->move(glm::vec3(0.05, 0.0, 0.0));
break;
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
default:
break;
}
}
이 마우스의 움직임을 다루는 카메라로 주변을 둘러 보는 기능입니다 :
이
내가 운동 GLFW의 주요 콜백과 거래를하고 있어요 기능입니다이동 카메라 :
glm::vec3 Camera::move(glm::vec3 translation)
{
int dims = viewMatrix.length() - 1;
// Apply translation
for (int i = 0; i < dims; i++)
viewMatrix[dims][i] -= translation[i];
// Update viewMatrix
Program::updateViewMatrix(viewMatrix);
return Entity::move(translation);
}
glm::vec3 Entity::move(glm::vec3 translation)
{
// Move by amount
position += translation;
// Return new position
return position;
}
카메라로 상대 :
glm::vec3 Camera::setDirection(glm::vec3 newDirection)
{
viewMatrix = glm::lookAt(newDirection, position, glm::vec3(0.0, 1.0, 0.0));
Program::updateViewMatrix(viewMatrix);
return Entity::setDirection(newDirection);
}
glm::vec3 Entity::setDirection(glm::vec3 newDirection)
{
// Set direction of entity
direction = newDirection;
// Return new direction of entity
return direction;
}
호출은 모든 활성 OpenGL 프로그램을 거쳐 뷰 매트릭스의 유니폼을 설정합니다. 차이를 만들 수있는 다른 관련 코드가있을 수 있다면 알려 주시기 바랍니다.
내 추측으로는 콜백으로 인해 스레딩 문제가 발생하거나 동일한 시간에 이동하고 둘러 볼 때 문제가있는 것 같습니다.
업데이트 1 : 나는 인자의 순서를 glm::lookAt()
으로 정했습니다. 이것은 사라지는 문제를 해결하지만, 아무 것도 움직이면 회전이 느려지는 것처럼 보입니다.
glm :: lookAt는 첫 번째 매개 변수로 방향을 지정하지 않습니다. 이 [질문] (http://stackoverflow.com/questions/21830340/understanding-glmlookat)을 확인하십시오. – BDL
@BDL 대부분의 문제를 해결하는 것 같았습니다. 나는 코드에서 도처에 잘못된 순서를 가졌으므로 한 번에 두 가지를 모두 변경하려고 시도 할 때까지 작동했다. 삼각형은 더 이상 사라지지 않지만 움직이면서 주변을 둘러 보는 것이 느린 것 같습니다. 그 원인이 무엇인지에 대한 아이디어가 있습니까? – danielunderwood
주문 만 변경하면 충분하지 않습니다. lookat는 공간의 두 위치, 카메라의 위치 및보고자하는 위치를 제공해야합니다. 나를 위해 당신의 코드는 당신이보고 싶은 방향을 제공하는 것처럼 보입니다. – BDL