Skip to content

Commit

Permalink
Merge pull request #88 from danlooo/danlooo/fix-obj-uvw
Browse files Browse the repository at this point in the history
Fix uvw coordinates in obj files
  • Loading branch information
SimonDanisch authored Feb 13, 2024
2 parents d6040f3 + ada98fc commit e2d9f88
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/io/obj.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
Expand All @@ -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
Expand Down Expand Up @@ -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), " "))
Expand Down
12 changes: 10 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/testfiles/cube_uv.obj
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions test/testfiles/cube_uvw.obj
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e2d9f88

Please sign in to comment.