diff options
Diffstat (limited to 'honey')
-rw-r--r-- | honey/ecs-systems.lua | 94 | ||||
-rw-r--r-- | honey/init.lua | 1 | ||||
-rw-r--r-- | honey/mesh.lua | 23 |
3 files changed, 78 insertions, 40 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, diff --git a/honey/init.lua b/honey/init.lua index 6cf8801..d2f5279 100644 --- a/honey/init.lua +++ b/honey/init.lua @@ -22,6 +22,7 @@ function init(width, height, title) gl.Enable(gl.DEPTH_TEST) honey.ode.InitODE() + honey.mesh.createBuiltins() return window end diff --git a/honey/mesh.lua b/honey/mesh.lua index e46c399..e9a3404 100644 --- a/honey/mesh.lua +++ b/honey/mesh.lua @@ -54,12 +54,12 @@ function loadFile(filename) end -local meshCache = {} -function loadMesh(filename, index) - if not meshCache[filename] then - meshCache[filename] = loadFile(filename) +cache = {} +function loadCached(filename, index) + if not cache[filename] then + cache[filename] = loadFile(filename) end - return meshCache[filename][index] + return cache[filename][index] end @@ -101,4 +101,17 @@ function Mesh.drawElements(self) end +--===== builtin meshes =====-- + +function createBuiltins() + cache["builtin.quad"] = {Mesh( + { 0, 0, 0, 0, 1, 0, 0, 0, + 1, 0, 0, 0, 1, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, + 1, 1, 0, 0, 1, 0, 1, 1 }, + { 0, 1, 3, 0, 3, 2 } + )} +end + + return module |