Files
RT_GPU/srcs/Camera.cpp
2024-12-21 21:17:35 +01:00

78 lines
2.5 KiB
C++

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Camera.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 14:00:38 by TheRed #+# #+# */
/* Updated: 2024/10/15 14:00:38 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#include "Camera.hpp"
Camera::Camera(glm::vec3 start_pos, glm::vec3 start_up, float start_yaw, float start_pitch)
: _forward(glm::vec3(0.0f, 0.0f, -1.0f)), _yaw(start_yaw), _pitch(start_pitch), _position(start_pos), _up(start_up)
{
update_camera_vectors();
}
Camera::~Camera(void)
{
}
void Camera::update_camera_vectors()
{
glm::vec3 frontTemp;
frontTemp.x = cos(glm::radians(_yaw)) * cos(glm::radians(_pitch));
frontTemp.y = sin(glm::radians(_pitch));
frontTemp.z = sin(glm::radians(_yaw)) * cos(glm::radians(_pitch));
_forward = glm::normalize(frontTemp);
_right = glm::normalize(glm::cross(_forward, _up));
_up = glm::normalize(glm::cross(_right, _forward));
std::cout << _right.x << " " << _right.y << " " << _right.z << std::endl;
}
glm::mat4 Camera::get_view_matrix()
{
return (glm::lookAt(_position, _position + _forward, _up));
}
glm::vec3 Camera::get_position()
{
return (_position);
}
void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch = true)
{
_yaw += xoffset * 0.2f;
_pitch += yoffset * 0.2f;
if (constraint_pitch)
{
if (_pitch > 89.0f) _pitch = 89.0f;
if (_pitch < -89.0f) _pitch = -89.0f;
}
update_camera_vectors();
std::cout << _yaw << " " << _pitch << std::endl;
}
void Camera::process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down)
{
float speed;
speed = 1.0f;
if (forward) _position += _forward * speed;
if (backward) _position -= _forward * speed;
if (left) _position -= _right * speed;
if (right) _position += _right * speed;
if (up) _position += up * speed;
if (down) _position -= up * speed;
}