From 9aa1de378f235a1da835a132434a180e353040a4 Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Thu, 9 Jan 2025 22:42:09 +0100 Subject: [PATCH] + | Cylinder intersectio and rotation but capped edge + need opti --- includes/RT.hpp | 2 + includes/RT/Object.hpp | 3 +- includes/RT/objects/Cylinder.hpp | 69 ++++++++++++++++++++++++++++++++ includes/RT/objects/Portal.hpp | 6 +-- scenes/portalrotation.rt | 2 +- scenes/test.rt | 4 +- shaders/compute.glsl | 5 ++- shaders/intersect.glsl | 61 +++++++++++++++++++++++++++- srcs/class/Scene.cpp | 8 +++- srcs/class/SceneParser.cpp | 6 +++ 10 files changed, 155 insertions(+), 11 deletions(-) create mode 100644 includes/RT/objects/Cylinder.hpp diff --git a/includes/RT.hpp b/includes/RT.hpp index 7014dd2..3f2d074 100644 --- a/includes/RT.hpp +++ b/includes/RT.hpp @@ -22,6 +22,7 @@ # include "glm/gtx/string_cast.hpp" # include "glm/gtc/matrix_transform.hpp" # include "glm/gtc/type_ptr.hpp" +# include "glm/gtx/euler_angles.hpp" # include "glad/gl.h" # include "GLFW/glfw3.h" @@ -46,6 +47,7 @@ struct Vertex { # include "objects/Triangle.hpp" # include "objects/Cube.hpp" # include "objects/Portal.hpp" +# include "objects/Cylinder.hpp" # include "Camera.hpp" # include "Window.hpp" diff --git a/includes/RT/Object.hpp b/includes/RT/Object.hpp index 29f9b3d..edd88fb 100644 --- a/includes/RT/Object.hpp +++ b/includes/RT/Object.hpp @@ -43,7 +43,8 @@ class Object QUAD, TRIANGLE, CUBE, - PORTAL + PORTAL, + CYLINDER }; virtual Type getType() const = 0; diff --git a/includes/RT/objects/Cylinder.hpp b/includes/RT/objects/Cylinder.hpp new file mode 100644 index 0000000..81c79e8 --- /dev/null +++ b/includes/RT/objects/Cylinder.hpp @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cylinder.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ycontre +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */ +/* Updated: 2025/01/08 20:20:34 by ycontre ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RT_CYLINDER__HPP +# define RT_CYLINDER__HPP + +# include "RT.hpp" + +class Cylinder : public Object +{ + public: + Cylinder(std::stringstream &line) : Object(glm::vec3(0.0f), -1) + { + try { + float x, y, z; + float radius, height; + float pitch, yaw, roll; + + int mat_index; + + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing position"); + + if (!(line >> radius >> height)) + throw std::runtime_error("Missing radius or height values"); + + if (!(line >> pitch >> yaw >> roll)) + throw std::runtime_error("Missing rotation values"); + + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); + + _position = glm::vec3(x, y, z); + + _radius = radius / 2.0; + _height = height; + + _rotation = glm::mat3(glm::eulerAngleXYZ(pitch, yaw, roll)); + + _mat_index = mat_index; + } + catch (const std::exception& e) { throw; } + } + Cylinder(const glm::vec3& position, float radius, const int mat_index) + : Object(position, mat_index), _radius(radius) {} + + float getRadius() const { return (_radius); } + float getHeight() const { return (_height); } + glm::mat3 getRotation() const { return (_rotation); } + + Type getType() const override { return Type::CYLINDER; } + + private: + float _radius; + float _height; + + glm::mat3 _rotation; +}; + +#endif \ No newline at end of file diff --git a/includes/RT/objects/Portal.hpp b/includes/RT/objects/Portal.hpp index 331ae7d..de274a9 100644 --- a/includes/RT/objects/Portal.hpp +++ b/includes/RT/objects/Portal.hpp @@ -54,7 +54,7 @@ class Portal : public Object up = normalize(glm::cross(forward, right)); - _transform = glm::mat3(right, up, forward); + _rotation = glm::mat3(right, up, forward); _normal = forward * (invert_normal ? -1.0f : 1.0f); _linked_portal = -1; @@ -89,7 +89,7 @@ class Portal : public Object glm::vec3 getRight() const { return (_right); } glm::vec3 getNormal() const { return (_normal); } - glm::mat3 getTransform() const { return (_transform); } + glm::mat3 getRotation() const { return (_rotation); } int getLinkedPortalIndex() const { return (_linked_portal); } void setLinkedPortalIndex(int index) { _linked_portal = index; } @@ -101,7 +101,7 @@ class Portal : public Object glm::vec3 _right; glm::vec3 _normal; - glm::mat3 _transform; + glm::mat3 _rotation; int _linked_portal; }; diff --git a/scenes/portalrotation.rt b/scenes/portalrotation.rt index de8c22c..8f95100 100644 --- a/scenes/portalrotation.rt +++ b/scenes/portalrotation.rt @@ -20,5 +20,5 @@ pl -3 0 0 1 0 0 3 pl 0 0 -3 0 0 1 4 pl 0 0 3 0 0 -1 2 -po -0.33 -0.66 -1 -0.25 1 0 1 0.25 0 0 2 +po -0.33 -0.66 -1 0 1 0 0.5 0.5 0 0 2 po -0.5 -0.5 1 0 1 0 1 0 0 1 4 \ No newline at end of file diff --git a/scenes/test.rt b/scenes/test.rt index 65d97e7..b64e276 100644 --- a/scenes/test.rt +++ b/scenes/test.rt @@ -2,7 +2,7 @@ CAM -0.0970577 1.63916 1.69444 -13.6 -84 0 1 MAT 200 200 200 0.0 0.0 0.0 //white -MAT 255 50 50 0.0 0.0 0.0 //red +MAT 255 50 50 0.0 1.0 0.0 //red MAT 50 255 50 0.0 0.0 0.0 //green MAT 100 100 255 0.0 0.0 0.0 //blue MAT 255 100 255 0.0 0.0 0.0 //purple @@ -19,3 +19,5 @@ sp -0.8 0.8 0 1.5 1 sp -2.2 0.4 0.25 1 2 sp -3 0.2 0.50 0.75 3 sp -3.8 -0.1 0.60 0.5 5 + +cy 0 1 2 0.5 2 -1.5 0 0.75 1 \ No newline at end of file diff --git a/shaders/compute.glsl b/shaders/compute.glsl index 180fec9..47e8c10 100644 --- a/shaders/compute.glsl +++ b/shaders/compute.glsl @@ -5,7 +5,7 @@ layout(binding = 0, rgba32f) uniform image2D output_image; layout(binding = 1, rgba32f) uniform image2D accumulation_image; struct GPUObject { - mat4 transform; + mat4 rotation; vec3 position; // 12 + 4 @@ -87,7 +87,7 @@ Ray portalRay(Ray ray, hitInfo hit) relative = hit.position - portal_1.position; - mat3 rotation = mat3(portal_2.transform) * transpose(mat3(portal_1.transform)); + mat3 rotation = mat3(portal_2.rotation) * transpose(mat3(portal_1.rotation)); if (dot(portal_1.normal, portal_2.normal) > 0.0) { @@ -169,6 +169,7 @@ vec3 pathtrace(Ray ray, inout uint rng_state) ray = newRay(hit, ray, rng_state); } + return (color * light); } diff --git a/shaders/intersect.glsl b/shaders/intersect.glsl index 1ca3652..ce2da7a 100644 --- a/shaders/intersect.glsl +++ b/shaders/intersect.glsl @@ -58,7 +58,7 @@ bool intersectQuad(Ray ray, GPUObject obj, out hitInfo hit) return (inside); } -intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit) +bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit) { vec3 pvec = cross(ray.direction, obj.vertex2); float det = dot(obj.vertex1, pvec); @@ -81,7 +81,6 @@ intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit) return (valid); } -bool // bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit) // { @@ -150,6 +149,62 @@ bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit) return true; } +bool intersectCylinder(Ray ray, GPUObject obj, out hitInfo hit) +{ + float radius = obj.normal.x; + float height = obj.normal.y; + + vec3 p = ray.origin - obj.position; + + vec3 d = ray.direction * mat3(obj.rotation); + p = p * mat3(obj.rotation); + + float half_height = height * 0.5; + + float a = d.x * d.x + d.z * d.z; + float b = p.x * d.x + p.z * d.z; + float c = p.x * p.x + p.z * p.z - radius * radius; + + float h = b * b - a * c; + if (h < 0.0) return false; + + float sqrt_h = sqrt(h); + float t = (-b - sqrt_h) / a; + + if (t <= 0.0) + { + t = (-b + sqrt_h) / a; + if (t <= 0.0) return false; + } + + float y = p.y + t * d.y; + + if (abs(y) <= half_height) + { + + hit.t = t; + hit.position = ray.origin + ray.direction * t; + + vec3 local_normal = vec3(p.x + t * d.x, 0.0, p.z + t * d.z); + hit.normal = normalize(local_normal * inverse(mat3(obj.rotation))); + return true; + } + + float cap_t = (sign(y) * half_height - p.y) / d.y; + if (cap_t <= 0.0) return false; + + float cap_x = p.x + cap_t * d.x; + float cap_z = p.z + cap_t * d.z; + if (cap_x * cap_x + cap_z * cap_z > radius * radius) return false; + + hit.t = cap_t; + hit.position = ray.origin + ray.direction * cap_t; + + + vec3 cap_normal = vec3(0.0, sign(y), 0.0); + hit.normal = normalize(cap_normal * inverse(mat3(obj.rotation))); + return true; +} bool intersect(Ray ray, GPUObject obj, out hitInfo hit) { @@ -163,5 +218,7 @@ bool intersect(Ray ray, GPUObject obj, out hitInfo hit) return (intersectTriangle(ray, obj, hit)); if (obj.type == 4) return (intersectCube(ray, obj, hit)); + if (obj.type == 6) + return (intersectCylinder(ray, obj, hit)); return (false); } \ No newline at end of file diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 76623e1..73c9afb 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -107,13 +107,19 @@ void Scene::updateGPUData() gpu_obj.position = cube->getPosition(); gpu_obj.vertex1 = cube->getSize(); } + else if (obj->getType() == Object::Type::CYLINDER) + { + auto cylinder = static_cast(obj); + gpu_obj.normal = glm::vec3(cylinder->getRadius(), cylinder->getHeight(), 0.0f); + gpu_obj.transform = glm::mat4(cylinder->getRotation()); + } else if (obj->getType() == Object::Type::PORTAL) { auto portal = static_cast(obj); gpu_obj.vertex1 = portal->getUp(); gpu_obj.vertex2 = portal->getRight(); gpu_obj.normal = portal->getNormal(); - gpu_obj.transform = glm::mat4(portal->getTransform()); + gpu_obj.transform = glm::mat4(portal->getRotation()); Portal *linked = static_cast(_objects[i - 2]); diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index b5837a7..9463199 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -49,6 +49,12 @@ SceneParser::SceneParser(Scene *scene) : _scene(scene) try { return (new Portal(ss)); } catch (const std::exception &e) { throw; } }; + + object_parsers["cy"] = [](std::stringstream &ss) -> Object * + { + try { return (new Cylinder(ss)); } + catch (const std::exception &e) { throw; } + }; } void SceneParser::parseMaterial(std::stringstream &line)