diff options
-rw-r--r-- | honey/shader.lua | 35 | ||||
-rw-r--r-- | main.lua | 75 |
2 files changed, 76 insertions, 34 deletions
diff --git a/honey/shader.lua b/honey/shader.lua index 82d7e8a..1fb9f5b 100644 --- a/honey/shader.lua +++ b/honey/shader.lua @@ -59,10 +59,45 @@ function Shader.use(self) end +function Shader.setInt(self, name, value) + local location = self:getLocation(name) + gl.Uniform1i(location, value) +end +function Shader.setFloat(self, name, value) + local location = self:getLocation(name) + gl.Uniform1f(location, value) +end + +function Shader.setVec3(self, name, value) + local location = self:getLocation(name) + gl.Uniform3f(location, value[1], value[2], value[3]) +end +function Shader.setVec4(self, name, value) + local location = self:getLocation(name) + gl.Uniform3f(location, value[1], value[2], value[3], value[4]) +end + function Shader.setMatrix(self, name, matrix) local location = self:getLocation(name) gl.UniformMatrix4fv(location, false, matrix.data) end +function Shader.configure(self, tbl) + local processKey = function(key, set) + local subtbl = tbl[key] + if subtbl then + for name, value in pairs(subtbl) do + self[set](self, name, value) + end + end + end + + processKey("int", "setInt") + processKey("float", "setFloat") + processKey("vec3", "setVec3") + processKey("vec4", "setVec4") + processKey("matrix", "setMatrix") +end + return module.Shader @@ -13,7 +13,7 @@ local camera = { view=Mat4.Identity(), projection=Mat4(), } -camera.view:translate(Vec3{0, 0, -3}) +camera.view:translate(Vec3{0, 0, -60}) camera.projection:perspective(math.rad(45), 640/480, 0.1, 100) @@ -57,9 +57,13 @@ world:addSystem{ filter=ecs.Filter.AND{"mesh", "shader", "transform"}, update=function(entity, dt) entity.shader:use() - entity.shader:setMatrix('model', entity._transform) - entity.shader:setMatrix('view', camera.view) - entity.shader:setMatrix('projection', camera.projection) + entity.shader:configure{ + matrix={ + model=entity._transform, + view=camera.view, + projection=camera.projection, + }, + } entity.mesh:drawElements() end, priority=99, @@ -128,35 +132,47 @@ local dodeca = honey.mesh.loadFile("assets/dodecahedron.obj")[1] local icosa = honey.mesh.loadFile("assets/icosahedron.obj")[1] -local tetraEntity = { - parent=false, - transform=Mat4.Identity(), - mesh=tetra, - shader=shader, -} +function growLine(prev, depth) + if depth == 0 then return prev end -local cubeEntity = { - parent=tetraEntity, - transform=Mat4.Identity(), - mesh=cube, - shader=shader, -} -cubeEntity.transform:translate(Vec3{2, 0, 0}) + local entity = { + transform=Mat4.Identity(), + mesh=octa, + shader=shader, + } + entity.transform:translate(Vec3{2, 0, 0}) + prev.parent = entity + world:addEntity(prev) + return growLine(entity, depth-1) +end -local octaEntity = { - parent=cubeEntity, + +local leaf = { transform=Mat4.Identity(), - mesh=octa, + mesh=tetra, shader=shader, } -octaEntity.transform:translate(Vec3{2, 0, 0}) +local root = growLine(leaf, 24) +root.parent = false +world:addEntity(root) +function updateTransforms(dt) + local entity = leaf + while entity ~= false do + entity.transform:rotateY(0.3 * math.pi * dt) + entity.transform:rotateX(0.1 * math.pi * dt) + entity = entity.parent + end +end + -world:addEntity(tetraEntity) -world:addEntity(cubeEntity) -world:addEntity(octaEntity) +window:setKeyCallback(function(_, key) + if key == glfw.KEY_ESCAPE then + window:setShouldClose(true) + end +end) @@ -166,16 +182,7 @@ while not window:shouldClose() do local dt = time - prevTime prevTime = time - tetraEntity.transform:identity() - tetraEntity.transform:rotateY(0.5 * math.pi * time) - tetraEntity.transform:rotateX(0.05 * math.pi * time) - --model:scale(0.2 * Vec3{1, 1, 1}) - - cubeEntity.transform:identity() - cubeEntity.transform:translate(Vec3{2, 0, 0}) - cubeEntity.transform:rotateY(0.3 * math.pi * time) - cubeEntity.transform:rotateX(0.08 * math.pi * time) - + updateTransforms(dt) gl.ClearColor(0.2, 0.4, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT) |