diff --git a/includes/RT.hpp b/includes/RT.hpp index 9bca437..80b240b 100644 --- a/includes/RT.hpp +++ b/includes/RT.hpp @@ -16,7 +16,10 @@ # define WIDTH 1920 # define HEIGHT 1080 +#define GLM_ENABLE_EXPERIMENTAL + # include "glm/glm.hpp" +# include "glm/gtx/string_cast.hpp" # include "glm/gtc/matrix_transform.hpp" # include "glm/gtc/type_ptr.hpp" diff --git a/includes/RT/Camera.hpp b/includes/RT/Camera.hpp index 3b242df..a82c3bd 100644 --- a/includes/RT/Camera.hpp +++ b/includes/RT/Camera.hpp @@ -48,7 +48,7 @@ class Camera float _maxSpeed = 8.0f; float _acceleration_rate = 40.0f; - float _deceleration_rate = 20.0f; + float _deceleration_rate = 10000.0f; float _sensitivity = 0.2f; }; diff --git a/includes/RT/Scene.hpp b/includes/RT/Scene.hpp index 9b7be5e..4fbce65 100644 --- a/includes/RT/Scene.hpp +++ b/includes/RT/Scene.hpp @@ -17,6 +17,8 @@ struct GPUObject { + glm::mat4 transform; + alignas(16) glm::vec3 position; alignas(16) glm::vec3 normal; // plane triangle diff --git a/includes/RT/objects/Portal.hpp b/includes/RT/objects/Portal.hpp index 1354c13..2e88118 100644 --- a/includes/RT/objects/Portal.hpp +++ b/includes/RT/objects/Portal.hpp @@ -46,6 +46,18 @@ class Portal : public Object _edge1 = glm::vec3(x1, y1, z1); _edge2 = glm::vec3(x2, y2, z2); + glm::vec3 up = glm::normalize(_edge1); + glm::vec3 right = glm::normalize(_edge2); + glm::vec3 forward = glm::normalize(glm::cross(right, up)); + + up = normalize(glm::cross(forward, right)); + + _transform = glm::mat3(right, up, forward); + _normal = forward; + + std::cout << "Portal Transform Matrix:" << std::endl; + std::cout << glm::to_string(_transform) << std::endl; + _linked_portal = linked_portal; _mat_index = mat_index; @@ -57,12 +69,20 @@ class Portal : public Object glm::vec3 getEdge1() const { return (_edge1); } glm::vec3 getEdge2() const { return (_edge2); } + glm::vec3 getNormal() const { return (_normal); } + + glm::mat3 getTransform() const { return (_transform); } + int getLinkedPortalIndex() const { return (_linked_portal); } + Type getType() const override { return Type::PORTAL; } private: glm::vec3 _edge1; glm::vec3 _edge2; + glm::vec3 _normal; + + glm::mat3 _transform; int _linked_portal; }; diff --git a/scenes/test.rt b/scenes/test.rt index 66441c2..8b2d6f4 100644 --- a/scenes/test.rt +++ b/scenes/test.rt @@ -12,7 +12,11 @@ pl 0 0 0 0 1 0 0 cu 1 0.5 3 1 1 1 3 cu 1 0.5 -3 1 1 1 4 -po 3 0 0 2 0 0 0 2 0 4 0 -po -3 0 0 2 0 0 0 2 0 3 0 + +qu 2.9 0.9 -0.001 0 2.2 0 2.2 0 0 3 +po 3 1 0 0 2 0 2 0 0 6 0 + +qu -3.1 0.9 -0.001 0 2.2 0 2.2 0 0 4 +po -3 1 0 0 2 0 2 0 0 4 0 sp 0 30 0 30 2 \ No newline at end of file diff --git a/shaders/compute.glsl b/shaders/compute.glsl index 79806b3..91c6b4a 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -5,6 +5,8 @@ layout(binding = 0, rgba32f) uniform image2D output_image; layout(binding = 1, rgba32f) uniform image2D accumulation_image; struct GPUObject { + mat4 transform; + vec3 position; // 12 + 4 vec3 normal; // 12 + 4 @@ -12,6 +14,7 @@ struct GPUObject { vec3 vertex1; // 12 + 4 vec3 vertex2; // 12 + 4 + float radius; // 4 int mat_index; // 4 @@ -71,13 +74,17 @@ Ray portalRay(Ray ray, hitInfo hit) portal_1 = objects[hit.obj_index]; portal_2 = objects[int(portal_1.radius)]; // saving memory radius = portal_index - vec3 portal_2_normal = normalize(cross(portal_2.vertex1, portal_2.vertex2)); - portal_2_normal *= sign(dot(ray.direction, portal_2_normal)); + vec3 portal_2_normal = portal_2.normal * sign(dot(ray.direction, portal_2.normal)); - relative = portal_2.position - portal_1.position; + relative = hit.position - portal_1.position; - ray.origin = hit.position + relative + portal_2_normal * 0.01; - ray.direction = normalize(ray.direction + portal_2_normal * 0.01); + ray.origin = portal_2.position + mat3(portal_2.transform) * relative; + ray.origin += portal_2_normal * 0.01; + + if (dot(ray.direction, portal_2_normal) < 0.0) + ray.direction = reflect(ray.direction, portal_2_normal); + + ray.direction = normalize(mat3(portal_2.transform) * ray.direction); return (ray); } diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 569e15b..b472231 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -111,6 +111,8 @@ void Scene::updateGPUData() auto portal = static_cast(obj); gpu_obj.vertex1 = portal->getEdge1(); gpu_obj.vertex2 = portal->getEdge2(); + gpu_obj.normal = portal->getNormal(); + gpu_obj.transform = glm::mat4(portal->getTransform()); gpu_obj.radius = portal->getLinkedPortalIndex(); }