Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function decode_obj(data)
- local vertices = {}
- local normals = {}
- local uvs = {}
- local function insert_triangle(output, indexes)
- local triangle = {}
- local vertex_i, uv_i, normal_i = indexes:match("(%d+)/(%d+)/(%d+)")
- vertex_i = tonumber(vertex_i)
- uv_i = tonumber(uv_i)
- normal_i = tonumber(normal_i)
- if vertex_i then
- triangle.pos = vertices[vertex_i]
- end
- if uv_i then
- triangle.uv = uvs[uv_i]
- end
- if normal_i then
- triangle.normal = normals[normal_i]
- end
- table.insert(output, triangle)
- end
- -- get all the types
- for type, x, y, z in data:gmatch("(.-)%s+(.-)%s+(.-)%s+(.-)\n") do
- if type == "v" then
- table.insert(vertices, Vec3(tonumber(x),tonumber(y),tonumber(z)))
- end
- if type == "vn" then
- table.insert(normals, Vec3(tonumber(x),tonumber(y),tonumber(z)))
- end
- if type == "vt" then
- table.insert(uvs, Vec2(tonumber(x),tonumber(y)))
- end
- end
- local output = {}
- -- assemble them
- for type, a, b, c, d in data:gmatch("(.-)%s+(.-)%s+(.-)%s+(.-)\n") do
- if type == "f" then
- if a and b and c then
- insert_triangle(output, a)
- insert_triangle(output, c)
- insert_triangle(output, b)
- elseif a and b and c and d then
- error("quad is not implemented!", 2)
- end
- end
- end
- return output
- end
Advertisement
Add Comment
Please, Sign In to add comment