~ | Better camera system

This commit is contained in:
TheRedShip
2024-12-27 11:32:35 +01:00
parent ea3dd738f8
commit ebb8ca36bd
6 changed files with 78 additions and 43 deletions

View File

@ -22,23 +22,32 @@ class Camera
Camera(glm::vec3 startPos, glm::vec3 startUp, float startYaw, float startPitch); Camera(glm::vec3 startPos, glm::vec3 startUp, float startYaw, float startPitch);
~Camera(void); ~Camera(void);
void update_camera_vectors();
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);
glm::mat4 get_view_matrix(); glm::mat4 get_view_matrix();
glm::vec3 get_position(); glm::vec3 get_position();
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: private:
void update_camera_vectors();
glm::vec3 _forward;
glm::vec3 _position; glm::vec3 _position;
glm::vec3 _forward;
glm::vec3 _up; glm::vec3 _up;
glm::vec3 _right; glm::vec3 _right;
float _pitch; float _pitch;
float _yaw; 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 #endif

View File

@ -30,6 +30,8 @@ class Window
void pollEvents(); void pollEvents();
bool shouldClose(); bool shouldClose();
void process_input();
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
static void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos); static void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos);
static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods); static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods);

View File

@ -13,7 +13,8 @@
#include "Camera.hpp" #include "Camera.hpp"
Camera::Camera(glm::vec3 start_pos, glm::vec3 start_up, float start_yaw, float start_pitch) 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(); update_camera_vectors();
} }
@ -35,20 +36,32 @@ void Camera::update_camera_vectors()
_up = glm::normalize(glm::cross(_right, _forward)); _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) void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch = true)
{ {
_yaw += xoffset * 0.2f; _yaw += xoffset * _sensitivity;
_pitch += yoffset * 0.2f; _pitch += yoffset * _sensitivity;
if (constraint_pitch) 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) 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 (forward) _position += _forward * speed; if (glm::length(acceleration) > 0.1f)
if (backward) _position -= _forward * speed; acceleration = glm::normalize(acceleration) * _acceleration_rate;
if (left) _position -= _right * speed;
if (right) _position += _right * speed; _acceleration = acceleration;
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);
} }

View File

@ -90,15 +90,6 @@ void Scene::updateGPUData()
const std::vector<GPUObject>& Scene::getGPUData() const const std::vector<GPUObject>& 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); return (_gpu_objects);
} }

View File

@ -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<Window*>(glfwGetWindowUserPointer(window));
(void) win; (void) key; (void) scancode; (void) mods; (void) action;
bool forward = glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS; bool forward = glfwGetKey(_window, GLFW_KEY_W) == GLFW_PRESS;
bool backward = glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS; bool backward = glfwGetKey(_window, GLFW_KEY_S) == GLFW_PRESS;
bool left = glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS; bool left = glfwGetKey(_window, GLFW_KEY_A) == GLFW_PRESS;
bool right = glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS; bool right = glfwGetKey(_window, GLFW_KEY_D) == GLFW_PRESS;
bool up = glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS; bool up = glfwGetKey(_window, GLFW_KEY_SPACE) == GLFW_PRESS;
bool down = glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == 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) void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos)
{ {
Window* win = static_cast<Window*>(glfwGetWindowUserPointer(window)); Window* win = static_cast<Window*>(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<Window*>(glfwGetWindowUserPointer(window));
(void) win; (void) key; (void) scancode; (void) action; (void) mods;
}
void Window::display() void Window::display()
{ {
@ -118,6 +122,9 @@ void Window::display()
} }
void Window::pollEvents() void Window::pollEvents()
{ {
this->process_input();
_scene->getCamera()->update(1.0f / _fps);
glfwPollEvents(); glfwPollEvents();
} }
bool Window::shouldClose() bool Window::shouldClose()