diff --git a/src/io/obj.jl b/src/io/obj.jl index a427777..0ebf193 100644 --- a/src/io/obj.jl +++ b/src/io/obj.jl @@ -5,7 +5,7 @@ ############################## function load(io::Stream{format"OBJ"}; facetype=GLTriangleFace, - pointtype=Point3f, normaltype=Vec3f, uvtype=Vec2f) + pointtype=Point3f, normaltype=Vec3f, uvtype=Any) points, v_normals, uv, faces = pointtype[], normaltype[], uvtype[], facetype[] f_uv_n_faces = (faces, facetype[], facetype[]) @@ -26,9 +26,17 @@ function load(io::Stream{format"OBJ"}; facetype=GLTriangleFace, push!(v_normals, normaltype(parse.(eltype(normaltype), lines))) elseif "vt" == command if length(lines) == 2 - push!(uv, Vec{2, eltype(uvtype)}(parse.(eltype(uvtype), lines))) + if uvtype == Any + uvtype = Vec2f + uv = uvtype[] + end + push!(uv, Vec{2,eltype(uvtype)}(parse.(eltype(uvtype), lines))) elseif length(lines) == 3 - push!(uv, Vec{3, eltype(uvtype)}(parse.(eltype(uvtype), lines))) + if uvtype == Any + uvtype = Vec3f + uv = uvtype[] + end + push!(uv, Vec{3,eltype(uvtype)}(parse.(eltype(uvtype), lines))) else error("Unknown UVW coordinate: $lines") end @@ -155,7 +163,7 @@ function save(f::Stream{format"OBJ"}, mesh::AbstractMesh) println(io, "vn ", n[1], " ", n[2], " ", n[3]) end end - + F = eltype(faces(mesh)) for f in decompose(F, mesh) println(io, "f ", join(convert.(Int, f), " ")) diff --git a/test/runtests.jl b/test/runtests.jl index 89103fd..d9a7edd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -14,7 +14,7 @@ function test_face_indices(mesh) end @testset "MeshIO" begin - dirlen = 1f0 + dirlen = 1.0f0 baselen = 0.02f0 mesh = [ Rect3f(Vec3f(baselen), Vec3f(dirlen, baselen, baselen)), @@ -104,7 +104,7 @@ end @test test_face_indices(msh) @test length(coordinates(msh)) == 72 - msh = load(joinpath(tf, "binary.ply")) + msh = load(joinpath(tf, "binary.ply")) @test length(faces(msh)) == 36 @test test_face_indices(msh) @test length(coordinates(msh)) == 72 @@ -142,6 +142,14 @@ end @test length(coordinates(msh)) == 8 @test test_face_indices(msh) + msh = load(joinpath(tf, "cube_uv.obj")) + @test typeof(msh.uv) == Vector{Vec{2,Float32}} + @test length(msh.uv) == 8 + + msh = load(joinpath(tf, "cube_uvw.obj")) + @test typeof(msh.uv) == Vector{Vec{3,Float32}} + @test length(msh.uv) == 8 + msh = load(joinpath(tf, "polygonal_face.obj")) @test length(faces(msh)) == 4 @test length(coordinates(msh)) == 6 diff --git a/test/testfiles/cube_uv.obj b/test/testfiles/cube_uv.obj new file mode 100644 index 0000000..9d061c2 --- /dev/null +++ b/test/testfiles/cube_uv.obj @@ -0,0 +1,28 @@ +# Blender v2.71 (sub 0) OBJ File: '' +# www.blender.org +mtllib cube.mtl +o Cube +v 1.000000 -1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -0.999999 +v 0.999999 1.000000 1.000001 +v -1.000000 1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +vt 0 0.0 +vt 0 0.1 +vt 0 0.2 +vt 0 0.3 +vt 0 0.4 +vt 0 0.5 +vt 0 0.6 +vt 0 0.7 +usemtl Material +s off +f 1 2 3 4 +f 5 8 7 6 +f 1 5 6 2 +f 2 6 7 3 +f 3 7 8 4 +f 5 1 4 8 diff --git a/test/testfiles/cube_uvw.obj b/test/testfiles/cube_uvw.obj new file mode 100644 index 0000000..e71298f --- /dev/null +++ b/test/testfiles/cube_uvw.obj @@ -0,0 +1,28 @@ +# Blender v2.71 (sub 0) OBJ File: '' +# www.blender.org +mtllib cube.mtl +o Cube +v 1.000000 -1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -0.999999 +v 0.999999 1.000000 1.000001 +v -1.000000 1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +vt 0 0 0 +vt 1 0 0 +vt 0 1 0 +vt 0 0 1 +vt 1 1 0 +vt 0 1 1 +vt 1 0 1 +vt 1 1 1 +usemtl Material +s off +f 1 2 3 4 +f 5 8 7 6 +f 1 5 6 2 +f 2 6 7 3 +f 3 7 8 4 +f 5 1 4 8