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 | |
parent | 02c6f822e2cca41d5d28afd9f3a05211316587fd (diff) |
add split drawing function and renderQuad component
-rw-r--r-- | honey/ecs-systems.lua | 94 | ||||
-rw-r--r-- | honey/init.lua | 1 | ||||
-rw-r--r-- | honey/mesh.lua | 23 | ||||
-rw-r--r-- | main.lua | 12 | ||||
-rw-r--r-- | save | 8 |
5 files changed, 93 insertions, 45 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 @@ -65,6 +65,7 @@ function setupEntities() entities:addComponents(camera, { camera={ projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), + render="screen", }, transform={ matrix=Mat4():identity():translate(Vec3{0, 0, -6}), @@ -126,13 +127,22 @@ function setupEntities() entities:addComponents(id2, { renderMesh = { shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/tetrahedron.obj", index=1 }, + --mesh = { filename="assets/tetrahedron.obj", index=1 }, + mesh = { filename="builtin.quad", index=1 }, }, transform = { parent=id, matrix=Mat4():identity():translate(Vec3{0, 2, 0}), }, }) + + local quad = entities:createEntity() + entities:addComponents(quad, { + renderQuad = { + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + textures = { ourTexture = { filename = "44d9a0ec1c18e6126a5e9d9d9317f5ac.png" } }, + }, + }) end setupEntities() @@ -1,4 +1,4 @@ -Entity("252e3aec-c36d-4e59-a5b9-73872a63656b", {transform={parent="f451df85-d8d1-4b72-abf5-f384f2ffa730",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 2.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={mesh={index=1,filename="assets/tetrahedron.obj"},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) -Entity("4f4191ff-f1b7-4382-904b-156b07091850", {onKey={script="cameraKeyHandler"},transform={matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, -59.1379, 0.0000, 0.0000, 0.0000, 1.0000}},script={script="cameraRotationScript"},camera={projection=Mat4{2.5518, 0.0000, 0.0000, 0.0000, 0.0000, 2.4142, 0.0000, 0.0000, 0.0000, 0.0000, -1.0020, -0.2002, 0.0000, 0.0000, -1.0000, 0.0000}},z={value=-60},onWindowResize={script="cameraHandleResize"}}) -Entity("268bfa52-f18b-4d23-bd02-7f3366ae9da3", {collision={class="plane"},transform={matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}}}) -Entity("f451df85-d8d1-4b72-abf5-f384f2ffa730", {transform={matrix=Mat4{0.6164, -0.6164, 0.4899, -0.0000, 0.7071, 0.7071, 0.0000, 4.8477, -0.3464, 0.3464, 0.8718, -0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},collision={radius=1,class="sphere"},physics={angularVelocity=Vec3{0.0000, 1.0000, 0.0000},mass={radius=1,class="sphere",density=1},velocity=Vec3{-0.0000, 6.9881, -0.0000}},renderMesh={mesh={index=1,filename="assets/icosahedron.obj"},textures={ourTexture={filename="77155.png"}},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) +Entity("db523689-0ef0-4dd9-8847-7f2eb72409f1", {transform={parent="e928ddf1-98eb-49a6-9b22-fb815edef06f",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 2.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={mesh={index=1,filename="assets/tetrahedron.obj"},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) +Entity("e928ddf1-98eb-49a6-9b22-fb815edef06f", {transform={matrix=Mat4{0.0297, 0.9996, 0.0000, -10.2024, -0.9996, 0.0297, 0.0000, 0.1079, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},collision={radius=1,class="sphere"},physics={angularVelocity=Vec3{0.0000, 0.0000, 3.5659},mass={radius=1,class="sphere",density=1},velocity=Vec3{-3.5523, -0.3108, 0.0000}},renderMesh={mesh={index=1,filename="assets/icosahedron.obj"},textures={ourTexture={filename="77155.png"}},shader={vertex="vertex.glsl",fragment="fragment.glsl"}}}) +Entity("badbb36e-27ae-40bd-8601-13f90d6ad936", {onKey={script="cameraKeyHandler"},transform={matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, -59.7493, 0.0000, 0.0000, 0.0000, 1.0000}},script={script="cameraRotationScript"},camera={projection=Mat4{1.8107, 0.0000, 0.0000, 0.0000, 0.0000, 2.4142, 0.0000, 0.0000, 0.0000, 0.0000, -1.0020, -0.2002, 0.0000, 0.0000, -1.0000, 0.0000}},z={value=-60},onWindowResize={script="cameraHandleResize"}}) +Entity("862b003d-99f6-4dfa-8319-f11477bb4bba", {collision={class="plane"},transform={matrix=Mat4{0.9962, -0.0872, 0.0000, 0.0000, 0.0872, 0.9962, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}}}) |