summaryrefslogtreecommitdiff
path: root/honey/mesh.lua
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-05-09 14:46:08 -0500
committersanine-a <sanine.not@pm.me>2023-05-09 14:46:08 -0500
commit9c9690ab5e4ecb484ee130c21f1767785908bd10 (patch)
tree847d7868a25936b1e632caac2b581b2c90d0eee0 /honey/mesh.lua
parentfe6564d8c31662d4d2c2606c62ba6d4416c6b6a6 (diff)
fix texture loading
Diffstat (limited to 'honey/mesh.lua')
-rw-r--r--honey/mesh.lua46
1 files changed, 38 insertions, 8 deletions
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