From d3dbf8f9599b6f2cb50917f195e181c4c907f66b Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Thu, 6 Feb 2025 00:16:25 +0100 Subject: [PATCH] + | Portal viewing angle teleportation --- imgui.ini | 4 ++-- includes/RT/Camera.hpp | 1 + includes/RT/Window.hpp | 1 + includes/RT/objects/Portal.hpp | 12 +--------- scenes/lambo.rt | 2 +- scenes/noneuclidian.rt | 14 ++++++++++-- shaders/compute.glsl | 14 +++++------- srcs/RT.cpp | 2 ++ srcs/class/Camera.cpp | 41 ++++++++++++++++++++++++++++++++-- srcs/class/Window.cpp | 8 ++++++- 10 files changed, 72 insertions(+), 27 deletions(-) diff --git a/imgui.ini b/imgui.ini index b3e0d68..fc7ef94 100644 --- a/imgui.ini +++ b/imgui.ini @@ -29,6 +29,6 @@ Pos=1556,610 Size=284,382 [Window][Settings] -Pos=83,57 -Size=336,330 +Pos=1567,9 +Size=340,941 diff --git a/includes/RT/Camera.hpp b/includes/RT/Camera.hpp index 3b81d31..924f54a 100644 --- a/includes/RT/Camera.hpp +++ b/includes/RT/Camera.hpp @@ -41,6 +41,7 @@ class Camera void processKeyboard(bool forward, bool backward, bool left, bool right, bool up, bool down); void updateCameraVectors(); + void updateCameraDirections(); int portalTeleport(Scene *scene, float delta_time); diff --git a/includes/RT/Window.hpp b/includes/RT/Window.hpp index 4c654d1..e139ddf 100644 --- a/includes/RT/Window.hpp +++ b/includes/RT/Window.hpp @@ -23,6 +23,7 @@ class Window Window(Scene *scene, int width, int height, const char *title, int sleep, Arguments &args); ~Window(void); + void updateDeltaTime(); void display(); void pollEvents(); bool shouldClose(); diff --git a/includes/RT/objects/Portal.hpp b/includes/RT/objects/Portal.hpp index e1ab03f..0e341b3 100644 --- a/includes/RT/objects/Portal.hpp +++ b/includes/RT/objects/Portal.hpp @@ -45,16 +45,10 @@ class Portal : public Object _up = glm::vec3(x1, y1, z1); _right = glm::vec3(x2, y2, z2); - // glm::vec3 temp_right = _right; - // _right = _invert_normal ? _up : _right; - // _up = _invert_normal ? temp_right : _up; - glm::vec3 right = glm::normalize(_right); glm::vec3 up = glm::normalize(_up); glm::vec3 forward = glm::normalize(glm::cross(right, up)); - // up = normalize(glm::cross(forward, right)); - _rotation = glm::mat3(right, up, forward); _normal = forward * (_invert_normal ? -1.0f : 1.0f); @@ -72,10 +66,6 @@ class Portal : public Object glm::vec3 right_dir = glm::normalize(_right); glm::vec3 up_dir = glm::normalize(_up); - // glm::vec3 temp_right = right_dir; - // right_dir = _invert_normal ? right_dir : up_dir; - // up_dir = _invert_normal ? up_dir : temp_right; - float right_length = glm::length(_right) + extension; float up_length = glm::length(_up) + extension; @@ -84,7 +74,7 @@ class Portal : public Object glm::vec3 right = right_dir * right_length; glm::vec3 up = up_dir * up_length; - // position += 10; + return (new Quad(position, right, up, _normal, 1, _mat_index)); } 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 7ad933b..55f68cf 100644 --- a/scenes/noneuclidian.rt +++ b/scenes/noneuclidian.rt @@ -1,4 +1,4 @@ -CAM -2.43549 3.10568 -6.50845 -16.4 53.8007 0 1 90 5 +CAM -3.31413 5.09653 9.67312 -19.6 -74.7992 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 @@ -36,4 +36,14 @@ qu 9.5 3 -1 3 0 0 0 0 2 0 1 # #small tunnel entrace behind # po 9.5 2 -1 3 0 0 0 3 0 1 5 -# po -1.5 2 -4 3 0 0 0 3 0 0 5 \ No newline at end of file +# po -1.5 2 -4 3 0 0 0 3 0 0 5 + +#top portals + +po -6 1 0 3 0 0 0 0 3 1 1 +po -10 1 0 0 3 0 0 0 3 0 1 + + + +po -1 1.5 10 0 1.5 0 1.5 0 0 0 1 +po 1 1.5 10 0 0 1.5 1.5 0 0 0 1 \ No newline at end of file diff --git a/shaders/compute.glsl b/shaders/compute.glsl index febe279..5797d84 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -187,16 +187,14 @@ vec3 pathtrace(Ray ray, inout uint rng_state) } #endif - - float miss_condition = float(hit.obj_index == -1); - light += miss_condition * transmittance * GetEnvironmentLight(ray); + if (hit.obj_index == -1) + { + light += transmittance * GetEnvironmentLight(ray); + break; + } float p = max(color.r, max(color.g, color.b)); - float rr_continue = float(randomValue(rng_state) <= p); - - float break_condition = miss_condition + (1.0 - rr_continue); - if (break_condition > 0.0) break; - + if (randomValue(rng_state) >= p) break; color /= max(p, 0.001); GPUMaterial mat = materials[hit.mat_index]; diff --git a/srcs/RT.cpp b/srcs/RT.cpp index a9f12ee..5f66716 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -104,6 +104,8 @@ int main(int argc, char **argv) while (!window.shouldClose()) { + window.updateDeltaTime(); + glUseProgram(shader.getProgramCompute()); glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO); diff --git a/srcs/class/Camera.cpp b/srcs/class/Camera.cpp index 3aaf6f7..29975f1 100644 --- a/srcs/class/Camera.cpp +++ b/srcs/class/Camera.cpp @@ -36,6 +36,19 @@ void Camera::updateCameraVectors() _up = glm::normalize(glm::cross(_right, _forward)); } +void Camera::updateCameraDirections() +{ + glm::vec3 forward_xz = glm::normalize(glm::vec3(_forward.x, 0.0f, _forward.z)); + _pitch = glm::degrees(asin(_forward.y)); + + _yaw = glm::degrees(atan2(-_forward.x, _forward.z)); + + _yaw = fmod(_yaw + 360.0f, 360.0f); + _pitch = glm::clamp(_pitch, -89.0f, 89.0f); + + _yaw += 90; +} + void Camera::update(Scene *scene, float delta_time) { // delta_time = std::min(delta_time, 0.01f); @@ -83,11 +96,35 @@ int Camera::portalTeleport(Scene *scene, float delta_time) float distance_future_pos = glm::length(future_pos - _position); float distance_portal = glm::length(point_projected - _position); + float imprecision = 0.1f; + if (distance_portal <= distance_future_pos && glm::dot(glm::normalize(future_pos - _position), obj.normal) > 0.0f) { GPUObject linked_portal = scene->getObjectData()[obj.radius]; - _position = (linked_portal.position) + (_position - obj.position) - (((distance_future_pos - distance_portal)) * linked_portal.normal); - + + glm::mat4 portal_transform = linked_portal.transform * glm::inverse(obj.transform); + + //teleportation + + glm::vec3 relative_pos = _position - obj.position; + glm::vec3 transformed_relative_pos = glm::vec3(portal_transform * glm::vec4(relative_pos, 1.0f)); + + float remaining_distance = distance_future_pos - distance_portal + imprecision; + glm::vec3 new_movement = remaining_distance * glm::vec3(portal_transform * glm::vec4(linked_portal.normal, 0.0f)); + + _position = linked_portal.position + transformed_relative_pos - new_movement; + // _position = (linked_portal.position) + (_position - obj.position) - (((distance_future_pos - distance_portal + imprecision)) * linked_portal.normal); + + // view direction + + _forward = glm::vec3(portal_transform * glm::vec4(_forward, 1.0f)); + _up = glm::vec3(portal_transform * glm::vec4(_up, 1.0f)); + _right = glm::vec3(portal_transform * glm::vec4(_right, 1.0f)); + + updateCameraDirections(); + + _velocity = glm::vec3(portal_transform * glm::vec4(_velocity, 0.0f)); + return (1); } } diff --git a/srcs/class/Window.cpp b/srcs/class/Window.cpp index 108f583..6794415 100644 --- a/srcs/class/Window.cpp +++ b/srcs/class/Window.cpp @@ -134,7 +134,7 @@ void Window::keyCallback(GLFWwindow *window, int key, int scancode, int action, } } -void Window::display() +void Window::updateDeltaTime() { static double lastTime = glfwGetTime(); double currentTime = glfwGetTime(); @@ -146,6 +146,12 @@ void Window::display() if (accumulate) _frameCount++; +} + +void Window::display() +{ + if (accumulate) + _frameCount++; if (_scene->getCamera()->getVelocity() > 0.0f) _frameCount = 0;