summaryrefslogtreecommitdiff
path: root/demo/Node.lua
blob: 46c372a81ba738f69bbdf60318e42c5b7afe1ed5 (plain)
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
local Vector = require('Vector')
local Matrix = require('Matrix')

local Node = {}

Node.prototype = {}
Node.prototype.updateTransform = function(self)
   honey.cglm.mat4.identity(self.transform.array)

   self.transform:scale(self.scale)

   self.transform:rotate(Vector.Vec3.ZERO, Vector.Vec3.Z_UNIT, self.rotation:at(2))
   self.transform:rotate(Vector.Vec3.ZERO, Vector.Vec3.Y_UNIT, self.rotation:at(1))
   self.transform:rotate(Vector.Vec3.ZERO, Vector.Vec3.X_UNIT, self.rotation:at(0))

   self.transform:translate(self.position)

   if parent ~= nil then
      self.transform:mul(self.parent.transform)
   end
end

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Node.mt = {}
Node.mt.__index = Node.prototype

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Node.new = function(parent, position, rotation, scale)
   local node = {}

   setmetatable(node, Node.mt)

   node.parent = parent

   node.position = position
   node.rotation = rotation
   node.scale = scale
   
   node.transform = Matrix.Mat4.eye()
   node:updateTransform()

   return node
end

return Node