coral_camera.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "coral_camera.h"
  2. #include <gtc/matrix_transform.hpp>
  3. #include <algorithm>
  4. #include "imgui.h"
  5. using namespace coral_3d;
  6. coral_camera::coral_camera(glm::vec3 position, float movement_speed, float mouse_sensitivity)
  7. : position_{ position }
  8. , movement_speed_{ movement_speed }
  9. , mouse_sensitivity_{ mouse_sensitivity }
  10. {
  11. }
  12. void coral_camera::set_orthographic_projection(float left, float right, float top, float bottom, float near, float far)
  13. {
  14. projection_matrix_ = glm::ortho(left, right, bottom, top, near, far);
  15. }
  16. void coral_camera::set_perspective_projection(float fovy, float aspect, float near, float far)
  17. {
  18. projection_matrix_ = glm::perspective(fovy, aspect, near, far);
  19. }
  20. void coral_camera::set_view_direction(glm::vec3 position, glm::vec3 direction, glm::vec3 up)
  21. {
  22. view_matrix_ = glm::lookAt(position, position + direction, up);
  23. }
  24. void coral_camera::update_input(GLFWwindow* pWindow, float dt)
  25. {
  26. if(ImGui::IsWindowFocused()) return;
  27. // Mouse input
  28. double mouse_x, mouse_y;
  29. glfwGetCursorPos(pWindow, &mouse_x, &mouse_y);
  30. float mouse_dx = static_cast<float>(mouse_x - last_mouse_x_) * mouse_sensitivity_;
  31. float mouse_dy = static_cast<float>(last_mouse_y_ - mouse_y) * mouse_sensitivity_;
  32. last_mouse_x_ = static_cast<float>(mouse_x);
  33. last_mouse_y_ = static_cast<float>(mouse_y);
  34. yaw_ -= mouse_dx;
  35. pitch_ += mouse_dy;
  36. pitch_ = std::clamp(pitch_, -89.f, 89.f);
  37. update_camera_vectors();
  38. set_view_direction(position_, forward_, up_);
  39. // Keyboard input
  40. const float velocity{ glfwGetKey(pWindow, keys_.sprint) == GLFW_PRESS ? sprint_movement_speed_ * dt : movement_speed_ * dt };
  41. if(glfwGetKey(pWindow, keys_.move_forward) == GLFW_PRESS) position_ += forward_ * velocity;
  42. if(glfwGetKey(pWindow, keys_.move_backward) == GLFW_PRESS) position_ -= forward_ * velocity;
  43. if(glfwGetKey(pWindow, keys_.move_left) == GLFW_PRESS) position_ -= right_ * velocity;
  44. if(glfwGetKey(pWindow, keys_.move_right) == GLFW_PRESS) position_ += right_ * velocity;
  45. if(glfwGetKey(pWindow, keys_.move_up) == GLFW_PRESS) position_ += world_up_ * velocity;
  46. if(glfwGetKey(pWindow, keys_.move_down) == GLFW_PRESS) position_ -= world_up_ * velocity;
  47. }
  48. void coral_camera::update_camera_vectors()
  49. {
  50. float yaw_rad{glm::radians(yaw_)};
  51. float pitch_rad{glm::radians(pitch_)};
  52. glm::vec3 forward
  53. {
  54. glm::cos(yaw_rad) * glm::cos(pitch_rad),
  55. glm::sin(pitch_rad),
  56. glm::sin(yaw_rad) * glm::cos(pitch_rad)
  57. };
  58. forward_ = glm::normalize(forward);
  59. right_ = glm::normalize(glm::cross(world_up_, forward_));
  60. up_ = glm::normalize(glm::cross(right_, forward_));
  61. }