From 5a93fb6f6a8fb6f1dad821b407b4f4531bc23783 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 29 Mar 2023 00:01:54 -0500 Subject: add image caching --- main.lua | 59 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 23 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 098a65b..f13a1a6 100644 --- a/main.lua +++ b/main.lua @@ -22,27 +22,12 @@ local vw, vh = 640, 480 -- create camera matrices local camera = { - view=Mat4():identity():translate(Vec3{0, 0, -30}), + view=Mat4():identity():translate(Vec3{0, 0, -3}), projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), } --- create shader -local shader = honey.Shader{ - vertexFile = "vertex.glsl", - fragmentFile = "fragment.glsl", -} - --- load models -local plane = honey.mesh.loadFile("assets/plane.obj")[1] -local tetra = honey.mesh.loadFile("assets/tetrahedron.obj")[1] -local cube = honey.mesh.loadFile("assets/cube.obj")[1] -local octa = honey.mesh.loadFile("assets/octahedron.obj")[1] -local dodeca = honey.mesh.loadFile("assets/dodecahedron.obj")[1] -local icosa = honey.mesh.loadFile("assets/icosahedron.obj")[1] - - -- close window on ESCAPE key window:setKeyCallback(function(_, key, scancode, action) if action == glfw.PRESS then @@ -82,14 +67,39 @@ sdb:addSystem{ update = function(self, dt) local entities = self.db:queryComponent("renderMesh") for entity, tbl in pairs(entities) do - tbl.shader:use() - tbl.shader:configure{matrix={ - view=camera.view, - projection=camera.projection, - model=Mat4():identity(), - }} + -- get shader + local shader = honey.shader.loadShader(tbl.shader.vertex, tbl.shader.fragment) + shader:use() + + -- bind textures + local texOffset = 0 + for name, texTbl in pairs(tbl.textures) do + local texture = honey.image.loadImage(texTbl.filename, texTbl.params) + gl.BindTexture(gl.TEXTURE_2D + texOffset, texture.texture) + shader:setInt(name, texOffset) + texOffset = texOffset + 1 + end + + -- configure default uniforms + shader:configure{ + float={ + time=glfw.GetTime(), + }, + matrix={ + view=camera.view, + projection=camera.projection, + model=Mat4():identity(), + }, + } + + -- draw mesh local mesh = honey.mesh.loadMesh(tbl.mesh.filename, tbl.mesh.index) mesh:drawElements() + + -- unbind textures + for i=0,texOffset-1 do + gl.BindTexture(gl.TEXTURE_2D + i, 0) + end end end, db = edb, @@ -99,7 +109,10 @@ sdb:addSystem{ local id = edb:createEntity() edb:addComponent(id, "renderMesh", { - shader = shader, + textures = { + ourTexture={ filename="77155.png" }, + }, + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, mesh = { filename="assets/icosahedron.obj", index=1 }, }) -- cgit v1.2.1