From b9b7084a4f8b86d73092d582249fcae80a0ea6d9 Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Sun, 5 Jan 2025 14:50:29 +0100 Subject: [PATCH] ~ | Variable refactoring --- includes/RT/Camera.hpp | 12 +++++++----- includes/RT/SceneParser.hpp | 2 +- srcs/RT.cpp | 4 ++-- srcs/class/Camera.cpp | 19 ++++++++++++------- srcs/class/SceneParser.cpp | 12 ++++++++++++ srcs/class/Window.cpp | 4 ++-- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/includes/RT/Camera.hpp b/includes/RT/Camera.hpp index 7e17da5..3b242df 100644 --- a/includes/RT/Camera.hpp +++ b/includes/RT/Camera.hpp @@ -24,14 +24,16 @@ class Camera 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); + void processMouse(float xoffset, float yoffset, bool constrainPitch); + void processKeyboard(bool forward, bool backward, bool left, bool right, bool up, bool down); - glm::mat4 get_view_matrix(); - glm::vec3 get_position(); + glm::mat4 getViewMatrix(); + glm::vec3 getPosition(); + + void setPosition(glm::vec3 position); private: - void update_camera_vectors(); + void updateCameraVectors(); glm::vec3 _position; glm::vec3 _forward; diff --git a/includes/RT/SceneParser.hpp b/includes/RT/SceneParser.hpp index 792c698..1a52563 100644 --- a/includes/RT/SceneParser.hpp +++ b/includes/RT/SceneParser.hpp @@ -24,7 +24,7 @@ class SceneParser private: void parseMaterial(std::stringstream &line); - + void parseCamera(std::stringstream &line); Scene *_scene; diff --git a/srcs/RT.cpp b/srcs/RT.cpp index b209f10..7d66f7c 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -49,8 +49,8 @@ int main(int argc, char **argv) shader.set_int("u_objectsNum", gpu_data.size()); shader.set_float("u_time", (float)(glfwGetTime())); shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT)); - shader.set_vec3("u_cameraPosition", scene.getCamera()->get_position()); - shader.set_mat4("u_viewMatrix", scene.getCamera()->get_view_matrix()); + shader.set_vec3("u_cameraPosition", scene.getCamera()->getPosition()); + shader.set_mat4("u_viewMatrix", scene.getCamera()->getViewMatrix()); glDispatchCompute((WIDTH + 15) / 16, (HEIGHT + 15) / 16, 1); glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); diff --git a/srcs/class/Camera.cpp b/srcs/class/Camera.cpp index d5cec9c..665bd0d 100644 --- a/srcs/class/Camera.cpp +++ b/srcs/class/Camera.cpp @@ -16,14 +16,14 @@ Camera::Camera(glm::vec3 start_pos, glm::vec3 start_up, float start_yaw, float s : _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(); + updateCameraVectors(); } Camera::~Camera(void) { } -void Camera::update_camera_vectors() +void Camera::updateCameraVectors() { glm::vec3 frontTemp; @@ -54,7 +54,7 @@ void Camera::update(float delta_time) } -void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch = true) +void Camera::processMouse(float xoffset, float yoffset, bool constraint_pitch = true) { _yaw += xoffset * _sensitivity; _pitch += yoffset * _sensitivity; @@ -65,10 +65,10 @@ void Camera::process_mouse(float xoffset, float yoffset, bool constraint_pitch if (_pitch < -89.0f) _pitch = -89.0f; } - update_camera_vectors(); + updateCameraVectors(); } -void Camera::process_keyboard(bool forward, bool backward, bool left, bool right, bool up, bool down) +void Camera::processKeyboard(bool forward, bool backward, bool left, bool right, bool up, bool down) { glm::vec3 acceleration(0.0f); @@ -85,12 +85,17 @@ void Camera::process_keyboard(bool forward, bool backward, bool left, bool righ _acceleration = acceleration; } -glm::mat4 Camera::get_view_matrix() +glm::mat4 Camera::getViewMatrix() { return (glm::lookAt(_position, _position + _forward, _up)); } -glm::vec3 Camera::get_position() +glm::vec3 Camera::getPosition() { return (_position); +} + +void Camera::setPosition(glm::vec3 position) +{ + _position = position; } \ No newline at end of file diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index b06db29..82b67ca 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -54,6 +54,16 @@ void SceneParser::parseMaterial(std::stringstream &line) _scene->addMaterial(mat); } +void SceneParser::parseCamera(std::stringstream &line) +{ + float x,y,z; + + if (!(line >> x >> y >> z)) + throw std::runtime_error("Camera: Missing camera properties"); + + _scene->getCamera()->setPosition(glm::vec3(x, y, z)); +} + bool SceneParser::parseLine(const std::string &line) { if (line.empty() || line[0] == '#') @@ -77,6 +87,8 @@ bool SceneParser::parseLine(const std::string &line) if (identifier == "MAT") this->parseMaterial(ss); + else if (identifier == "CAM") + this->parseCamera(ss); } catch (const std::exception& e) { diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 3e4cd18..1e9d345 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -67,7 +67,7 @@ void Window::process_input() if (forward || backward || left || right || up || down) _frameCount = 0; - _scene->getCamera()->process_keyboard(forward, backward, left, right, up, down); + _scene->getCamera()->processKeyboard(forward, backward, left, right, up, down); } void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) @@ -89,7 +89,7 @@ void Window::mouseMoveCallback(GLFWwindow* window, double xpos, double ypos) if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) { - win->_scene->getCamera()->process_mouse(xoffset, yoffset, true); + win->_scene->getCamera()->processMouse(xoffset, yoffset, true); win->_frameCount = 0; }