#include "camera.h" #include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/type_ptr.hpp> void Camera_::lockMouse(bool lock) { m_lock = lock; if (lock) { // Hide Mouse and record relative movement SDL_SetRelativeMouseMode(SDL_TRUE); // Flush the offset since we showed the mouse int xOffset{0}, yOffset{0}; SDL_GetRelativeMouseState(&xOffset, &yOffset); } else { // Show Mouse SDL_SetRelativeMouseMode(SDL_FALSE); } } bool Camera_::isLocked() const { return m_lock; } void Camera_::setPerspective(float fovY, float aspect, float zNear, float zFar) { /*glm::mat4*/ perspectiveMatrix = glm::perspective(fovY, aspect, zNear, zFar); glMatrixMode(GL_PROJECTION); glLoadMatrixf(glm::value_ptr(perspectiveMatrix)); glMatrixMode(GL_MODELVIEW); } void Camera_::update(float deltaTime) { int xOffset{0}, yOffset{0}; SDL_GetRelativeMouseState(&xOffset, &yOffset); float prevCamY = position.y; if (move.forward) { position += (speed * deltaTime) * front; } if (move.backward) { position -= (speed * deltaTime) * front; } if (move.right) { position += glm::normalize(glm::cross(front, up)) * (speed * deltaTime); } if (move.left) { position -= glm::normalize(glm::cross(front, up)) * (speed * deltaTime); } if (!move.freeCam) { position.y = prevCamY; } // only update if locked if (m_lock) { // Limit yaw values to 360 yaw = glm::mod(yaw + (float)xOffset * sensitivity, 360.0f); pitch += (float)yOffset * sensitivity; if (pitch > 89.0f) pitch = 89.0f; if (pitch < -89.0f) pitch = -89.0f; } direction.x = glm::sin(glm::radians(yaw)) * glm::cos(glm::radians(pitch)); direction.y = -glm::sin(glm::radians(pitch)); direction.z = -glm::cos(glm::radians(yaw)) * glm::cos(glm::radians(pitch)); front = glm::normalize(direction); // glm::mat4 viewMatrix(1.0f); viewMatrix = glm::mat4(1.0f); viewMatrix = glm::lookAt(position, position + front, up); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(glm::value_ptr(viewMatrix)); }