【发布时间】:2019-10-07 06:08:39
【问题描述】:
我无法理解翻译相机。我已经可以成功旋转相机,但我仍然对平移相机感到困惑。我包含有关如何旋转相机的代码,因为平移和旋转需要使用lookat功能。作业说平移相机意味着眼睛和中心都应该移动相同的量。我知道我可以更改查看函数中的参数来实现这一点。
lookat函数定义如下:
Lookat(cameraPos, center, up)
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 10.0f);
glm::vec3 center(0.0f, 0.0f, 0.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
modelViewProjectionMatrix.Perspective(glm::radians(fov), float(width) / float(height), 0.1f, 100.0f);
modelViewProjectionMatrix.LookAt(cameraPos, center, cameraUp);
void CursorPositionCallback(GLFWwindow* lWindow, double xpos, double ypos)
{
int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
if (state == GLFW_PRESS)
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY- ypos;
lastX = xpos;
lastY = ypos;
yaw += xoffset;
pitch += yoffset;
glm::vec3 front;
front.x = center[0] + 5.0f*cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = center[1] + 5.0f*sin(glm::radians(pitch));
front.z = center[1] + 5.0f*sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraPos = front;
}
}
【问题讨论】:
-
您的代码似乎已经将
cameraPos重新定位在围绕中心的球体上,因此将您的位移添加到center还不够吗? -
感谢您的建议!我现在明白了。