diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | honey/ecs-systems.lua | 75 | ||||
-rw-r--r-- | honey/ecs.lua | 12 | ||||
-rw-r--r-- | honey/mat4.lua | 16 | ||||
-rw-r--r-- | honey/std.lua | 13 | ||||
-rw-r--r-- | main.lua | 76 |
6 files changed, 116 insertions, 79 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8638800 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.
+*~
+*.swp
diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua new file mode 100644 index 0000000..a511b3d --- /dev/null +++ b/honey/ecs-systems.lua @@ -0,0 +1,75 @@ +local ecs = require 'honey.ecs'
+
+
+local module = {}
+setmetatable(module, {__index=_G})
+setfenv(1, module)
+
+
+
+--===== transform cascading =====--
+
+local function recursiveComputeTransform(entity)
+ if entity._transformComputed then
+ return entity._transform
+ end
+ if entity.parent == false then
+ entity._transformComputed = true
+ entity._transform = entity.transform
+ return entity.transform
+ end
+
+ entity._transformComputed = true
+ local parentTransform = recursiveComputeTransform(entity.parent)
+ entity._transform = parentTransform * entity.transform
+ return entity._transform
+end
+
+-- update transforms
+transformCascade = {
+ filter=ecs.Filter.AND{"transform", "parent"},
+ preUpdate=function(entity)
+ entity._transform = nil
+ entity._transformComputed = false
+ end,
+ update=function(entity, dt)
+ recursiveComputeTransform(entity)
+ end,
+ priority=98,
+}
+
+
+--===== rendering =====--
+
+function renderCam(camera, priority)
+ local priority = priority or 99
+ return {
+ filter=ecs.Filter.AND{"mesh", "shader", "transform"},
+ update=function(entity, dt)
+ entity.shader:use()
+ entity.shader:configure{
+ matrix={
+ model=entity._transform,
+ view=camera.view,
+ projection=camera.projection,
+ },
+ }
+ entity.mesh:drawElements()
+ end,
+ priority=priority,
+ }
+end
+
+--===== update functions =====--
+
+update = {
+ filter=ecs.Filter.AND{"update"},
+ update=function(entity, dt)
+ entity.update(entity, dt)
+ end,
+ priority=50,
+}
+
+
+
+return module
diff --git a/honey/ecs.lua b/honey/ecs.lua index 4eee343..23bb0ba 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -194,10 +194,22 @@ end function Level.update(self, dt) for _, system in ipairs(self.systems) do + if system.preUpdate then + for id in pairs(system.entities) do + local entity = self.entities[id] + system.preUpdate(entity) + end + end for id in pairs(system.entities) do local entity = self.entities[id] system.update(entity, dt) end + if system.postUpdate then + for id in pairs(system.entities) do + local entity = self.entities[id] + system.postUpdate(entity) + end + end end end diff --git a/honey/mat4.lua b/honey/mat4.lua index 8ea73d4..6ea17fd 100644 --- a/honey/mat4.lua +++ b/honey/mat4.lua @@ -57,13 +57,6 @@ end setmetatable(Mat4, {__call=Mat4.new}) -function Mat4.Identity() - local m = Mat4() - m:identity() - return m -end - - function Mat4.__index(self, key) if type(key) == "number" then return RowLookup(key, self.data) @@ -111,40 +104,49 @@ end function Mat4.identity(self) glm.mat4_identity(self.data) + return self end function Mat4.zero(self) glm.mat4_zero(self.data) + return self end function Mat4.translate(self, vec) glm.translate(self.data, vec.data) + return self end function Mat4.rotateX(self, angle) glm.rotate_x(self.data, angle, self.data) + return self end function Mat4.rotateY(self, angle) glm.rotate_y(self.data, angle, self.data) + return self end function Mat4.rotateZ(self, angle) glm.rotate_z(self.data, angle, self.data) + return self end function Mat4.scale(self, vec) glm.scale(self.data, vec.data) + return self end function Mat4.perspective(self, fovy, aspect, near, far) glm.perspective(fovy, aspect, near, far, self.data) + return self end function Mat4.perspectiveResize(self, aspect) glm.perspective_resize(aspect, self.data) + return self end diff --git a/honey/std.lua b/honey/std.lua index 752df4d..028a2a8 100644 --- a/honey/std.lua +++ b/honey/std.lua @@ -3,9 +3,10 @@ local init = require 'honey.init' honey.init = init.init honey.terminate = init.terminate -honey.ecs = require 'honey.ecs' -honey.Mat4 = require 'honey.mat4' -honey.mesh = require 'honey.mesh' -honey.Shader = require 'honey.shader' -honey.Vec3 = require 'honey.vec3' -honey.Window = require 'honey.window' +honey.ecs = require 'honey.ecs' +honey.standardSystems = require 'honey.ecs-systems' +honey.Mat4 = require 'honey.mat4' +honey.mesh = require 'honey.mesh' +honey.Shader = require 'honey.shader' +honey.Vec3 = require 'honey.vec3' +honey.Window = require 'honey.window' @@ -6,77 +6,21 @@ local gl = honey.gl local Vec3 = honey.Vec3 local Mat4 = honey.Mat4 local ecs = honey.ecs +local systems = honey.standardSystems -- camera matrices local camera = { - view=Mat4.Identity(), - projection=Mat4(), + view=Mat4():identity():translate(Vec3{0, 0, -60}), + projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), } -camera.view:translate(Vec3{0, 0, -60}) -camera.projection:perspective(math.rad(45), 640/480, 0.1, 100) -function recursiveComputeTransform(entity) - if entity._transformComputed then - return entity._transform - end - if entity.parent == false then - entity._transformComputed = true - entity._transform = entity.transform - return entity.transform - end - - entity._transformComputed = true - local parentTransform = recursiveComputeTransform(entity.parent) - entity._transform = parentTransform * entity.transform - return entity._transform -end - local level = ecs.Level() --- update transforms -level:addSystem{ - filter=ecs.Filter.AND{"transform", "parent"}, - update=function(entity, dt) - recursiveComputeTransform(entity) - end, - priority=1, -} -level:addSystem{ - filter=ecs.Filter.AND{"transform", "parent"}, - update=function(entity, dt) - entity._transform = nil - entity._transformComputed = false - end, - priority=0, -} - --- render objects -level:addSystem{ - filter=ecs.Filter.AND{"mesh", "shader", "transform"}, - update=function(entity, dt) - entity.shader:use() - entity.shader:configure{ - matrix={ - model=entity._transform, - view=camera.view, - projection=camera.projection, - }, - } - entity.mesh:drawElements() - end, - priority=99, -} - --- run custom scripts -level:addSystem{ - filter=ecs.Filter.AND{"update"}, - update=function(entity, dt) - entity.update(entity, dt) - end, - priority=50, -} +level:addSystem(systems.transformCascade) +level:addSystem(systems.renderCam(camera)) +level:addSystem(systems.update) @@ -149,12 +93,12 @@ function growLine(prev, depth) if depth == 0 then return prev end local entity = { - transform=Mat4.Identity(), + transform=Mat4():identity():translate(Vec3{2, 0, 0}), + parent=false, mesh=octa, shader=shader, update=updateTransform, } - entity.transform:translate(Vec3{2, 0, 0}) prev.parent = entity level:addEntity(prev) return growLine(entity, depth-1) @@ -162,12 +106,12 @@ end local leaf = { - transform=Mat4.Identity(), + transform=Mat4():identity():translate(Vec3{2, 0, 0}), + parent=false, mesh=tetra, shader=shader, } local root = growLine(leaf, 24) -root.parent = false level:addEntity(root) |