From 94572182ea873d5dadbc8428330d4c00eead2c80 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Sat, 7 Nov 2020 13:14:30 -0600 Subject: add basic Node transform functions and implement transform cascading --- demo/Node.lua | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'demo/Node.lua') 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() -- cgit v1.2.1