diff options
author | sanine-a <sanine.not@pm.me> | 2020-11-07 13:14:30 -0600 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-11-07 13:14:30 -0600 |
commit | 94572182ea873d5dadbc8428330d4c00eead2c80 (patch) | |
tree | b9152a7d474c236c29d42e133300b8513690bfef /demo/Node.lua | |
parent | 8bb877ad57e7e41d928408e81f4e8cca63e171f7 (diff) |
add basic Node transform functions and implement transform cascading
Diffstat (limited to 'demo/Node.lua')
-rw-r--r-- | demo/Node.lua | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/demo/Node.lua b/demo/Node.lua index 46c372a..08916dd 100644 --- a/demo/Node.lua +++ b/demo/Node.lua @@ -4,6 +4,7 @@ local Matrix = require('Matrix') local Node = {} Node.prototype = {} + Node.prototype.updateTransform = function(self) honey.cglm.mat4.identity(self.transform.array) @@ -15,11 +16,31 @@ Node.prototype.updateTransform = function(self) self.transform:translate(self.position) - if parent ~= nil then - self.transform:mul(self.parent.transform) + if self.parent ~= nil then + self.transform:mul(self.parent.transform, self.transform) + end + + for _, child in ipairs(self.children) do + child:updateTransform() end end +Node.prototype.translate = function(self, translation) + self.position:add(translation, self.position) +end + +Node.prototype.pitch = function(self, angle) + self.rotation:setAt(0, angle) +end + +Node.prototype.yaw = function(self, angle) + self.rotation:setAt(1, angle) +end + +Node.prototype.roll = function(self, angle) + self.rotation:setAt(2, angle) +end + -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Node.mt = {} @@ -37,6 +58,13 @@ Node.new = function(parent, position, rotation, scale) node.position = position node.rotation = rotation node.scale = scale + + node.children = {} + + if parent ~= nil then + local index = #parent.children + parent.children[index + 1] = node + end node.transform = Matrix.Mat4.eye() node:updateTransform() |