@@ -26,21 +26,21 @@ struct Mesh
26
26
vertices:: Vector{Vertex}
27
27
normals:: Vector{Normal}
28
28
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
32
32
end
33
33
34
34
35
35
36
36
function loadOBJ (path:: String )
37
37
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 }[]
44
44
45
45
46
46
stream = open (path)
@@ -54,18 +54,18 @@ function loadOBJ(path::String)
54
54
if command == " o"
55
55
name = lines[end ]
56
56
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[] )
63
63
elseif command == " v"
64
- push! ( vertices[ end ] , parse .(Float32,lines) )
64
+ push! ( temp_vertices , parse .(Float32,lines) )
65
65
elseif command == " vn"
66
- push! ( normals[ end ] , parse .(Float32,lines) )
66
+ push! ( temp_normals , parse .(Float32,lines) )
67
67
elseif command == " vt"
68
- push! ( texturecoords[ end ] , parse .(Float32,lines) )
68
+ push! ( temp_texturecoords , parse .(Float32,lines) )
69
69
# Handle faces
70
70
elseif command == " f"
71
71
# Handle missing texture coordinate indices case, ie.
@@ -81,10 +81,8 @@ function loadOBJ(path::String)
81
81
push! (normalIndices, parsed[2 ])
82
82
end
83
83
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 )
88
86
# Handle full indices case, ie.
89
87
# f v1/vt1/vn1 v2/vt2/vn2 v3/vt2/vn3 ...
90
88
elseif any (x-> occursin (" /" , x), lines)
@@ -99,30 +97,54 @@ function loadOBJ(path::String)
99
97
push! (texCoordinateIndices, parsed[2 ])
100
98
push! (normalIndices, parsed[3 ])
101
99
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 )
105
103
# Handle only vertex indices
106
104
else
107
105
parsed = parse .(UInt32, lines)
108
- push ! ( vertexFaces[end ], SVec3 ( parsed ) )
106
+ append ! ( vertexFaces[end ], parsed )
109
107
end
110
108
end
111
109
end
112
110
end
113
111
close (stream)
114
112
115
113
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
+
116
141
meshes = Mesh[]
117
142
for i in 1 : length (names)
118
143
mesh = Mesh (
119
144
names[i],
120
145
vertices[i],
121
146
normals[i],
122
147
texturecoords[i],
123
- vertexFaces[i],
124
- normalFaces[i],
125
- textureFaces[i]
126
148
)
127
149
push! ( meshes, mesh )
128
150
end
0 commit comments