-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhittable.lua
More file actions
75 lines (65 loc) · 1.8 KB
/
hittable.lua
File metadata and controls
75 lines (65 loc) · 1.8 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function HittableList()
local hittablelist = {}
hittablelist.list = {}
hittablelist.add = function(object)
table.insert(hittablelist.list, object)
end
hittablelist.clear = function ()
hittablelist.list = {}
end
hittablelist.hit = function (ray, interval)
local temporary_memory = HitMemory()
local hit_anything = false
local closest_so_far = interval.max
for object=1,#hittablelist.list do
local hit, temporary_memory = hittablelist.list[object].hit(ray, Interval(interval.min, closest_so_far), temporary_memory)
if hit then
hit_anything = true
closest_so_far = temporary_memory.t
end
end
return hit_anything, temporary_memory
end
return hittablelist
end
-- returns a template for hittable objects
function ReturnHittable()
local hittable = {}
return hittable
end
function HitMemory(point, vector, dist, front_face, material)
local memory = {}
if point ~= nil then
memory.p = point
else
memory.p = nil
end
if vector ~= nil then
memory.normal = vector
else
memory.normal = nil
end
if dist ~= nil then
memory.t = dist
else
memory.t = nil
end
if front_face ~= nil then
memory.front_face = front_face
else
memory.front_face = nil
end
if material ~= nil then
memory.material = material
else
memory.material = nil
end
memory.set_face_normal = function (ray, outward_normal)
memory.front_face = ray.direction.dot(outward_normal) < 0
if not memory.front_face then
outward_normal = outward_normal.negative()
end
memory.normal = outward_normal
end
return memory
end