#ifndef CAMERA_H #define CAMERA_H #include "pch.h" #include "glm/glm.hpp" #include "glm/gtc/matrix_transform.hpp" enum class CAMERA_MOVEMENT { FORWARD, BACKWARD, LEFT, RIGHT, UP, DOWN }; const float YAW = -90.f; const float PITCH = 0.f; const float SPEED = 2.5f; const float SENSITIVITY = 0.1f; const float ZOOM = 45.0f; class CameraService { public: glm::vec3 Position; glm::vec3 Front; glm::vec3 Up; glm::vec3 Right; glm::vec3 WorldUp; float Yaw; float Pitch; float MovementSpeed; float MouseSensitivity; float Zoom; CameraService(glm::vec3 position = glm::vec3{0.f, 0.f, 0.f}, glm::vec3 up = glm::vec3{0.f, 1.f, 0.f}, float yaw = YAW, float pitch = PITCH) : Front(glm::vec3{0.f, 0.f, -1.f}), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM) { Position = position; WorldUp = up; Yaw = yaw; Pitch = pitch; UpdateCameraVectors(); } CameraService(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw = YAW, float pitch = PITCH) : Front(glm::vec3{0.f, 0.f, -1.f}), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM) { Position = glm::vec3{posX, posY, posZ}; WorldUp = glm::vec3{upX, upY, upZ}; Yaw = yaw; Pitch = pitch; UpdateCameraVectors(); } glm::mat4 GetViewMartix() { return this->LookAt(Position, Position + Front, Up); } void ProcessKeyboard(CAMERA_MOVEMENT direction, float deltaTime) { float velocity = MovementSpeed * deltaTime; switch (direction) { case CAMERA_MOVEMENT::FORWARD: Position += Front * velocity; break; case CAMERA_MOVEMENT::BACKWARD: Position -= Front * velocity; break; case CAMERA_MOVEMENT::LEFT: Position -= Right * velocity; break; case CAMERA_MOVEMENT::RIGHT: Position += Right * velocity; break; case CAMERA_MOVEMENT::UP: break; Position += Up * velocity; case CAMERA_MOVEMENT::DOWN: break; Position -= Up * velocity; default: break; } Position.y = 0.f; } void ProcessMouseMovement(float xOffset, float yOffset, GLboolean constrainPitch = true) { xOffset *= MouseSensitivity; yOffset *= MouseSensitivity; Yaw += xOffset; Pitch += yOffset; if (constrainPitch) { if (Pitch > 89.f) { Pitch = 89.f; } if (Pitch < -89.f) { Pitch = -89.f; } } UpdateCameraVectors(); } void ProcessMouseScroll(float yOffset) { Zoom -= yOffset; if (Zoom > 45.f) { Zoom = 45.f; } if (Zoom < 1.f) { Zoom = 1.f; } } private: void UpdateCameraVectors(){ 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); Right = glm::normalize(glm::cross(Front, WorldUp)); Up = glm::normalize(glm::cross(Right, Front)); } glm::mat4 LookAt(glm::vec3 position, glm::vec3 target, glm::vec3 worldUp){ glm::vec3 direction = position - target; glm::vec3 right = glm::normalize(glm::cross(glm::normalize(worldUp), direction)); glm::vec3 up = glm::normalize(glm::cross(direction, right)); glm::mat4 trans = glm::mat4{1.f}; trans[3][0] = -position.x; trans[3][1] = -position.y; trans[3][2] = -position.z; glm::mat4 rotation = glm::mat4{1.f}; rotation[0][0] = right.x; rotation[1][0] = right.y; rotation[2][0] = right.z; rotation[0][1] = up.x; rotation[1][1] = up.y; rotation[2][1] = up.z; rotation[0][2] = direction.x; rotation[1][2] = direction.y; rotation[2][2] = direction.z; return rotation * trans; } }; #endif // !CAMERA_H