-
Notifications
You must be signed in to change notification settings - Fork 0
/
hittable.jl
68 lines (58 loc) · 2.09 KB
/
hittable.jl
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# abstract type for something a ray can hit
abstract type AbstractHittable end
# holds info on whether something hittable has been hit
struct HitRecord{T<:AbstractFloat}
point::Vector{T} # location of hit
normal::Vector{T} # normal vector to hittable object at `point` - always point out from object
t::T # distance along ray to hit
front_face::Bool # is hit on outside (true) or inside of object
object::AbstractHittable # the object the ray has hit
end
# check hit in vector of hittables
function hit(objs::Vector{T}, r::Ray, tmin, tmax) where {T<:AbstractHittable}
rec = 0.0
closest_so_far = tmax # will take smallest value along ray
hit_anything = false
# loop through hittable objects and return HitRecord for closest object
for obj in objs
temp_rec = hit(obj, r, tmin, closest_so_far)
if temp_rec != false && temp_rec.t < closest_so_far
closest_so_far = temp_rec.t
rec = temp_rec
hit_anything = true
end
end
return hit_anything ? rec : false
end
#### hittable objects ####
struct Sphere{T<:AbstractFloat,M<:AbstractMaterial} <: AbstractHittable
origin::Vector{T}
radius::T
material::M
end
# location of intersection with ray
function hit(sphere::Sphere, r::Ray, tmin, tmax)
# solve intersection equation
ac = r.origin - sphere.origin
a = sum(abs2, r.direction)
halfb = r.direction ⋅ ac
c = sum(abs2, ac) - sphere.radius^2
discriminant = halfb^2 - a * c
# return if no hit
discriminant < 0 && return false
sqrtd = √discriminant
# find nearest root that lies in the acceptable range
root = (-halfb - sqrtd) / a
if root < tmin || tmax < root
root = (-halfb + sqrtd) / a
if root < tmin || tmax < root
return false
end
end
# return HitRecord otherwise
p = point(r, root)
out_normal = (p - sphere.origin) / sphere.radius # quicker way to normalise
front_face = r.direction ⋅ out_normal < 0
normal = front_face ? out_normal : -out_normal
return HitRecord(p, normal, root, front_face, sphere)
end