/
sphere.jl
53 lines (43 loc) · 1.28 KB
/
sphere.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
export Sphere
# ---------- #
# - Sphere - #
# ---------- #
"""
Sphere
Sphere is a primitive object.
### Fields:
* `center` - Center of the Sphere in 3D world space
* `radius` - Radius of the Sphere
* `material` - Material of the Sphere
"""
struct Sphere{C, P, Q, R, S, T, U} <: Object
center::Vec3{C}
radius::C
material::Material{P, Q, R, S, T, U}
end
show(io::IO, s::Sphere) =
print(io, "Sphere Object:\n Center - ", s.center, "\n Radius - ", s.radius[],
"\n ", s.material)
@diffops Sphere
function intersect(s::Sphere, origin, direction)
b = dot(direction, origin - s.center) # direction is a vec3 with array
c = l2norm(s.center) .+ l2norm(origin) .- 2 .* dot(s.center, origin) .- (s.radius .^ 2)
disc = (b .^ 2) .- c
function get_intersections(x, y)
t = bigmul(x + y) # Hack to split the 0.0 gradient to both. Otherwise one gets nothing
if y > 0
sqrty = sqrt(y)
z1 = -x - sqrty
z2 = -x + sqrty
if z1 <= 0 && z2 > 0
t = z2
elseif z1 > 0
t = z1
end
end
return t
end
result = broadcast(get_intersections, b, disc)
return result
end
get_normal(s::Sphere, pt, dir) = normalize(pt - s.center)