Opengl Free cam(来自 Learnopengl)到 3rd cam
Posted
技术标签:
【中文标题】Opengl Free cam(来自 Learnopengl)到 3rd cam【英文标题】:Opengl Free cam (from Learnopengl) to 3rd cam 【发布时间】: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)
有没有人经历过这个,谁能帮助我? 谢谢
【问题讨论】:
【参考方案1】:这是我用来创建第三人称相机的代码:
float pitch = -Pitch;
// I use a 90.0f offset
// but you can play around with that value to suit your needs
float yaw = Yaw - 90.0f;
// constrain pitch so you don't look from below ground level
if (pitch < 0.0)
pitch = 0.0;
// algorithm from ThinMatrix video on third person cameras
float distance = 20.0f;
float x = distance * cos(glm::radians(pitch)) * sin(radians(-yaw));
float y = distance * sin(glm::radians(pitch));
float z = distance * cos(glm::radians(pitch)) * cos(glm::radians(yaw));
glm::vec3 tpCamPos = playerPosition + vec3(-x, y, -z);
【讨论】:
以上是关于Opengl Free cam(来自 Learnopengl)到 3rd cam的主要内容,如果未能解决你的问题,请参考以下文章