function OBJ=read_wobj(fullfilename) % Read the objects from a Wavefront OBJ file % % OBJ=read_wobj(filename); % % OBJ struct containing: % % OBJ.vertices : Vertices coordinates % OBJ.vertices_texture: Texture coordinates % OBJ.vertices_normal : Normal vectors % OBJ.vertices_point : Vertice data used for points and lines % OBJ.material : Parameters from external .MTL file, will contain parameters like % newmtl, Ka, Kd, Ks, illum, Ns, map_Ka, map_Kd, map_Ks, % example of an entry from the material object: % OBJ.material(i).type = newmtl % OBJ.material(i).data = 'vase_tex' % OBJ.objects : Cell object with all objects in the OBJ file, % example of a mesh object: % OBJ.objects(i).type='f' % OBJ.objects(i).data.vertices: [n x 3 double] % OBJ.objects(i).data.texture: [n x 3 double] % OBJ.objects(i).data.normal: [n x 3 double] % % Example, % OBJ=read_wobj('examples\example10.obj'); % FV.vertices=OBJ.vertices; % FV.faces=OBJ.objects(3).data.vertices; % figure, patch(FV,'facecolor',[1 0 0]); camlight % % Function is written by D.Kroon University of Twente (June 2010) verbose=true; if(exist('fullfilename','var')==0) [filename, filefolder] = uigetfile('*.obj', 'Read obj-file'); fullfilename = [filefolder filename]; end filefolder = fileparts( fullfilename); if(verbose),disp(['Reading Object file : ' fullfilename]); end % Read the DI3D OBJ textfile to a cell array file_words = file2cellarray( fullfilename); % Remove empty cells, merge lines split by "\" and convert strings with values to double [ftype fdata]= fixlines(file_words); % Vertex data vertices=[]; nv=0; vertices_texture=[]; nvt=0; vertices_point=[]; nvp=0; vertices_normal=[]; nvn=0; material=[]; % Surface data no=0; % Loop through the Wavefront object file for iline=1:length(ftype) if(mod(iline,10000)==0), if(verbose),disp(['Lines processed : ' num2str(iline)]); end end type=ftype{iline}; data=fdata{iline}; % Switch on data type line switch(type) case{'mtllib'} if(iscell(data)) datanew=[]; for i=1:length(data) datanew=[datanew data{i}]; if(i1), val=tvals(2); if(val<0), val=val+1+nvt; end array_texture(i)=val; end end objects(no).type='l'; objects(no).data.vertices=array_vertices; objects(no).data.texture=array_texture; case('f') no=no+1; %if(mod(no,10000)==1) % objects(no+10001).data=0; %end array_vertices=[]; array_texture=[]; array_normal=[]; for i=1:length(data); switch class(data) case 'cell' tvals=str2double(stringsplit(data{i},'/')); case 'string' tvals=str2double(stringsplit(data,'/')); otherwise tvals=data(i); end val=tvals(1); if(val<0), val=val+1+nv; end array_vertices(i)=val; if(length(tvals)>1), if(isfinite(tvals(2))) val=tvals(2); if(val<0), val=val+1+nvt; end array_texture(i)=val; end end if(length(tvals)>2), val=tvals(3); if(val<0), val=val+1+nvn; end array_normal(i)=val; end end % A face of more than 3 indices is always split into % multiple faces of only 3 indices. objects(no).type='f'; findex=1:min (3,length(array_vertices)); objects(no).data.vertices=array_vertices(findex); if(~isempty(array_texture)),objects(no).data.texture=array_texture(findex); end if(~isempty(array_normal)),objects(no).data.normal=array_normal(findex); end for i=1:length(array_vertices)-3; no=no+1; %if(mod(no,10000)==1), objects(no+10001).data=0; end findex=[1 2+i 3+i]; findex(findex>length(array_vertices))=findex(findex>length(array_vertices))-length(array_vertices); objects(no).type='f'; objects(no).data.vertices=array_vertices(findex); if(~isempty(array_texture)),objects(no).data.texture=array_texture(findex); end if(~isempty(array_normal)),objects(no).data.normal=array_normal(findex); end end case{'#','$'} % Comment tline=' %'; if(iscell(data)) for i=1:length(data), tline=[tline ' ' data{i}]; end else tline=[tline data]; end if(verbose), disp(tline); end case{''} otherwise no=no+1; %if(mod(no,10000)==1), objects(no+10001).data=0; end objects(no).type=type; objects(no).data=data; end end % Initialize new object list, which will contain the "collapsed" objects objects2(no).data=0; index=0; i=0; while (ichar')'; fclose(fid); file_lines = regexp(file_text, '\n+', 'split'); file_words = regexp(file_lines, '\s+', 'split'); function [ftype fdata]=fixlines(file_words) ftype=cell(size(file_words)); fdata=cell(size(file_words)); iline=0; jline=0; while(iline