~ | Better camera system + | Quad intersection test

This commit is contained in:
TheRedShip
2025-01-03 16:23:09 +01:00
parent f973d77654
commit d25db020bf
16 changed files with 135 additions and 1876 deletions

View File

@ -15,6 +15,9 @@ struct GPUObject {
float radius; // 4
vec3 normal; // 12 + 4
vec3 edge1; // 12 + 4
vec3 edge2; // 12 + 4
int type; // 4
};

View File

@ -29,11 +29,43 @@ bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit)
return (valid);
}
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));
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);
// Check if point is inside quad using edge lengths
float l1 = dot(obj.edge1, obj.edge1);
float l2 = dot(obj.edge2, obj.edge2);
bool inside = e1 >= 0.0 && e1 <= l1 && e2 >= 0.0 && e2 <= l2;
hit.t = t;
hit.position = p + obj.position;
hit.normal = d < 0.0 ? normal : -normal;
return (inside);
}
bool intersect(Ray ray, GPUObject obj, out hitInfo hit)
{
if (obj.type == 0)
return (intersectSphere(ray, obj, hit));
if (obj.type == 1)
return (intersectPlane(ray, obj, hit));
if (obj.type == 2)
return (intersectQuad(ray, obj, hit));
return (false);
}