From 56d3a1394b3c090b1e3644c0b9d184047a72d7ab Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Tue, 7 Jan 2025 00:16:47 +0100 Subject: [PATCH] + | Cube intersection but problem with quads --- includes/RT.hpp | 5 ++- includes/RT/Object.hpp | 1 + includes/RT/objects/Cube.hpp | 55 +++++++++++++++++++++++++ scenes/roughness.rt | 2 +- scenes/stairs.rt | 78 ++++++++++++++++++++++++++++++++++++ scenes/test.rt | 20 ++++++--- shaders/intersect.glsl | 41 ++++++++++++++++++- srcs/class/Scene.cpp | 7 ++++ srcs/class/SceneParser.cpp | 6 +++ 9 files changed, 204 insertions(+), 11 deletions(-) create mode 100644 includes/RT/objects/Cube.hpp create mode 100644 scenes/stairs.rt diff --git a/includes/RT.hpp b/includes/RT.hpp index ad9ef9d..8b69772 100644 --- a/includes/RT.hpp +++ b/includes/RT.hpp @@ -13,8 +13,8 @@ #ifndef RT__HPP # define RT__HPP -# define WIDTH 1920 -# define HEIGHT 1080 +# define WIDTH 1000 +# define HEIGHT 1000 # include "glm/glm.hpp" # include "glm/gtc/matrix_transform.hpp" @@ -40,6 +40,7 @@ struct Vertex { # include "objects/Plane.hpp" # include "objects/Quad.hpp" # include "objects/Triangle.hpp" +# include "objects/Cube.hpp" # include "Camera.hpp" # include "Window.hpp" diff --git a/includes/RT/Object.hpp b/includes/RT/Object.hpp index 7a2061b..d10172c 100644 --- a/includes/RT/Object.hpp +++ b/includes/RT/Object.hpp @@ -42,6 +42,7 @@ class Object PLANE, QUAD, TRIANGLE, + CUBE }; virtual Type getType() const = 0; diff --git a/includes/RT/objects/Cube.hpp b/includes/RT/objects/Cube.hpp new file mode 100644 index 0000000..8e4c0b6 --- /dev/null +++ b/includes/RT/objects/Cube.hpp @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Cube.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ycontre +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/23 19:12:51 by ycontre #+# #+# */ +/* Updated: 2024/12/23 19:47:09 by ycontre ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RT_CUBE__HPP +# define RT_CUBE__HPP + +# include "RT.hpp" + +class Cube : public Object +{ + public: + Cube(std::stringstream &line) : Object(glm::vec3(0.0f), -1) + { + try { + float x, y, z; + float width, height, depth; + int mat_index; + + if (!(line >> x >> y >> z)) + throw std::runtime_error("Missing position values"); + + if (!(line >> width >> height >> depth)) + throw std::runtime_error("Missing width, height or depth values"); + + if (!(line >> mat_index)) + throw std::runtime_error("Missing material properties"); + + _position = glm::vec3(x, y, z); + _size = glm::vec3(width, height, depth); + + _mat_index = mat_index; + } + catch (const std::exception& e) { throw; } + } + Cube(const glm::vec3& position, const glm::vec3 &size, const int mat_index) + : Object(position, mat_index), _size(size) {} + + const glm::vec3 &getSize() const { return (_size); } + Type getType() const override { return Type::CUBE; } + + private: + glm::vec3 _size; + +}; + +#endif \ No newline at end of file diff --git a/scenes/roughness.rt b/scenes/roughness.rt index b5c6cff..b8f9281 100644 --- a/scenes/roughness.rt +++ b/scenes/roughness.rt @@ -40,5 +40,5 @@ sp 2.4 1.5 -1 1.5 14 // light quad -qu -1 2.9 -1 2 0 0 0 0 2 0 +qu -1 2.9 -1 2 0 0 0 2 0 0 diff --git a/scenes/stairs.rt b/scenes/stairs.rt new file mode 100644 index 0000000..12e9f94 --- /dev/null +++ b/scenes/stairs.rt @@ -0,0 +1,78 @@ +CAM -7 10 6 + +MAT 255 255 255 4.0 0.0 0.0 //white light + +sp -10 100 10 50 0 +qu -2.5 15 -9 7 0 0 0 0 7 0 + + +MAT 255 100 10 0.0 0.5 1.0 +cu 0 0.3 0 3 3 3 1 +MAT 230 60 20 0.0 0.5 1.0 +cu 3 1 0 3 3 3 2 +MAT 245 140 5 0.0 0.5 1.0 +cu -3 0.4 0 3 3 3 3 +MAT 220 110 18 0.0 0.5 1.0 +cu 0 0.0 3 3 3 3 4 +MAT 240 90 0 0.0 0.5 1.0 +cu 0 0.87 -3 3 3 3 5 +MAT 220 110 18 0.0 0.5 1.0 +cu 3 0.3 3 3 3 3 6 +MAT 220 110 18 0.0 0.5 1.0 +cu -3 0.9 3 3 3 3 7 +MAT 220 110 18 0.0 0.5 1.0 +cu 3 -0.9 -3 3 3 3 8 +MAT 240 90 0 0.0 0.5 1.0 +cu -3 0.78 -3 3 3 3 9 +MAT 230 60 20 0.0 0.5 1.0 +cu 6 0.14 0 3 3 3 10 +MAT 245 140 5 0.0 0.5 1.0 +cu -6 0.96 0 3 3 3 11 +MAT 220 110 18 0.0 0.5 1.0 +cu 0 -0.27 6 3 3 3 12 +MAT 240 90 0 0.0 0.5 1.0 +cu 0 0.87 -6 3 3 3 13 +MAT 220 110 18 0.0 0.5 1.0 +cu 6 -1.05 6 3 3 3 14 +MAT 220 110 18 0.0 0.5 1.0 +cu -6 1.3 6 3 3 3 15 +MAT 220 110 18 0.0 0.5 1.0 +cu 6 1.3 -6 3 3 3 16 +MAT 240 90 0 0.0 0.5 1.0 +cu -6 1.2 -6 3 3 3 17 +MAT 210 67 25 0.0 0.5 1.0 +cu 6 0.28 3 3 3 3 18 +MAT 245 140 5 0.0 0.5 1.0 +cu -6 1.34 3 3 3 3 19 +MAT 245 100 5 0.0 0.5 1.0 +cu 6 1.5 -3 3 3 3 20 +MAT 245 80 5 0.0 0.5 1.0 +cu -6 0.8 -3 3 3 3 21 +MAT 210 100 40 0.0 0.5 1.0 +cu 3 -0.38 6 3 3 3 22 +MAT 223 120 38 0.0 0.5 1.0 +cu -3 0.85 6 3 3 3 23 +MAT 245 115 25 0.0 0.5 1.0 +cu 3 1 -6 3 3 3 24 +MAT 217 140 5 0.0 0.5 1.0 +cu -3 1.0 -6 3 3 3 25 +MAT 205 120 30 0.0 0.5 1.0 +cu -9 0.64 0 3 3 3 26 +MAT 255 80 40 0.0 0.5 1.0 +cu -9 -0.38 -3 3 3 3 27 +MAT 234 129 0 0.0 0.5 1.0 +cu -9 0.98 3 3 3 3 28 +MAT 244 95 28 0.0 0.5 1.0 +cu -9 1.23 -6 3 3 3 29 + +MAT 200 20 20 0.0 1.0 0.05 +MAT 255 255 255 0.0 1.8 0.0 1 +MAT 255 255 255 0.0 1.0 1.0 + +sp 0 5 -4 3 30 +sp -4 4 0 3 31 +cu 4 6 2 5 8 5 32 +cu -20 4 0 5 8 5 32 + +MAT 30 30 30 0.0 0.0 0.0 +pl 0 0 -7.7 0 0 1 33 \ No newline at end of file diff --git a/scenes/test.rt b/scenes/test.rt index 300224e..2371565 100644 --- a/scenes/test.rt +++ b/scenes/test.rt @@ -1,11 +1,19 @@ -MAT 255 255 255 0.0 0.0 0.0 //white -MAT 255 100 100 0.0 1.0 1.0 //red -MAT 255 255 255 5.0 0.0 0.0 //sun -CAM 0 100 0 +CAM 0 2 5 + +MAT 30 30 30 0.0 0.0 0.0 //white +MAT 255 255 255 0.0 1.0 1.0 //mirror +MAT 255 255 255 1.0 0.0 0.0 //sun + +MAT 255 100 100 0.0 0.0 0.0 //red pl 0 -1 0 0 1 0 0 +pl 0 -1 -13 0 0 1 0 +# pl 0 0 -1 0 0 1 0 -tr 0 0 0 1 0 0 0 0 1 0 +tr 0 0 0 1 0 0 0 0 1 1 +cu 0 1 0 1 1 1 3 -sp 15 30 -30 30 2 +qu -10 15 -9 0 7 0 0 0 7 2 + +# sp 15 30 -30 30 2 diff --git a/shaders/intersect.glsl b/shaders/intersect.glsl index b6dc046..1f219f4 100644 --- a/shaders/intersect.glsl +++ b/shaders/intersect.glsl @@ -21,6 +21,8 @@ bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit) float d = dot(obj.normal, ray.direction); float t = dot(obj.position - ray.origin, obj.normal) / d; bool valid = t >= 0.0 && d != 0.0; + + if (!valid) return (false); hit.t = t; hit.position = ray.origin + ray.direction * t; @@ -52,7 +54,7 @@ bool intersectQuad(Ray ray, GPUObject obj, out hitInfo hit) hit.t = t; hit.position = p + obj.position; - hit.normal = d < 0.0 ? normal : -normal; + hit.normal = normal * -sign(d); return (inside); } @@ -110,6 +112,40 @@ bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit) return (true); } +bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit) +{ + vec3 obj_size = obj.vertex1; + + vec3 boxMin = obj.position - obj_size * 0.5; + vec3 boxMax = obj.position + obj_size * 0.5; + + vec3 t1 = (boxMin - ray.origin) / ray.direction; + vec3 t2 = (boxMax - ray.origin) / ray.direction; + + vec3 tNear = min(t1, t2); + vec3 tFar = max(t1, t2); + + float tMin = max(max(tNear.x, tNear.y), tNear.z); + float tMax = min(min(tFar.x, tFar.y), tFar.z); + + if (tMax < tMin || tMax < 0.0) + return (false); + + hit.t = tMin > 0.0 ? tMin : tMax; // Use the closer valid intersection point + hit.position = ray.origin + hit.t * ray.direction; // Calculate hit position + + vec3 hitPointLocal = hit.position - obj.position; + vec3 halfSize = obj_size * 0.5; + + if (abs(hitPointLocal.x) > halfSize.x - 1e-4) + hit.normal = vec3(sign(hitPointLocal.x), 0.0, 0.0); + else if (abs(hitPointLocal.y) > halfSize.y - 1e-4) + hit.normal = vec3(0.0, sign(hitPointLocal.y), 0.0); + else if (abs(hitPointLocal.z) > halfSize.z - 1e-4) + hit.normal = vec3(0.0, 0.0, sign(hitPointLocal.z)); + + return (true); +} bool intersect(Ray ray, GPUObject obj, out hitInfo hit) { @@ -121,6 +157,7 @@ bool intersect(Ray ray, GPUObject obj, out hitInfo hit) return (intersectQuad(ray, obj, hit)); if (obj.type == 3) return (intersectTriangle(ray, obj, hit)); - + if (obj.type == 4) + return (intersectCube(ray, obj, hit)); return (false); } \ No newline at end of file diff --git a/srcs/class/Scene.cpp b/srcs/class/Scene.cpp index 3005871..0a5452d 100644 --- a/srcs/class/Scene.cpp +++ b/srcs/class/Scene.cpp @@ -99,6 +99,13 @@ void Scene::updateGPUData() gpu_obj.vertex2 = triangle->getVertex3(); gpu_obj.normal = triangle->getNormal(); } + else if (obj->getType() == Object::Type::CUBE) + { + auto cube = static_cast(obj); + gpu_obj.position = cube->getPosition(); + gpu_obj.vertex1 = cube->getSize(); + gpu_obj.type = static_cast(cube->getType()); + } _gpu_objects.push_back(gpu_obj); } diff --git a/srcs/class/SceneParser.cpp b/srcs/class/SceneParser.cpp index c28c11f..ba432a6 100644 --- a/srcs/class/SceneParser.cpp +++ b/srcs/class/SceneParser.cpp @@ -37,6 +37,12 @@ SceneParser::SceneParser(Scene *scene) : _scene(scene) try { return (new Triangle(ss)); } catch (const std::exception &e) { throw; } }; + + object_parsers["cu"] = [](std::stringstream &ss) -> Object * + { + try { return (new Cube(ss)); } + catch (const std::exception &e) { throw; } + }; } void SceneParser::parseMaterial(std::stringstream &line)