summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-03-19 01:17:48 -0500
committersanine <sanine.not@pm.me>2023-03-19 01:17:48 -0500
commit05009088588e6171dd26c3002208e338e2c5a65a (patch)
treed3bb7afa0a7388b8a0ac2219c06e32fbd8f6e47e
parentd94d9fb122e42264eca20bb037fe8a82290bd3e2 (diff)
add shader:configure()
-rw-r--r--honey/shader.lua35
-rw-r--r--main.lua75
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
diff --git a/main.lua b/main.lua
index 9be332b..c9b1733 100644
--- a/main.lua
+++ b/main.lua
@@ -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)