-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsphere.v
34 lines (32 loc) · 801 Bytes
/
sphere.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import math
struct Sphere {
mut:
center Vec3
radius f32
mat Material
}
fn (s Sphere) hit(r Ray, t_min f32, t_max f32, mut rec HitRecord) bool {
oc := r.origin().sub(s.center)
a := r.direction().dot(r.direction())
b := oc.dot(r.direction())
c := oc.dot(oc) - s.radius * s.radius
discriminant := b*b - a*c
if discriminant > 0 {
rec.mat = s.mat
mut temp := (-b - math.sqrtf((b*b) - (a*c)))/a
if temp < t_max && temp > t_min {
rec.t = temp
rec.p = r.point_at_parameter(rec.t)
rec.normal = rec.p.sub(s.center).scalar_div(s.radius)
return true
}
temp = (-b + math.sqrtf((b*b) - (a*c)))/a
if temp < t_max && temp > t_min {
rec.t = temp
rec.p = r.point_at_parameter(rec.t)
rec.normal = rec.p.sub(s.center).scalar_div(s.radius)
return true
}
}
return false
}