Skip to content

Commit bc7caed

Browse files
committed
Update obj.jl
updated to handle .obj indices correctly
1 parent 9f50c2a commit bc7caed

File tree

1 file changed

+51
-29
lines changed

1 file changed

+51
-29
lines changed

src/obj.jl

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ struct Mesh
2626
vertices::Vector{Vertex}
2727
normals::Vector{Normal}
2828
texcoords::Vector{TextureCoordinate}
29-
vindices::Vector{SVec{3,UInt32}} # vertex indices
30-
nindices::Vector{SVec{3,UInt32}} # normal indices
31-
tindices::Vector{SVec{3,UInt32}} # texture indices
29+
# vindices::Vector{SVec{3,UInt32}} # vertex indices
30+
# nindices::Vector{SVec{3,UInt32}} # normal indices
31+
# tindices::Vector{SVec{3,UInt32}} # texture indices
3232
end
3333

3434

3535

3636
function loadOBJ(path::String)
3737
names = String[]
38-
vertices = Array{Vertex,1}[]
39-
normals = Array{Normal,1}[]
40-
texturecoords = Array{TextureCoordinate,1}[]
41-
vertexFaces = Array{SVec{3,UInt32},1}[]
42-
normalFaces = Array{SVec{3,UInt32},1}[]
43-
textureFaces = Array{SVec{3,UInt32},1}[]
38+
temp_vertices = Vertex[]
39+
temp_normals = Normal[]
40+
temp_texturecoords = TextureCoordinate[]
41+
vertexFaces = Array{UInt32,1}[]
42+
normalFaces = Array{UInt32,1}[]
43+
textureFaces = Array{UInt32,1}[]
4444

4545

4646
stream = open(path)
@@ -54,18 +54,18 @@ function loadOBJ(path::String)
5454
if command == "o"
5555
name = lines[end]
5656
push!( names, String(name) )
57-
push!( vertices, Vertex[] )
58-
push!( normals, Normal[] )
59-
push!( texturecoords, TextureCoordinate[] )
60-
push!( vertexFaces, SVec{3,UInt32}[] )
61-
push!( normalFaces, SVec{3,UInt32}[] )
62-
push!( textureFaces, SVec{3,UInt32}[] )
57+
# push!( temp_vertices, Vertex[] )
58+
# push!( temp_normals, Normal[] )
59+
# push!( temp_texturecoords, TextureCoordinate[] )
60+
push!( vertexFaces, UInt32[] )
61+
push!( normalFaces, UInt32[] )
62+
push!( textureFaces, UInt32[] )
6363
elseif command == "v"
64-
push!( vertices[end], parse.(Float32,lines) )
64+
push!( temp_vertices, parse.(Float32,lines) )
6565
elseif command == "vn"
66-
push!( normals[end], parse.(Float32,lines) )
66+
push!( temp_normals, parse.(Float32,lines) )
6767
elseif command == "vt"
68-
push!( texturecoords[end], parse.(Float32,lines) )
68+
push!( temp_texturecoords, parse.(Float32,lines) )
6969
# Handle faces
7070
elseif command == "f"
7171
# Handle missing texture coordinate indices case, ie.
@@ -81,10 +81,8 @@ function loadOBJ(path::String)
8181
push!(normalIndices, parsed[2])
8282
end
8383

84-
vertexTriangle = SVec3( vertIndices )
85-
push!( vertexFaces[end], vertexTriangle )
86-
normalTriangle = SVec3( normalIndices )
87-
push!( normalFaces[end], normalTriangle )
84+
append!( vertexFaces[end], vertIndices )
85+
append!( normalFaces[end], normalIndices )
8886
# Handle full indices case, ie.
8987
# f v1/vt1/vn1 v2/vt2/vn2 v3/vt2/vn3 ...
9088
elseif any(x->occursin("/", x), lines)
@@ -99,30 +97,54 @@ function loadOBJ(path::String)
9997
push!(texCoordinateIndices, parsed[2])
10098
push!(normalIndices, parsed[3])
10199
end
102-
push!( vertexFaces[end], SVec3( vertIndices ) )
103-
push!( normalFaces[end], SVec3( normalIndices ) )
104-
push!( textureFaces[end], SVec3( texCoordinateIndices ) )
100+
append!( vertexFaces[end], vertIndices )
101+
append!( normalFaces[end], normalIndices )
102+
append!( textureFaces[end], texCoordinateIndices )
105103
# Handle only vertex indices
106104
else
107105
parsed = parse.(UInt32, lines)
108-
push!( vertexFaces[end], SVec3( parsed ) )
106+
append!( vertexFaces[end], parsed )
109107
end
110108
end
111109
end
112110
end
113111
close(stream)
114112

115113

114+
# Go through objects' indices and push correct vertex data into their vectors
115+
vertices = Array{Vertex,1}[]
116+
normals = Array{Normal,1}[]
117+
texturecoords = Array{TextureCoordinate,1}[]
118+
119+
#FIXME Handles only .objs with full indices v1/vt1/vn1 case
120+
for name in 1:length(names)
121+
push!( vertices, Vertex[] )
122+
push!( normals, Normal[] )
123+
push!( texturecoords, TextureCoordinate[] )
124+
for i in 1:length(vertexFaces[name])
125+
vFace = vertexFaces[name][i]
126+
nFace = normalFaces[name][i]
127+
uvFace = textureFaces[name][i]
128+
129+
vertexPos = temp_vertices[vFace]
130+
vertexNormal = temp_normals[nFace]
131+
vertexUV = temp_texturecoords[uvFace]
132+
133+
# Add the vertex to the object's data
134+
push!(vertices[name], vertexPos)
135+
push!(normals[name], vertexNormal)
136+
push!(texturecoords[name], vertexUV)
137+
end
138+
end
139+
140+
116141
meshes = Mesh[]
117142
for i in 1:length(names)
118143
mesh = Mesh(
119144
names[i],
120145
vertices[i],
121146
normals[i],
122147
texturecoords[i],
123-
vertexFaces[i],
124-
normalFaces[i],
125-
textureFaces[i]
126148
)
127149
push!( meshes, mesh )
128150
end

0 commit comments

Comments
 (0)