blob: 659e14409ba2bce3ec67fcc243fc5a535e7d426b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// Ray-sphere intersection.
// ro: ray origin, rd: ray direction (must be normalized).
// center: sphere center, radius: sphere radius.
// Returns t of the nearest positive intersection, or -1.0 if no hit.
struct RaySphereHit {
t: f32, // distance along ray to nearest hit (negative = miss)
hit: bool,
};
fn ray_sphere_intersection(ro: vec3f, rd: vec3f,
center: vec3f, radius: f32) -> RaySphereHit {
let oc = ro - center;
let b = dot(oc, rd);
let c = dot(oc, oc) - radius * radius;
let disc = b * b - c;
if (disc < 0.0) { return RaySphereHit(-1.0, false); }
let t = -b - sqrt(disc);
if (t < 0.0) { return RaySphereHit(-1.0, false); }
return RaySphereHit(t, true);
}
|