From ebb8ca36bd9e709953aa39f10916e337b6a2c40e Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Fri, 27 Dec 2024 11:32:35 +0100 Subject: [PATCH] ~ | Better camera system --- includes/RT/Camera.hpp | 21 ++++++++++----- includes/RT/Window.hpp | 2 ++ srcs/RT.cpp | 2 +- srcs/class/Camera.cpp | 60 ++++++++++++++++++++++++++++++------------ srcs/class/Scene.cpp | 9 ------- srcs/class/Window.cpp | 27 ++++++++++++------- 6 files changed, 78 insertions(+), 43 deletions(-) diff --git a/includes/RT/Camera.hpp b/includes/RT/Camera.hpp index 880e11f..a6b4004 100644 --- a/includes/RT/Camera.hpp +++ b/includes/RT/Camera.hpp @@ -22,23 +22,32 @@ class Camera Camera(glm::vec3 startPos, glm::vec3 startUp, float startYaw, float startPitch); ~Camera(void); - void update_camera_vectors(); - - glm::mat4 get_view_matrix(); - glm::vec3 get_position(); + void update(float deltaTime); void process_mouse(float xoffset, float yoffset, bool constrainPitch); void process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down); - private: + glm::mat4 get_view_matrix(); + glm::vec3 get_position(); + + private: + void update_camera_vectors(); - glm::vec3 _forward; glm::vec3 _position; + glm::vec3 _forward; glm::vec3 _up; glm::vec3 _right; float _pitch; float _yaw; + + glm::vec3 _velocity; + glm::vec3 _acceleration; + + float _maxSpeed = 10.0f; + float _acceleration_rate = 40.0f; + float _deceleration_rate = 40.0f; + float _sensitivity = 0.2f; }; #endif \ No newline at end of file diff --git a/includes/RT/Window.hpp b/includes/RT/Window.hpp index 6178a5b..4b766cc 100644 --- a/includes/RT/Window.hpp +++ b/includes/RT/Window.hpp @@ -30,6 +30,8 @@ class Window void pollEvents(); bool shouldClose(); + void process_input(); + static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); static void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos); static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods); diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 7b8d542..9747532 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -72,7 +72,7 @@ int main(int argc, char **argv) std::cout << "\rFPS: " << int(window.getFps()) << " " << std::flush; window.display(); - window.pollEvents(); + window.pollEvents(); } return (0); diff --git a/srcs/class/Camera.cpp b/srcs/class/Camera.cpp index c16ca46..79c0a38 100644 --- a/srcs/class/Camera.cpp +++ b/srcs/class/Camera.cpp @@ -13,7 +13,8 @@ #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)), _position(start_pos), _up(start_up), _pitch(start_pitch), _yaw(start_yaw) + : _position(start_pos), _forward(glm::vec3(0.0f, 0.0f, -1.0f)), _up(start_up), _pitch(start_pitch), _yaw(start_yaw), + _velocity(0.0f), _acceleration(0.0f) { update_camera_vectors(); } @@ -35,20 +36,32 @@ void Camera::update_camera_vectors() _up = glm::normalize(glm::cross(_right, _forward)); } -glm::mat4 Camera::get_view_matrix() +void Camera::update(float delta_time) { - return (glm::lookAt(_position, _position + _forward, _up)); + _velocity += _acceleration * delta_time; + // std::cout << _acceleration.x << " " << _acceleration.y << " " << _acceleration.z << " " << std::endl; + + // Apply deceleration when no acceleration + if (glm::length(_acceleration) < 0.1f) + _velocity *= (1.0f - _deceleration_rate * delta_time); + + // Clamp velocity to maximum speed + float speed = glm::length(_velocity); + if (speed > _maxSpeed) + _velocity = (_velocity / speed) * _maxSpeed; + + // Update position + _position += _velocity * delta_time; + + // Reset acceleration + _acceleration = glm::vec3(0.0f); } -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; + _yaw += xoffset * _sensitivity; + _pitch += yoffset * _sensitivity; if (constraint_pitch) { @@ -61,14 +74,27 @@ void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch void Camera::process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down) { - float speed; + glm::vec3 acceleration(0.0f); - speed = .1f; + if (forward) acceleration += _forward; + if (backward) acceleration -= _forward; + if (right) acceleration += _right; + if (left) acceleration -= _right; + if (up) acceleration += _up; + if (down) acceleration -= _up; + + if (glm::length(acceleration) > 0.1f) + acceleration = glm::normalize(acceleration) * _acceleration_rate; + + _acceleration = acceleration; +} - 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; +glm::mat4 Camera::get_view_matrix() +{ + return (glm::lookAt(_position, _position + _forward, _up)); +} + +glm::vec3 Camera::get_position() +{ + return (_position); } \ No newline at end of file diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index bd003c6..0f5bdb6 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -90,15 +90,6 @@ void Scene::updateGPUData() const std::vector& Scene::getGPUData() const { - for (const auto& obj : _gpu_objects) - { - std::cout << "objType: " << obj.type << std::endl; - std::cout << "position: " << obj.position.x << " " << obj.position.y << " " << obj.position.z << std::endl; - std::cout << "color: " << obj.color.x << " " << obj.color.y << " " << obj.color.z << std::endl; - std::cout << "mat: " << obj.emission << " " << obj.roughness << " " << obj.specular << std::endl; - std::cout << "Size of GPUObject: " << sizeof(GPUObject) << " bytes" << std::endl; - } - return (_gpu_objects); } diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 7cf179c..a7f7ba0 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -53,20 +53,19 @@ Window::~Window(void) } -void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) +void Window::process_input() { - Window* win = static_cast(glfwGetWindowUserPointer(window)); - (void) win; (void) key; (void) scancode; (void) mods; (void) action; - bool forward = glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS; - bool backward = glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS; - bool left = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS; - bool right = glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS; - bool up = glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS; - bool down = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS; + bool forward = glfwGetKey(_window, GLFW_KEY_W) == GLFW_PRESS; + bool backward = glfwGetKey(_window, GLFW_KEY_S) == GLFW_PRESS; + bool left = glfwGetKey(_window, GLFW_KEY_A) == GLFW_PRESS; + bool right = glfwGetKey(_window, GLFW_KEY_D) == GLFW_PRESS; + bool up = glfwGetKey(_window, GLFW_KEY_SPACE) == GLFW_PRESS; + bool down = glfwGetKey(_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS; - win->_scene->getCamera()->process_keyboard(forward, backward, left, right, up, down); + _scene->getCamera()->process_keyboard(forward, backward, left, right, up, down); } + void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) { Window* win = static_cast(glfwGetWindowUserPointer(window)); @@ -104,6 +103,11 @@ void Window::mouseButtonCallback(GLFWwindow* window, int button, int action, int } } +void Window::keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) +{ + Window* win = static_cast(glfwGetWindowUserPointer(window)); + (void) win; (void) key; (void) scancode; (void) action; (void) mods; +} void Window::display() { @@ -118,6 +122,9 @@ void Window::display() } void Window::pollEvents() { + this->process_input(); + _scene->getCamera()->update(1.0f / _fps); + glfwPollEvents(); } bool Window::shouldClose()