mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
~ | Better camera system + | Quad intersection test
This commit is contained in:
@ -15,6 +15,9 @@ struct GPUObject {
|
||||
float radius; // 4
|
||||
vec3 normal; // 12 + 4
|
||||
|
||||
vec3 edge1; // 12 + 4
|
||||
vec3 edge2; // 12 + 4
|
||||
|
||||
int type; // 4
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
}
|
Reference in New Issue
Block a user