+ | Triangle intersection

This commit is contained in:
TheRedShip
2025-01-05 23:16:18 +01:00
parent b9b7084a4f
commit 93e9e45224
15 changed files with 797 additions and 30 deletions

View File

@ -31,23 +31,19 @@ bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit)
bool intersectQuad(Ray ray, GPUObject obj, out hitInfo hit)
{
// obj.edge1 and obj.edge2 are the two edge vectors from quad origin
vec3 normal = normalize(cross(obj.edge1, obj.edge2));
vec3 normal = normalize(cross(obj.vertex1, obj.vertex2));
float d = dot(normal, ray.direction);
float t = dot(obj.position - ray.origin, normal) / d;
if (t <= 0.0 || d == 0.0) return (false);
// Get hit point relative to quad origin
vec3 p = ray.origin + ray.direction * t - obj.position;
// Project hit point onto edges using dot product
float e1 = dot(p, obj.edge1);
float e2 = dot(p, obj.edge2);
float e1 = dot(p, obj.vertex1);
float e2 = dot(p, obj.vertex2);
// Check if point is inside quad using edge lengths
float l1 = dot(obj.edge1, obj.edge1);
float l2 = dot(obj.edge2, obj.edge2);
float l1 = dot(obj.vertex1, obj.vertex1);
float l2 = dot(obj.vertex2, obj.vertex2);
bool inside = e1 >= 0.0 && e1 <= l1 && e2 >= 0.0 && e2 <= l2;
@ -58,6 +54,60 @@ bool intersectQuad(Ray ray, GPUObject obj, out hitInfo hit)
return (inside);
}
// bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
// {
// vec3 pvec = cross(ray.direction, obj.vertex2);
// float det = dot(obj.vertex1, pvec);
// vec3 tvec = ray.origin - obj.position;
// float invDet = 1.0 / det;
// float u = dot(tvec, pvec) * invDet;
// vec3 qvec = cross(tvec, obj.vertex1);
// float v = dot(ray.direction, qvec) * invDet;
// float t = dot(obj.vertex2, qvec) * invDet;
// bool valid = abs(det) > 1e-8 &&
// u >= 0.0 && u <= 1.0 &&
// v >= 0.0 && (u + v) <= 1.0 &&
// t > 0.0;
// hit.t = t;
// hit.position = ray.origin + ray.direction * t;
// hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal));
// return (valid);
// }
bool intersectTriangle(Ray ray, GPUObject obj, out hitInfo hit)
{
vec3 pvec = cross(ray.direction, obj.vertex2);
float det = dot(obj.vertex1, pvec);
if (abs(det) < 1e-8) return (false); // det < 0.0
float invDet = 1.0 / det;
vec3 tvec = ray.origin - obj.position;
float u = dot(tvec, pvec) * invDet;
if (u < 0.0 || u > 1.0) return (false);
vec3 qvec = cross(tvec, obj.vertex1);
float v = dot(ray.direction, qvec) * invDet;
if (v < 0.0 || u + v > 1.0) return (false);
float t = dot(obj.vertex2, qvec) * invDet;
if (t <= 0.0) return (false);
hit.t = t;
hit.position = ray.origin + ray.direction * t;
vec3 normal = obj.normal;
hit.normal = dot(ray.direction, normal) < 0.0 ? normal : -normal;
return (true);
}
bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
{
if (obj.type == 0)
@ -66,6 +116,8 @@ bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
return (intersectPlane(ray, obj, hit));
if (obj.type == 2)
return (intersectQuad(ray, obj, hit));
if (obj.type == 3)
return (intersectTriangle(ray, obj, hit));
return (false);
}