|
| 1 | +#include <graphics/3d/camera_state.h> |
| 2 | +#include <iostream> |
| 3 | +CameraState camera_state; |
| 4 | + |
| 5 | +void update_camera_vectors(CameraState& state) { |
| 6 | + glm::vec3 new_front; |
| 7 | + new_front.x = cos(glm::radians(state.yaw)) * cos(glm::radians(state.pitch)); |
| 8 | + new_front.y = sin(glm::radians(state.pitch)); |
| 9 | + new_front.z = sin(glm::radians(state.yaw)) * cos(glm::radians(state.pitch)); |
| 10 | + state.front = glm::normalize(new_front); |
| 11 | + state.right = glm::normalize(glm::cross(state.front, state.world_up)); |
| 12 | + state.up = glm::normalize(glm::cross(state.right, state.front)); |
| 13 | +} |
| 14 | + |
| 15 | +glm::mat4 get_view_matrix(const CameraState& state) { |
| 16 | + return glm::lookAt(state.position, state.position + state.front, state.up); |
| 17 | +} |
| 18 | + |
| 19 | +void process_keyboard(CameraState& state, int direction, float delta_time) { |
| 20 | + float velocity = state.movement_speed * delta_time; |
| 21 | + if (direction == 0) state.position += state.front * velocity; |
| 22 | + if (direction == 1) state.position -= state.front * velocity; |
| 23 | + if (direction == 2) state.position -= state.right * velocity; |
| 24 | + if (direction == 3) state.position += state.right * velocity; |
| 25 | + if (direction == 4) state.position += state.world_up * velocity; |
| 26 | + if (direction == 5) state.position -= state.world_up * velocity; |
| 27 | +} |
| 28 | + |
| 29 | +void process_mouse_movement(CameraState& state, float x_offset, float y_offset, bool constrain_pitch) { |
| 30 | + x_offset *= state.mouse_sensitivity; |
| 31 | + y_offset *= state.mouse_sensitivity; |
| 32 | + state.yaw += x_offset; |
| 33 | + state.pitch += y_offset; |
| 34 | + |
| 35 | + if (constrain_pitch) { |
| 36 | + if (state.pitch > 89.0f) state.pitch = 89.0f; |
| 37 | + if (state.pitch < -89.0f) state.pitch = -89.0f; |
| 38 | + } |
| 39 | + update_camera_vectors(state); |
| 40 | +} |
| 41 | + |
| 42 | +void process_mouse_scroll(CameraState& state, float y_offset) { |
| 43 | + state.zoom -= (float)y_offset; |
| 44 | + if (state.zoom < 1.0f) state.zoom = 1.0f; |
| 45 | + if (state.zoom > 45.0f) state.zoom = 45.0f; |
| 46 | +} |
| 47 | + |
| 48 | +void handle_camera_input(GLFWwindow* window, const float delta_time) { |
| 49 | + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) |
| 50 | + process_keyboard(camera_state, 0, delta_time); |
| 51 | + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) |
| 52 | + process_keyboard(camera_state, 1, delta_time); |
| 53 | + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) |
| 54 | + process_keyboard(camera_state, 2, delta_time); |
| 55 | + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) |
| 56 | + process_keyboard(camera_state, 3, delta_time); |
| 57 | + if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(window,GLFW_KEY_SPACE) == GLFW_PRESS) |
| 58 | + process_keyboard(camera_state, 4, delta_time); |
| 59 | + |
| 60 | + if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) |
| 61 | + process_keyboard(camera_state, 5, delta_time); |
| 62 | +} |
0 commit comments