summaryrefslogtreecommitdiff
path: root/honey
diff options
context:
space:
mode:
Diffstat (limited to 'honey')
-rw-r--r--honey/ecs/render.lua2
-rw-r--r--honey/mesh.lua46
-rw-r--r--honey/window.lua15
3 files changed, 54 insertions, 9 deletions
diff --git a/honey/ecs/render.lua b/honey/ecs/render.lua
index 4217422..abeac04 100644
--- a/honey/ecs/render.lua
+++ b/honey/ecs/render.lua
@@ -72,7 +72,7 @@ system = function(params)
)
-- get mesh
local mesh = honey.mesh.loadCached(
- tbl.mesh.filename, tbl.mesh.index
+ tbl.mesh.filename, tbl.mesh.index, tbl.mesh.debug
)
draw(model, view, projection, tbl.textures, shader, mesh)
end
diff --git a/honey/mesh.lua b/honey/mesh.lua
index e9a3404..12edea9 100644
--- a/honey/mesh.lua
+++ b/honey/mesh.lua
@@ -4,25 +4,53 @@ setmetatable(module, {__index=_G})
setfenv(1, module)
-local function insertVertex(vertices, attrib, vertex)
+local function printVertex(vertices, i)
+ print(string.format(
+ "p(%f, %f, %f), n(%f, %f, %f), t(%f, %f)",
+ vertices[i+0],
+ vertices[i+1],
+ vertices[i+2],
+
+
+ vertices[i+3],
+ vertices[i+4],
+ vertices[i+5],
+
+
+ vertices[i+5],
+ vertices[i+6]
+ ))
+end
+
+
+local function insertVertex(vertices, attrib, vertex, debug)
+ if debug then print() end
local pos = 3*vertex.v_idx
for i=1,3 do
table.insert(vertices, attrib.vertices[pos+i])
+ if debug then print(vertices[#vertices]) end
end
local normal = 3*vertex.vn_idx
for i=1,3 do
table.insert(vertices, attrib.normals[normal+i])
+ if debug then print(vertices[#vertices]) end
end
- local tex = 3*vertex.vt_idx
+ local tex = 2*vertex.vt_idx
for i=1,2 do
table.insert(vertices, attrib.texcoords[tex+i])
+ if debug then print(vertices[#vertices]) end
+ end
+ if debug then
+ for i=1,#attrib.texcoords do
+ print(i, attrib.texcoords[i])
+ end
end
end
-function loadShape(shape, attrib)
+function loadShape(shape, attrib, debug)
local vertices = {}
local indices = {}
@@ -32,7 +60,7 @@ function loadShape(shape, attrib)
assert(attrib.face_num_verts[i+1] == 3, "non-triangular face!")
for j=0,2 do
local vertex = attrib.faces[(3*i) + j + 1]
- insertVertex(vertices, attrib, vertex)
+ insertVertex(vertices, attrib, vertex, debug)
table.insert(indices, #indices)
end
end
@@ -41,23 +69,25 @@ function loadShape(shape, attrib)
end
-function loadFile(filename)
+function loadFile(filename, debug)
local flags = honey.tinyobj.FLAG_TRIANGULATE
+ if debug then print("load file:", filename) end
local attrib, shapes, materials = honey.tinyobj.parse_obj(filename, flags)
local meshes = {}
for _, shape in ipairs(shapes) do
- local vertices, indices = loadShape(shape, attrib)
+ local vertices, indices = loadShape(shape, attrib, debug)
table.insert(meshes, Mesh(vertices, indices))
end
+ if debug then print("finished file:", filename) end
return meshes
end
cache = {}
-function loadCached(filename, index)
+function loadCached(filename, index, debug)
if not cache[filename] then
- cache[filename] = loadFile(filename)
+ cache[filename] = loadFile(filename, debug)
end
return cache[filename][index]
end
diff --git a/honey/window.lua b/honey/window.lua
index 3b5d5c1..e2b9fb2 100644
--- a/honey/window.lua
+++ b/honey/window.lua
@@ -1,3 +1,5 @@
+local ecs = require 'honey.ecs'
+
local module = {}
local glfw = honey.glfw
setmetatable(module, {__index=_G})
@@ -165,4 +167,17 @@ function Window.swapBuffers(self)
end
+function Window.bindEvents(self, db)
+ self:setCursorPosCallback(function(_, xpos, ypos)
+ ecs.script.dispatch(db, "onCursorPos", {window=self, xpos=xpos, ypos=ypos})
+ end)
+ self:setKeyCallback(function(_, key, scancode, action)
+ ecs.script.dispatch(db, "onKey", {window=self, key=key, scancode=scancode, action=action})
+ end)
+ self:setFramebufferSizeCallback(function(_, width, height)
+ ecs.script.dispatch(db, "onFramebufferSize", {window=self, width=width, height=height})
+ end)
+end
+
+
return module.Window