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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
local Node = {}
Node.prototype = {}
Node.prototype.updateTransform = function(self)
self.transform:eye()
self.transform:translate(self.position)
self.transform:rotateZ(self.rotation:get(2))
self.transform:rotateY(self.rotation:get(1))
self.transform:rotateX(self.rotation:get(0))
self.transform:scalev(self.scale)
if self.parent ~= nil then
self.parent.transform:mul(self.transform, self.transform)
end
for _, child in ipairs(self.children) do
child:updateTransform()
end
end
Node.prototype.updateCascade = function(self, dt)
if self.update then
self:update(dt)
end
self:updateTransform()
for _, child in ipairs(self.children) do
child:updateCascade(dt)
end
end
Node.prototype.drawCascade = function(self, camera, shader)
if self.draw then
self:draw(camera, shader)
end
-- do not draw base nodes, but recursively draw children.
for _, child in ipairs(self.children) do
child:drawCascade(camera, shader)
end
end
Node.prototype.translate = function(self, translation)
self.position:add(translation, self.position)
end
local rotationAxes = { x=0, y=1, z=2 }
function Node.prototype.rotate(self, axis, angle)
local index = rotationAxes[axis]
local oldAngle = self.rotation:get(index)
self.rotation:set(index, oldAngle + angle)
end
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Node.mt = {}
Node.mt.__index = Node.prototype
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Node.new = function(parent, position, rotation, scale)
local node = {}
setmetatable(node, Node.mt)
node.parent = parent
if not position then node.position = honey.glm.vec3{0,0,0}
else node.position = position end
if not rotation then node.rotation = honey.glm.vec3{0,0,0}
else node.rotation = rotation end
if not scale then node.scale = honey.glm.vec3{1,1,1}
else node.scale = scale end
node.children = {}
if parent ~= nil then
local index = #parent.children
parent.children[index + 1] = node
end
node.transform = honey.glm.mat4()
node.transform:eye()
node:updateTransform()
return node
end
return Node
|