【发布时间】:2020-03-12 19:31:26
【问题描述】:
我正在学习opengl并关注learnopengl网站上的教程(https://learnopengl.com/)
有人可以帮我把这个来自learnopengl教程的相机从免费相机转换为第三人称吗?
我试过放物体(角色)的值,但是我不能让相机围绕玩家旋转。只有物体在镜头前走,如果我转向一边,物体会跟着转动,看起来像FPS相机,物体(角色)是武器。
行走代码(键盘):
void processKeyboard(Camera_Movement direction, float deltaTime)
{
frontY = Front.y;//para tirar o freeCamera
if (cameraStyle == FPS_CAMERA) {
Front.y = 0;
}
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
Front.y = frontY;
}
鼠标事件:
void processMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
Yaw += xoffset;
Pitch += yoffset;
// Make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
}
// Update Front, Right and Up Vectors using the updated Euler angles
updateCameraVectors();
}
更新值:
void updateCameraVectors()
{
// Calculate the new Front vector
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
// Also re-calculate the Right and Up vector
Right = glm::normalize(glm::cross(Front, WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
Up = glm::normalize(glm::cross(Right, Front));
}
并使用:
glm::vec3 playerPosition = glm::vec3(Position.x, terrainY, Position.z) + glm::vec3(1, -0.06f, 1)
有没有人经历过这个,谁能帮助我? 谢谢
【问题讨论】: