diff options
author | sanine <sanine.not@pm.me> | 2023-04-19 23:49:38 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-19 23:49:38 -0500 |
commit | 445fc5217859a799e3a977c9763141d9a5f6cbb5 (patch) | |
tree | 9fe30aa97fe5d44b1f6fe0f85fe62543f5c2db4b /honey/ecs-systems.lua | |
parent | 02c6f822e2cca41d5d28afd9f3a05211316587fd (diff) |
add split drawing function and renderQuad component
Diffstat (limited to 'honey/ecs-systems.lua')
-rw-r--r-- | honey/ecs-systems.lua | 94 |
1 files changed, 59 insertions, 35 deletions
diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index c287d06..e400011 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -80,13 +80,46 @@ end --===== rendering =====-- +function draw(model, view, projection, textures, shader, mesh) + shader:use() + + -- bind textures + local texOffset = 0 + for name, texTbl in pairs(textures or {}) 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=view, + projection=projection, + model=model, + }, + } + + -- draw mesh + mesh:drawElements() + + -- unbind textures + for i=0,texOffset-1 do + gl.BindTexture(gl.TEXTURE_2D + i, 0) + end +end + function renderCamera(params) return { - camera = params.camera, db = params.db, priority = params.priority or 99, update = function(self, dt) for id, camera in pairs(self.db:queryComponent("camera")) do + local projection = camera.projection local cameraTransform = self.db:getComponent(id, "transform") local view if cameraTransform then @@ -97,41 +130,32 @@ function renderCamera(params) local entities = self.db:queryComponent("renderMesh") for entity, tbl in pairs(entities) do + -- get model + local transform = self.db:getComponent(entity, "transform") + local model = + (transform and transform._matrix) or + 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 or {}) 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 - local query = self.db:getComponent(entity, "transform") - local model = (query and query._matrix) or Mat4():identity() - shader:configure{ - float={ - time=glfw.GetTime(), - }, - matrix={ - view=view, - projection=camera.projection, - model=model, - }, - } - - -- 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 + local shader = honey.shader.loadShader( + tbl.shader.vertex, tbl.shader.fragment + ) + -- get mesh + local mesh = honey.mesh.loadCached( + tbl.mesh.filename, tbl.mesh.index + ) + draw(model, view, projection, tbl.textures, shader, mesh) + end + + entities = self.db:queryComponent("renderQuad") + local quadmesh = honey.mesh.loadCached("builtin.quad", 1) + for entity, tbl in pairs(entities) do + -- get model + local model = Mat4():identity() + -- get shader + local shader = honey.shader.loadShader( + tbl.shader.vertex, tbl.shader.fragment + ) + draw(model, view, projection, tbl.textures, shader, quadmesh) end end end, |