From 635cadf52719b96e9c8f69700a2a9688ae6cac75 Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Tue, 4 Feb 2025 22:36:10 +0100 Subject: [PATCH] + | Portal teleportation --- includes/RT/Camera.hpp | 6 +++++- scenes/dragon.rt | 6 +++--- scenes/lambo.rt | 2 +- scenes/noneuclidian.rt | 27 ++++++++++++++++++--------- srcs/class/Camera.cpp | 40 +++++++++++++++++++++++++++++++++++++++- srcs/class/Window.cpp | 2 +- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/includes/RT/Camera.hpp b/includes/RT/Camera.hpp index 32886fb..757680f 100644 --- a/includes/RT/Camera.hpp +++ b/includes/RT/Camera.hpp @@ -26,6 +26,8 @@ struct GPUCamera int bounce; }; +class Scene; + class Camera { public: @@ -34,12 +36,14 @@ class Camera ~Camera(void); - void update(float deltaTime); + void update(Scene *scene, float deltaTime); void processMouse(float xoffset, float yoffset, bool constrainPitch); void processKeyboard(bool forward, bool backward, bool left, bool right, bool up, bool down); void updateCameraVectors(); + void portalTeleport(Scene *scene); + glm::vec3 getPosition(); glm::vec2 getDirection(); glm::mat4 getViewMatrix(); diff --git a/scenes/dragon.rt b/scenes/dragon.rt index 93963d9..fd53608 100644 --- a/scenes/dragon.rt +++ b/scenes/dragon.rt @@ -22,10 +22,10 @@ pl 0 2 0 0 -1 0 0 // ceiling pl 0 -2 0 0 1 0 2 // floor -qu -1 1.999 -1 2 0 0 0 0 2 6 +qu -1 1.999 -1 0 0 2 2 0 0 1 6 OBJ scenes/obj/Dragon_800K.obj -0.5 0 0.55 5 0 90 0 OBJ scenes/obj/Dragon_800K.obj 0.5 0 -0.55 5 0 -90 0 -po -1.99 -0.5 -0.5 0 1 0 0 0 1 1 4 -po 1.99 -0.5 -0.5 0 1 0 0 0 1 0 4 +po -1.99 -0.5 -0.5 0 1 0 0 0 1 0 4 +po 1.99 -0.5 -0.5 0 1 0 0 0 1 1 4 diff --git a/scenes/lambo.rt b/scenes/lambo.rt index 19c8f89..eb4dc9a 100644 --- a/scenes/lambo.rt +++ b/scenes/lambo.rt @@ -19,7 +19,7 @@ cu 0 10 0 5 5 5 3 sp 0 10 0 1 4 -OBJ obj/lambo.obj 0 1.5 0 1 0 0 0 +OBJ scenes/obj/lambo.obj 0 1.5 0 1 0 0 0 diff --git a/scenes/noneuclidian.rt b/scenes/noneuclidian.rt index c47f2c5..c9af13c 100644 --- a/scenes/noneuclidian.rt +++ b/scenes/noneuclidian.rt @@ -3,18 +3,27 @@ CAM 0 2.59881 7 -4.8 -89.9996 0 1 90 5 MAT 100 200 100 0.0 0.0 0.0 // 0 MAT 200 200 200 0.0 0.0 0.0 // 1 -MAT 200 200 200 0 0 0 // 2 portal +MAT 200 100 100 0 0 0 // 2 portal +MAT 100 100 200 0 0 0 // 3 portal + +MAT 100 200 100 0 0 0 // 4 portal +MAT 200 100 200 0 0 0 // 5 portal pl 0 0 0 0 1 0 0 # long tunnel -po -1.5 0 4.5 3 0 0 0 3 0 0 2 -po -1.5 0 -4.5 3 0 0 0 3 0 1 2 - qu -1.5 0 -5 0 3 0 0 0 10 0 1 qu 1.5 0 -5 0 3 0 0 0 10 0 1 qu -1.5 3 -5 3 0 0 0 0 10 0 1 +# long tunnel entrance +po -1.5 0 5 3 0 0 0 3 0 0 2 +po 9.5 0 0.95 3 0 0 0 3 0 1 2 + +# long tunnel entrance behind +po -1.5 0 -5 3 0 0 0 3 0 1 3 +po 9.5 0 -0.95 3 0 0 0 3 0 0 3 + # small tunnel qu 9.5 0 -1 0 3 0 0 0 2 0 1 @@ -22,9 +31,9 @@ qu 12.5 0 -1 0 3 0 0 0 2 0 1 qu 9.5 3 -1 3 0 0 0 0 2 0 1 # small tunnel entrance -po 9.5 0 1 3 0 0 0 3 0 0 2 -po -1.5 0 4.49 3 0 0 0 3 0 1 2 +# po 9.5 0 1 3 0 0 0 3 0 0 4 +# po -1.5 0 4.99 3 0 0 0 3 0 1 4 -#small tunnel entrace behind -po 9.5 0 -1 3 0 0 0 3 0 1 2 -po -1.5 0 -4.49 3 0 0 0 3 0 0 2 \ No newline at end of file +# #small tunnel entrace behind +# po 9.5 0 -1 3 0 0 0 3 0 1 5 +# po -1.5 0 -4.99 3 0 0 0 3 0 0 5 \ No newline at end of file diff --git a/srcs/class/Camera.cpp b/srcs/class/Camera.cpp index aaab1b0..450f5ac 100644 --- a/srcs/class/Camera.cpp +++ b/srcs/class/Camera.cpp @@ -36,7 +36,7 @@ void Camera::updateCameraVectors() _up = glm::normalize(glm::cross(_right, _forward)); } -void Camera::update(float delta_time) +void Camera::update(Scene *scene, float delta_time) { // delta_time = std::min(delta_time, 0.01f); @@ -51,8 +51,46 @@ void Camera::update(float delta_time) _position += _velocity * delta_time; _acceleration = glm::vec3(0.0f); + + this->portalTeleport(scene); } +void Camera::portalTeleport(Scene *scene) +{ + for (const GPUObject &obj : scene->getObjectData()) + { + if (obj.type != (int)Object::Type::PORTAL) + continue; + + glm::vec3 portal_to_camera = _position - obj.position; + float distance_plane = glm::dot(portal_to_camera, obj.normal); + glm::vec3 point_projected = _position - distance_plane * obj.normal; + + glm::mat2 A = glm::mat2( + glm::dot(obj.vertex1, obj.vertex1), glm::dot(obj.vertex1, obj.vertex2), + glm::dot(obj.vertex1, obj.vertex2), glm::dot(obj.vertex2, obj.vertex2) + ); + glm::vec2 b = glm::vec2( + glm::dot(point_projected - obj.position, obj.vertex1), + glm::dot(point_projected - obj.position, obj.vertex2) + ); + glm::vec2 alphaBeta = glm::inverse(A) * b; + + if (alphaBeta.x >= 0.0f && alphaBeta.x <= 1.0f && alphaBeta.y >= 0.0f && alphaBeta.y <= 1.0f) + { + float distance = glm::length(point_projected - _position); + if (distance < 0.1f) + { + GPUObject linked_portal = scene->getObjectData()[obj.radius]; + std::cout << glm::to_string(point_projected) << std::endl; + _position = linked_portal.position + (_position - obj.position) - linked_portal.normal * 0.1f; + std::cout << "Teleporting to " << glm::to_string(_position) << std::endl; + + break ; + } + } + } +} void Camera::processMouse(float xoffset, float yoffset, bool constraint_pitch = true) { diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index bfc3fa1..d440611 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -155,7 +155,7 @@ void Window::display() void Window::pollEvents() { this->process_input(); - _scene->getCamera()->update(_delta); + _scene->getCamera()->update(_scene, _delta); glfwPollEvents(); }