-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshapegen.lua
More file actions
45 lines (36 loc) · 1.33 KB
/
shapegen.lua
File metadata and controls
45 lines (36 loc) · 1.33 KB
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
function Sphere(center, radius, material)
local sphere = ReturnHittable()
sphere.radius = math.max(0,radius)
sphere.center = center
if material ~= nil then
sphere.material = material
else
sphere.material = Lambertian(instancepoint3(0.5,0.5,0.5))
end
sphere.hit = function (ray, interval, hitmemory)
--ray.origin.printinfo()
local sphere_centre_vector = sphere.center.subvec(ray.origin)
local a = ray.direction.lensq()
local h = ray.direction.dot(sphere_centre_vector)
local c = sphere_centre_vector.lensq() - sphere.radius*sphere.radius
local discriminant = h*h - a*c
if discriminant < 0 then
return false, hitmemory
end
local sqrtd = math.sqrt(discriminant)
local root = (h-sqrtd)/a
if not interval.surrounds(root) then
root = (h+sqrtd)/a
if not interval.surrounds(root) then
return false, hitmemory
end
end
hitmemory.t = root
hitmemory.p = ray.at(hitmemory.t)
local outward_normal = hitmemory.p.subvec(sphere.center).divbynum(sphere.radius)
hitmemory.set_face_normal(ray, outward_normal)
hitmemory.material = sphere.material
return true, hitmemory
end
return sphere
end