+ | Camera system

This commit is contained in:
TheRedShip
2024-12-21 20:45:56 +01:00
parent a006174ce5
commit 97fb7948f0
442 changed files with 67864 additions and 41 deletions

View File

@ -12,23 +12,46 @@
#include "Camera.hpp"
Camera::Camera(void)
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)
{
}
Camera::Camera(Camera const &src)
{
*this = src;
update_camera_vectors();
}
Camera::~Camera(void)
{
}
Camera &Camera::operator=(Camera const &rhs)
void Camera::update_camera_vectors()
{
if (this != &rhs)
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));
}
glm::mat4 Camera::get_view_matrix()
{
return (glm::lookAt(_position, _position + _forward, _up));
}
void Camera::process_movement(float xoffset, float yoffset, bool constraint_pitch = true)
{
_yaw += xoffset;
_pitch += yoffset;
if (constraint_pitch)
{
if (_pitch > 89.0f) _pitch = 89.0f;
if (_pitch < -89.0f) _pitch = -89.0f;
}
return (*this);
update_camera_vectors();
std::cout << _yaw << " " << _pitch << std::endl;
}

View File

@ -14,6 +14,8 @@
Window::Window(int width, int height, const char *title, int sleep)
{
camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f), -90.0f, 0.0f);
if (!glfwInit())
{
fprintf( stderr, "Failed to initialize GLFW\n" );
@ -42,18 +44,11 @@ Window::Window(int width, int height, const char *title, int sleep)
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(sleep);
}
Window::Window(Window const &src)
{
*this = src;
}
Window &Window::operator=(Window const &rhs)
{
if (this != &rhs)
_window = rhs._window;
return (*this);
}
Window::~Window(void)
{
delete camera;
glfwTerminate();
}
@ -69,13 +64,32 @@ void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action,
}
void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
{
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
(void) win; (void) xpos; (void) ypos;
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window));
(void) win; (void) xpos; (void) ypos;
win->_prevMousePos = win->_mousePos;
win->_mousePos = RT::Vec2i(xpos, ypos);
static double lastX = 0;
static double lastY = 0;
win->_mouseDelta = win->_mousePos - win->_prevMousePos;
if (lastX == 0 && lastY == 0)
{
lastX = xpos;
lastY = ypos;
}
double xoffset = xpos - lastX;
double yoffset = lastY - ypos;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
win->camera->process_movement(xoffset, yoffset, true);
glfwSetCursorPos(window, WIDTH / 2, HEIGHT / 2);
// scene.frameCount = 0;
}
lastX = xpos;
lastY = ypos;
}
void Window::mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
@ -112,10 +126,6 @@ GLFWwindow *Window::getWindow(void) const
{
return (_window);
}
RT::Vec2i Window::getMousePos(void) const
{
return (_mousePos);
}
float Window::getFps(void) const
{