+ | Cube intersection but problem with quads

This commit is contained in:
TheRedShip
2025-01-07 00:16:47 +01:00
parent a06e1f80a8
commit 56d3a1394b
9 changed files with 204 additions and 11 deletions

View File

@ -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"

View File

@ -42,6 +42,7 @@ class Object
PLANE,
QUAD,
TRIANGLE,
CUBE
};
virtual Type getType() const = 0;

View File

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Cube.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -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

78
scenes/stairs.rt Normal file
View File

@ -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

View File

@ -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

View File

@ -22,6 +22,8 @@ bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit)
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;
hit.normal = d < 0.0 ? obj.normal : -obj.normal;
@ -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);
}

View File

@ -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<const Cube *>(obj);
gpu_obj.position = cube->getPosition();
gpu_obj.vertex1 = cube->getSize();
gpu_obj.type = static_cast<int>(cube->getType());
}
_gpu_objects.push_back(gpu_obj);
}

View File

@ -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)