// // Created by atdunbg on 2026/9/22. // #include #include #include #include "GLFW/glfw3.h" namespace Engine { void Camera::Update(Window& window, f32 deltaTime) { // 鼠标旋转(需显式启用,配合 cursor capture 使用) if (m_MouseLookEnabled) { f32 mouseDX = 0, mouseDY = 0; window.GetMouseDelta(mouseDX, mouseDY); if (mouseDX != 0 || mouseDY != 0) { m_Yaw += mouseDX * m_MouseSensitivity; m_Pitch -= mouseDY * m_MouseSensitivity; m_Pitch = glm::clamp(m_Pitch, -89.0f, 89.0f); } } UpdateBasis(); f32 speed = m_MoveSpeed * deltaTime; if (window.IsKeyPressed(GLFW_KEY_W)) m_Position += m_Forward * speed; if (window.IsKeyPressed(GLFW_KEY_S)) m_Position -= m_Forward * speed; if (window.IsKeyPressed(GLFW_KEY_A)) m_Position -= m_Right * speed; if (window.IsKeyPressed(GLFW_KEY_D)) m_Position += m_Right * speed; if (window.IsKeyPressed(GLFW_KEY_SPACE)) m_Position.y += speed; if (window.IsKeyPressed(GLFW_KEY_LEFT_SHIFT)) m_Position.y -= speed; } void Camera::UpdateBasis() { m_Forward.x = cosf(glm::radians(m_Yaw)) * cosf(glm::radians(m_Pitch)); m_Forward.y = sinf(glm::radians(m_Pitch)); m_Forward.z = sinf(glm::radians(m_Yaw)) * cosf(glm::radians(m_Pitch)); m_Forward = glm::normalize(m_Forward); m_Right = glm::normalize(glm::cross(m_Forward, glm::vec3(0, 1, 0))); m_Up = glm::normalize(glm::cross(m_Right, m_Forward)); } glm::mat4 Camera::GetView() const { return glm::lookAt(m_Position, m_Position + m_Forward, m_Up); } glm::mat4 Camera::GetProjection(f32 aspect, f32 nearZ, f32 farZ) const { auto proj = glm::perspective(glm::radians(45.0f), aspect, nearZ, farZ); proj[1][1] *= -1.0f; // Vulkan Y 翻转 // Vulkan clip 深度 [0,1]:z' = 0.5*z_clip + 0.5*w_clip。 // perspective 的 w_clip = -z_eye,偏移折算进 z 系数 //(注意:与 ortho 的公式不同,ortho 的 w=1 才能直接加到 [3][2]) proj[2][2] = proj[2][2] * 0.5f - 0.5f; proj[3][2] *= 0.5f; return proj; } } // namespace Engine