diff options
author | sanine <sanine.not@pm.me> | 2023-03-29 00:01:54 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-03-29 00:01:54 -0500 |
commit | 5a93fb6f6a8fb6f1dad821b407b4f4531bc23783 (patch) | |
tree | d7b7fb60868e2381bdd14efa357188ba3a27b804 /main.lua | |
parent | 45dbe47d17303050cbea7c2c51e838acfe21c2fb (diff) |
add image caching
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 59 |
1 files changed, 36 insertions, 23 deletions
@@ -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 }, }) |