-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjRead.m
51 lines (49 loc) · 1.6 KB
/
objRead.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [F,V] = objRead(filename)
% Reads a .obj mesh file and outputs the vertex and face list
% assumes a 3D triangle mesh and ignores everything but:
% v x y z and f i j k lines
% Input:
% filename string of obj file's path
%
% Output:
% V number of vertices x 3 array of vertex positions
% F number of faces x 3 array of face indices
%
V = zeros(0,3);
F = zeros(0,3);
vertex_index = 1;
face_index = 1;
fid = fopen(filename,'rt');
line = fgets(fid);
while ischar(line)
vertex = sscanf(line,'v %f %f %f');
face = sscanf(line,'f %d %d %d');
face_long = sscanf(line,'f %d//%d %d//%d %d//%d',6);
face_long_long = sscanf(line,'f %d/%d/%d %d/%d/%d %d/%d/%d',9);
% see if line is vertex command if so add to vertices
if(size(vertex)>0)
V(vertex_index,:) = vertex;
vertex_index = vertex_index+1;
% see if line is simple face command if so add to faces
elseif(size(face,1)==3)
F(face_index,:) = face;
face_index = face_index+1;
% see if line is a face with normal indices command if so add to faces
elseif(size(face_long,1)==6)
% remove normal
face_long = face_long(1:2:end);
F(face_index,:) = face_long;
face_index = face_index+1;
% see if line is a face with normal and texture indices command if so add to faces
elseif(size(face_long_long,1)==9)
% remove normal and texture indices
face_long_long = face_long_long(1:3:end);
F(face_index,:) = face_long_long;
face_index = face_index+1;
else
fprintf('Ignored: %s',line);
end
line = fgets(fid);
end
fclose(fid);
end