mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
+ | Cylinder intersectio and rotation but capped edge + need opti
This commit is contained in:
@ -22,6 +22,7 @@
|
|||||||
# include "glm/gtx/string_cast.hpp"
|
# include "glm/gtx/string_cast.hpp"
|
||||||
# include "glm/gtc/matrix_transform.hpp"
|
# include "glm/gtc/matrix_transform.hpp"
|
||||||
# include "glm/gtc/type_ptr.hpp"
|
# include "glm/gtc/type_ptr.hpp"
|
||||||
|
# include "glm/gtx/euler_angles.hpp"
|
||||||
|
|
||||||
# include "glad/gl.h"
|
# include "glad/gl.h"
|
||||||
# include "GLFW/glfw3.h"
|
# include "GLFW/glfw3.h"
|
||||||
@ -46,6 +47,7 @@ struct Vertex {
|
|||||||
# include "objects/Triangle.hpp"
|
# include "objects/Triangle.hpp"
|
||||||
# include "objects/Cube.hpp"
|
# include "objects/Cube.hpp"
|
||||||
# include "objects/Portal.hpp"
|
# include "objects/Portal.hpp"
|
||||||
|
# include "objects/Cylinder.hpp"
|
||||||
|
|
||||||
# include "Camera.hpp"
|
# include "Camera.hpp"
|
||||||
# include "Window.hpp"
|
# include "Window.hpp"
|
||||||
|
@ -43,7 +43,8 @@ class Object
|
|||||||
QUAD,
|
QUAD,
|
||||||
TRIANGLE,
|
TRIANGLE,
|
||||||
CUBE,
|
CUBE,
|
||||||
PORTAL
|
PORTAL,
|
||||||
|
CYLINDER
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual Type getType() const = 0;
|
virtual Type getType() const = 0;
|
||||||
|
69
includes/RT/objects/Cylinder.hpp
Normal file
69
includes/RT/objects/Cylinder.hpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Cylinder.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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
|
@ -54,7 +54,7 @@ class Portal : public Object
|
|||||||
|
|
||||||
up = normalize(glm::cross(forward, right));
|
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);
|
_normal = forward * (invert_normal ? -1.0f : 1.0f);
|
||||||
|
|
||||||
_linked_portal = -1;
|
_linked_portal = -1;
|
||||||
@ -89,7 +89,7 @@ class Portal : public Object
|
|||||||
glm::vec3 getRight() const { return (_right); }
|
glm::vec3 getRight() const { return (_right); }
|
||||||
glm::vec3 getNormal() const { return (_normal); }
|
glm::vec3 getNormal() const { return (_normal); }
|
||||||
|
|
||||||
glm::mat3 getTransform() const { return (_transform); }
|
glm::mat3 getRotation() const { return (_rotation); }
|
||||||
|
|
||||||
int getLinkedPortalIndex() const { return (_linked_portal); }
|
int getLinkedPortalIndex() const { return (_linked_portal); }
|
||||||
void setLinkedPortalIndex(int index) { _linked_portal = index; }
|
void setLinkedPortalIndex(int index) { _linked_portal = index; }
|
||||||
@ -101,7 +101,7 @@ class Portal : public Object
|
|||||||
glm::vec3 _right;
|
glm::vec3 _right;
|
||||||
glm::vec3 _normal;
|
glm::vec3 _normal;
|
||||||
|
|
||||||
glm::mat3 _transform;
|
glm::mat3 _rotation;
|
||||||
|
|
||||||
int _linked_portal;
|
int _linked_portal;
|
||||||
};
|
};
|
||||||
|
@ -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 4
|
||||||
pl 0 0 3 0 0 -1 2
|
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
|
po -0.5 -0.5 1 0 1 0 1 0 0 1 4
|
@ -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 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 50 255 50 0.0 0.0 0.0 //green
|
||||||
MAT 100 100 255 0.0 0.0 0.0 //blue
|
MAT 100 100 255 0.0 0.0 0.0 //blue
|
||||||
MAT 255 100 255 0.0 0.0 0.0 //purple
|
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 -2.2 0.4 0.25 1 2
|
||||||
sp -3 0.2 0.50 0.75 3
|
sp -3 0.2 0.50 0.75 3
|
||||||
sp -3.8 -0.1 0.60 0.5 5
|
sp -3.8 -0.1 0.60 0.5 5
|
||||||
|
|
||||||
|
cy 0 1 2 0.5 2 -1.5 0 0.75 1
|
@ -5,7 +5,7 @@ layout(binding = 0, rgba32f) uniform image2D output_image;
|
|||||||
layout(binding = 1, rgba32f) uniform image2D accumulation_image;
|
layout(binding = 1, rgba32f) uniform image2D accumulation_image;
|
||||||
|
|
||||||
struct GPUObject {
|
struct GPUObject {
|
||||||
mat4 transform;
|
mat4 rotation;
|
||||||
|
|
||||||
vec3 position; // 12 + 4
|
vec3 position; // 12 + 4
|
||||||
|
|
||||||
@ -87,7 +87,7 @@ Ray portalRay(Ray ray, hitInfo hit)
|
|||||||
|
|
||||||
relative = hit.position - portal_1.position;
|
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)
|
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);
|
ray = newRay(hit, ray, rng_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (color * light);
|
return (color * light);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ bool intersectQuad(Ray ray, GPUObject obj, out hitInfo hit)
|
|||||||
|
|
||||||
return (inside);
|
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);
|
vec3 pvec = cross(ray.direction, obj.vertex2);
|
||||||
float det = dot(obj.vertex1, pvec);
|
float det = dot(obj.vertex1, pvec);
|
||||||
@ -81,7 +81,6 @@ intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
|
|||||||
|
|
||||||
return (valid);
|
return (valid);
|
||||||
}
|
}
|
||||||
bool
|
|
||||||
|
|
||||||
// bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
|
// bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
|
||||||
// {
|
// {
|
||||||
@ -150,6 +149,62 @@ bool intersectCube(Ray ray, GPUObject obj, out hitInfo hit)
|
|||||||
return true;
|
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)
|
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));
|
return (intersectTriangle(ray, obj, hit));
|
||||||
if (obj.type == 4)
|
if (obj.type == 4)
|
||||||
return (intersectCube(ray, obj, hit));
|
return (intersectCube(ray, obj, hit));
|
||||||
|
if (obj.type == 6)
|
||||||
|
return (intersectCylinder(ray, obj, hit));
|
||||||
return (false);
|
return (false);
|
||||||
}
|
}
|
@ -107,13 +107,19 @@ void Scene::updateGPUData()
|
|||||||
gpu_obj.position = cube->getPosition();
|
gpu_obj.position = cube->getPosition();
|
||||||
gpu_obj.vertex1 = cube->getSize();
|
gpu_obj.vertex1 = cube->getSize();
|
||||||
}
|
}
|
||||||
|
else if (obj->getType() == Object::Type::CYLINDER)
|
||||||
|
{
|
||||||
|
auto cylinder = static_cast<Cylinder *>(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)
|
else if (obj->getType() == Object::Type::PORTAL)
|
||||||
{
|
{
|
||||||
auto portal = static_cast<Portal *>(obj);
|
auto portal = static_cast<Portal *>(obj);
|
||||||
gpu_obj.vertex1 = portal->getUp();
|
gpu_obj.vertex1 = portal->getUp();
|
||||||
gpu_obj.vertex2 = portal->getRight();
|
gpu_obj.vertex2 = portal->getRight();
|
||||||
gpu_obj.normal = portal->getNormal();
|
gpu_obj.normal = portal->getNormal();
|
||||||
gpu_obj.transform = glm::mat4(portal->getTransform());
|
gpu_obj.transform = glm::mat4(portal->getRotation());
|
||||||
|
|
||||||
Portal *linked = static_cast<Portal *>(_objects[i - 2]);
|
Portal *linked = static_cast<Portal *>(_objects[i - 2]);
|
||||||
|
|
||||||
|
@ -49,6 +49,12 @@ SceneParser::SceneParser(Scene *scene) : _scene(scene)
|
|||||||
try { return (new Portal(ss)); }
|
try { return (new Portal(ss)); }
|
||||||
catch (const std::exception &e) { throw; }
|
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)
|
void SceneParser::parseMaterial(std::stringstream &line)
|
||||||
|
Reference in New Issue
Block a user