fix: camera cannot pan left or right

This commit is contained in:
2026-03-21 13:07:28 +08:00
parent 772d606bbe
commit e2f0e7ea66
6 changed files with 24 additions and 5 deletions

View File

@@ -14,6 +14,7 @@ public:
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos); static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos);
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
static void window_focus_callback(GLFWwindow* window, int focused);
static void window_reshape_callback(GLFWwindow* window, int new_width, int new_height); static void window_reshape_callback(GLFWwindow* window, int new_width, int new_height);
static int start_cubed_application(int argc, char** argv); static int start_cubed_application(int argc, char** argv);

View File

@@ -25,7 +25,7 @@ public:
void update_move_camera(); void update_move_camera();
void camera_init(Player* player); void camera_init(Player* player);
void reset_camera();
void update_cursor_position_camera(double xpos, double ypos); void update_cursor_position_camera(double xpos, double ypos);
const glm::mat4 get_camera_lookat() const; const glm::mat4 get_camera_lookat() const;

View File

@@ -14,8 +14,8 @@ class Player {
private: private:
constexpr static float ACCELERATION = 30.0f; constexpr static float ACCELERATION = 30.0f;
constexpr static float DECELERATION = 50.0f; constexpr static float DECELERATION = 50.0f;
float m_yaw; float m_yaw = 0.0f;
float m_pitch; float m_pitch = 0.0f;
float m_sensitivity = 0.05f; float m_sensitivity = 0.05f;

View File

@@ -29,10 +29,12 @@ void App::init() {
m_window.init(); m_window.init();
glfwSetWindowUserPointer(m_window.get_glfw_window(), this); glfwSetWindowUserPointer(m_window.get_glfw_window(), this);
glfwSetWindowSizeCallback(m_window.get_glfw_window(), window_reshape_callback);
glfwSetKeyCallback(m_window.get_glfw_window(), key_callback);
glfwSetCursorPosCallback(m_window.get_glfw_window(), cursor_position_callback); glfwSetCursorPosCallback(m_window.get_glfw_window(), cursor_position_callback);
glfwSetMouseButtonCallback(m_window.get_glfw_window(), mouse_button_callback); glfwSetMouseButtonCallback(m_window.get_glfw_window(), mouse_button_callback);
glfwSetWindowFocusCallback(m_window.get_glfw_window(), window_focus_callback);
glfwSetWindowSizeCallback(m_window.get_glfw_window(), window_reshape_callback);
glfwSetKeyCallback(m_window.get_glfw_window(), key_callback);
m_renderer.init(); m_renderer.init();
m_window.update_viewport(); m_window.update_viewport();
@@ -88,6 +90,15 @@ void App::mouse_button_callback(GLFWwindow* window, int button, int action, int
} }
} }
void App::window_focus_callback(GLFWwindow* window, int focused) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr");
if (focused) {
app->m_camera.reset_camera();
}
}
void App::window_reshape_callback(GLFWwindow* window, int new_width, int new_height) { void App::window_reshape_callback(GLFWwindow* window, int new_width, int new_height) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window)); App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr"); CUBED_ASSERT_MSG(app, "nullptr");

View File

@@ -17,6 +17,11 @@ void Camera::update_move_camera() {
void Camera::camera_init(Player* player) { void Camera::camera_init(Player* player) {
m_player = player; m_player = player;
update_move_camera(); update_move_camera();
reset_camera();
}
void Camera::reset_camera() {
m_firse_mouse = true;
} }
void Camera::update_cursor_position_camera(double xpos, double ypos) { void Camera::update_cursor_position_camera(double xpos, double ypos) {
@@ -24,6 +29,7 @@ void Camera::update_cursor_position_camera(double xpos, double ypos) {
m_last_mouse_x = xpos; m_last_mouse_x = xpos;
m_last_mouse_y = ypos; m_last_mouse_y = ypos;
m_firse_mouse = false; m_firse_mouse = false;
return;
} }
float offset_x = xpos - m_last_mouse_x; float offset_x = xpos - m_last_mouse_x;

View File

@@ -280,6 +280,7 @@ void Player::update_front_vec(float offset_x, float offset_y) {
m_yaw += offset_x * m_sensitivity; m_yaw += offset_x * m_sensitivity;
m_pitch += offset_y * m_sensitivity; m_pitch += offset_y * m_sensitivity;
m_yaw = std::fmod(m_yaw, 360.0);
if (m_pitch > 89.0f) m_pitch = 89.0f; if (m_pitch > 89.0f) m_pitch = 89.0f;
if (m_pitch < -89.0f) m_pitch = -89.0f; if (m_pitch < -89.0f) m_pitch = -89.0f;