I'm reading a .obj file and converting it into triangles. Here's the code that is relevant.
Note I'm using Middleclass
Code: Select all
local vec3d = class("vec3d")
function vec3d:initialize(x, y, z)
self.x, self.y, self.z = x, y, z
end
local triangle = class("triangle")
function triangle:initialize(x1, y1, z1, x2, y2, z2, x3, y3, z3)
self.p = {vec3d:new(x1, y1, z1), vec3d:new(x2, y2, z2), vec3d(x3, y3, z3)} --Creates a triangle with 3 points
end
local mesh = class("mesh")
function mesh:initialize()
self.tris = {}
self.LoadFromObjectFile = function(filename)
local f = love.filesystem.newFile(filename)
if f == nil then return false end
--Local cache of verts
local verts = {}
--This differs quite a lot from the original code becaue it is very c++ specific
for line in f:lines() do
local words = {}
for word in line:gmatch("%S+") do table.insert(words, word) end
if words[1] == "v" then
local v = vec3d:new(words[2], words[3], words[4])
table.insert(verts, v)
end
if words[1] == "f" then
local f1 = {1, 1, 1} -- the c++ line is "int f[3]; -- How does this make sense as the ifstream is also called f?"
f1[1], f1[2], f1[3] = words[2], words[3], words[4]
local x1 = verts[f1[1] - 1].x -- This is where the problem is
table.insert(self.tris, triangle:new(verts[f1[1] - 1].x, verts[f1[2] - 1], verts[f1[3] - 1])) -- This line is not finished ignore it
end
end
return true
end
end
Code: Select all
local x1 = verts[f1[1] - 1].x -- This is where the problem is